I found a Powershell script that checks all automatic services on Windows servers and if they are not started, tries to start them. Here is the link http://doitcloudy.blogspot.de/2014/07/scriptgesteuerter-neustart-von-windows.html (It's German, but the script on the bottom of the page is all English). I would like to customize it a bit. However, I need some help with this part (lines 130 and following):
foreach ($item in $report){
Write-Host "Starting Service " $item.Name " on server: " $item.Server -Foregroundcolor yellow
$start = Get-Date
$startSVCblock = {param($item) Start-Service -InputObject (Get-Service -ComputerName $item.Server -Name $item.Name)}
$j = Start-Job -ScriptBlock $startSVCblock -Arg $item
do {
if ($j.State -ne 'Running') { break}
$j | Receive-Job
} while (((Get-Date) - $start) -le $timeout)
}
I see that this is the part where the script starts a service, but I'm not used to "jobs" in Powershell. How should I edit the script so that when starting the service fails, it writes the service name to a log file? Maybe with try/catch?
Any help is greatly appreciated!