I made this short script to monitor and, if needed, restart the printer spooler on a few servers
$c = Get-Credential
$servers = 'FQDN1', 'FQDN2', 'FQDN3'
foreach ($s in $servers){
Invoke-Command -ComputerName $s -Credential $c {$j = (Get-PrintJob -PrinterName 'Test Printer').count
Write-Host "On computer $s there are $j print jobs"
If ($j -gt 5){
Write-Host "About to restart the printer spooler on $s"
Restart-Service 'Spooler'
}
} # end of invoke-command
} # end of foreach
What I don't understand is why the Write-Host does not write the server name ($s), but it writes instead the number of jobs ($j).
I guess it has something to do with the variable being in the remote session but not in the local one. But I can't really understand what exactly is the problem.