Multi Threads

Hello,

 

i have a Script for Install Security Hotfixes remote.The Script is great, but if you add a lot of hosts it take a long time to finish the deployment. Have anyone a idea how i can add multi threads?

Here is the code:

$RootHotfixPath = 'C:\Temp\'

$Hotfixes = @('windows6.1-kb4499175-x64_3704acfff45ddf163d8049683d5a3b75e49b58cb.msu')
$Servers = Get-Content 'C:\temp\MachineList.txt'

foreach ($Server in $Servers)
{
Write-Host "Processing $Server..."

$needsReboot = $False
$remotePath = "\\$Server\c$\Temp\Patches\"

if( ! (Test-Connection $Server -Count 1 -Quiet))
{
Write-Warning "$Server is not accessible"
continue
}

if(!(Test-Path $remotePath))
{
New-Item -ItemType Directory -Force -Path $remotePath | Out-Null
}

foreach ($Hotfix in $Hotfixes)
{
Write-Host "`thotfix: $Hotfix"
$HotfixPath = "$RootHotfixPath$Hotfix"

Copy-Item $Hotfixpath $remotePath
# Run command as SYSTEM via PsExec (-s switch)
& C:\Tools\PSTools\PsExec -s \\$Server wusa C:\Temp\Patches\$Hotfix /quiet /norestart
write-host "& C:\Tools\PSTools\PsExec -s \\$Server wusa C:\Temp\Patches\$Hotfix /quiet /norestart"
if ($LastExitCode -eq 3010) {
$needsReboot = $true
}
}

# Delete local copy of update packages
Remove-Item $remotePath -Force -Recurse

if($needsReboot)
{
Write-Host "Restarting $Server..."
#Restart-Computer -ComputerName $Server -Force -Confirm
}
}

Yup, because you are doing it sequentially. Psexec do have an option to read the computer list from a file. You could use that option.

Example

psexec @C:\TEMP\serverlist.csv -u UrDomain\Username -c c:\file.exe