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,21 +1,30 @@
function Restart-Domain-Computer
{
$computername = Read-Host 'Please enter a computer name'
Restart-Computer -Force -ComputerName $computername
sleep 30
while ($status -eq $null)
try
{
if(Test-Connection -Computername $computername `
-BufferSize 16 `
-Count 1 `
-Quiet){break}
Write-Host 'Still offline'
sleep 5
$computername = Read-Host 'Please enter a computer name'
Restart-Computer -Force -ComputerName $computername -ErrorAction Stop
sleep 30
while ($status -eq $null)
{
if(Test-Connection -Computername $computername `
-BufferSize 16 `
-Count 1 `
-Quiet){break}
Write-Host 'Still offline'
sleep 5
}
Write-Host
Write-Host -backgroundcolor "green" $computername ' is online'
Write-Host
Read-Host 'Press a key to quit'
}
catch
{
Write-Warning $_
Read-Host 'Press a key to quit'
break
}
Write-Host
Write-Host -backgroundcolor "green" $computername ' is online'
Write-Host
Read-Host 'Press a key to quit'
}
Restart-Domain-Computer