• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Need some Invoke-Command help if you can

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

notarat

Member
Joined
Nov 11, 2010
I've never been great at programming but I have been able to throw code together in the past that doesn't crash our network...

I've thrown together a script that uses a list of computers, and the users who are assigned to each.,

It then queries the computer to determine the size of the assigned user's profile.

This assists us greatly in our efforts at upgrading our users' computers. By starting the upgrades with the users who have the largest profiles, those data transfers can be running while we upgrade other users requiring smaller profile transfers.

The script, however, has 1 drawback...It requires us to know the user assigned to the computer to ensure we're pulling the proper data from the old computer to the new one.

In our environment, the latest user of a computer is the person who is assigned to it. Therefore, I have modified that original script a bit to accomplish the following:

Look up all the user profiles on a computer and determine the most recent user, then record that information, along with the computer name and the Profile Size to a file I can hand off to the people doing the upgrades.

It works well for that purpose...on a single computer.

I need to be able to run the script against a list of computers being upgraded each day but I am totally unfamiliar with the invoke-command commandlet since it's not something I am allowed to run.(I'm NOT an admin nor will I ever be given those credentials)

I'm operating under the assumption that adding a get-content command (to get the list of computers) and then adding an invoke-command statement (to run the script I have on each computer) in the foreach won't work

Can I get some assistance in how to format the invoked-command to run the script remotely? (I can then throw in a try/catch statement in case the computer isn't powered up at the time it's scanned)


My Current Script
Code:
Import-Module ActiveDirectory
$ErrorActionPreference = "SilentlyContinue"
$Startfolder = "C:\Users"
$folders = Get-ChildItem $StartFolder | Where-Object {$_.PSIsContainer -eq $true}
$foldersorted = ($folders | Sort-Object -Property LastWriteTime -Descending)
$strMRP=($foldersorted.name[0])
$folderSize = ( Get-ChildItem $StartFolder\$StrMRP -Recurse -Force | Measure-Object -Property Length -Sum ).Sum
If ( $folderSize -lt 1MB ) {}
ElseIf ( $folderSize -lt 1MB ) { $folderSizeOutput = "$("{0:N2}" -f ($folderSize / 1KB)) KB" }
ElseIf ( $folderSize -lt 1GB ) { $folderSizeOutput = "$("{0:N2}" -f ($folderSize / 1MB)) MB" }
ElseIf ( $folderSize -lt 1TB ) { $folderSizeOutput = "$("{0:N2}" -f ($folderSize / 1GB)) GB" }
ElseIf ( $folderSize -lt 1PB ) { $folderSizeOutput = "$("{0:N2}" -f ($folderSize / 1TB)) TB" }
ElseIf ( $folderSize -ge 1PB ) { $folderSizeOutput = "$("{0:N2}" -f ($folderSize / 1PB)) PB" }
$StrOutput=($env:computername)+","+$strMRP+","+$folderSizeOutput
add-content c:\outputfiles\tout.txt -value $StrOutput


Obviously I would need to do something that looks remotely like:

Code:
$computers = Get-content -path c:\inputfiles\listofcomps.txt
foreach ($computer in $computers) {Invoke-command -computername $computer {

to get the command to run on each $computer in $computers
 
OP
notarat

notarat

Member
Joined
Nov 11, 2010
quick followup to add...

Would adding something like the following work?

Code:
$computers = Get-content -path c:\inputfiles\listofcomps.txt
foreach ($computer in $computers) {Invoke-command -computername $computer –ScriptBlock{My Script}
 
OP
notarat

notarat

Member
Joined
Nov 11, 2010
Surely someone has some invoke-command experience to pass along?