add a catch block

Otherwise the script doesn't work properly if you don't have the
permissions to restart the computer remotely
This commit is contained in:
Zweili Andreas 2017-07-17 08:35:49 +02:00
parent 23314a2dcd
commit 46013c0130
1 changed files with 23 additions and 14 deletions

View File

@ -1,7 +1,9 @@
function Restart-Domain-Computer function Restart-Domain-Computer
{ {
try
{
$computername = Read-Host 'Please enter a computer name' $computername = Read-Host 'Please enter a computer name'
Restart-Computer -Force -ComputerName $computername Restart-Computer -Force -ComputerName $computername -ErrorAction Stop
sleep 30 sleep 30
while ($status -eq $null) while ($status -eq $null)
{ {
@ -16,6 +18,13 @@ function Restart-Domain-Computer
Write-Host -backgroundcolor "green" $computername ' is online' Write-Host -backgroundcolor "green" $computername ' is online'
Write-Host Write-Host
Read-Host 'Press a key to quit' Read-Host 'Press a key to quit'
}
catch
{
Write-Warning $_
Read-Host 'Press a key to quit'
break
}
} }
Restart-Domain-Computer Restart-Domain-Computer