FeaturedPower ShellScripts
Check Multiple Patch IDs installed on Remote Machine

Use this script to find out if multiple Patch using KB IDs installed on remote machine
- it checks if machine is online and pingable
- create new ps session
- running background job on remote machine
- waiting for all jobs to be completed on Remote Servers
- Getting Results from jobs
- Saving Results to a file
$ErrorActionPreference = "silentlycontinue"
$servers = Get-Content -Path "C:\2016.txt"
write-host "################################################################################" -ForegroundColor Green
Write-host "###################### Checking if all remote servers are online ###############" -foregroundcolor Green
write-host "################################################################################" -ForegroundColor Green
foreach($comp in $servers){
$pingtest = Test-Connection -ComputerName $comp -Quiet -Count 1 -ErrorAction SilentlyContinue
if($pingtest){
Write-Host($comp + " is online")
}
else{
Write-Host($comp + " is not reachable") -ForegroundColor Red
}
}
write-host "################################################################################" -ForegroundColor Green
write-host "############# Creating New Sessions on all remote servers ####################" -ForegroundColor Green
write-host "#################################################################################" -ForegroundColor Green
Write-host "Creating New Sessions on all remote servers" -foregroundcolor Green
$s = New-PSSession -computername $servers
write-host "###################################################################################################" -ForegroundColor Green
Write-host "##### Runing Background Jobs on Remote Servers to get installed patches with target Kbs #####" -foregroundcolor Green
write-host "###################################################################################################" -ForegroundColor Green
Invoke-Command -session $s -scriptblock {
start-job -ScriptBlock {
$kbs = @(
'kb5005036',
'kb5005627',
'kb5005613',
'kb5005106',
'kb5005076',
'kb5005563',
'kb5005094',
'kb5005607',
'kb5005623'
'kb5005099'
)
$Installedkbs = (Get-HotFix).HotFixId
$kbs | foreach {
if ($Installedkbs -contains $_) {
Write-Output "$env:computername Hotfix $_ installed";
} else {
Write-Output "$env:computername Hotfix $_ missing";
}
}
} -Name job
}
#$job = Invoke-Command -Session $s -ScriptBlock {Get-Job -name job }
write-host "################################################################################" -ForegroundColor Green
write-host "######### waiting for all jobs to be completed on Remote Servers ###########" -ForegroundColor Green
write-host "#################################################################################" -ForegroundColor Green
Invoke-Command -Session $s -ScriptBlock {Get-Job | wait-job -any }
$result = Invoke-Command -Session $s -ScriptBlock {receive-job -name job -Keep }
write-host "######################################################################" -ForegroundColor Green
write-host "################ Getting Results from jobs ########################" -ForegroundColor Green
write-host "#####################################################################" -ForegroundColor Green
$result
write-host "########## Saving Results to a file #############" -ForegroundColor Green
$result | Out-File -FilePath C:\$((Get-Date).ToString("yyyy-MM-dd-HH_MM"))-hotfix-Result-2016.csv -Append
write-host "######################################################################" -ForegroundColor Green
write-host "########## Removing all jobs from all remote servers #############" -ForegroundColor Green
write-host "#####################################################################" -ForegroundColor Green
Invoke-Command -Session $s -ScriptBlock {Get-Job | Remove-Job}


