johnsr
April 29, 2019, 9:23pm
1
I need assistance with trying to display the computername of each server that was restarted using the command below. I believe this entails using the variable “$env:computername” but I’m unsure of the proper syntax.
Restart-Computer -computername $serverlist -force -confirm
Thanks
Ron
Putting -Verbose will show the message in console and you can even redirect the verbose stream to a file or to output stream.
Restart-Computer -computername $serverlist -force -confirm -Verbose
Or you can execute this as jobs and get the name from the child jobs created.
$Job = Restart-Computer -computername $serverlist -force -confirm -AsJob
$Job.ChildJobs.Location
If you want to add some error handling, you can see if any computers had an issue.
foreach ($Computer in $serverlist) {
Try {
Restart-Computer -ComputerName $Computer -Force -Confirm:$false -ErrorAction Stop
Write-Output "$Computer was restarted"
}
Catch {
Write-Error $_
}
}