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:
parent
23314a2dcd
commit
46013c0130
|
@ -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
|
||||
|
|
Reference in New Issue