Powershell Multithreading

by justusiv at 2013-04-10 12:36:13

Ok i am a powershell newbie here. I did whip up a script but this single threaded speed is killing me. Thoughts on what i should do here?
$cred = get-credential #Creds used for the invoke-command
$abccomputers = "abc.forest.org/abc Computers/DEPT/DEPT Computers/Windows 7/Department"
$items = Get-QADComputer -SearchRoot $abccomputers
$abc = ".abc.forest.org"
$backuptime = 7


foreach ($item in $items){ #Loop all computers found in AD query
$FQDN = $item.name+$abc #FQDN is required so this converts to FQDN
if ($item.name -ne "Directory") {
$version = .\wbadmin.exe get versions -backuptarget:\DEPTbackup0\e$\ClientBackup -machine:$item.name | select-string Version #this querys the backup to peak at the backup time.
if ($?) { #If backup is found
$a = get-date #cleans up date
$version = $version -replace("Version identifier: ","")
$days = ($a - [datetime]$version).days
$hours = ($a - [datetime]$version).hours
Write-Host $item.Name -foregroundcolor black -backgroundcolor green -nonewline; write-host " AGE:"$days","$hours #displays Computer name + backup age
if ($days -ge $backuptime){ #Only back if the backup is to old
Invoke-command -computername $FQDN -cred $cred -auth credssp -scriptblock{ #what kicks off the backup to the computer
WBADMIN START BACKUP -backuptarget:\DEPTbackup0\e$\ClientBackup -allcritical -quiet #meat & potatoes
}
}
}
else { #If backup is not found
ping $item.Name -n 1 | Out-Null #ON check
if ($?) { #If on
Write-Host $item.Name -foregroundcolor black -backgroundcolor Magenta #for visual report
Write-Host $FQDN -nonewline; write-host " Description:"$item.Description #for visual report
Invoke-command -computername $FQDN -cred $cred -auth credssp -scriptblock{ #what kicks off the backup to the computer
WBADMIN START BACKUP -backuptarget:\DEPTbackup0\e$\ClientBackup -allcritical -quiet #meat & potatoes
}
}
else { #NO backup and off
Write-Host $item.Name -foregroundcolor black -backgroundcolor red
}
}
}
}
by DonJ at 2013-04-10 12:51:56
Workflow (parallel) or jobs or switch to C#
by ArtB0514 at 2013-04-10 12:53:56
A little expansion on Don’s reply:

Since it doesn’t seem like you do anything with what Invoke-Command might return and they seem to be the only parts of the script that might take any time, why not run them -AsJob?
by justusiv at 2013-04-10 13:50:39
Like i said i am very new powershell and i don’t really code, Not apologizing just stating so i comply with the rules :slight_smile:
Once things go out of linear code i got lost. I don’t have a reason why i did things that way i did it was just the only way i knew how.
with the -asjob it processes and proceeds on i assume. With my setup i can only handle a certain amount of jobs at a time. so if i spin up jobs i will need to throttle.
I have read through this post http://www.get-blog.com/?p=22 but it got complicated quick.
by ArtB0514 at 2013-04-11 06:26:12
Here is where you do this to find out what help topics are available and then read the individual topics:

Get-Help about_job