From 46013c0130e4c102ecf88cb4bb5041e3483e0163 Mon Sep 17 00:00:00 2001 From: Zweili Andreas Date: Mon, 17 Jul 2017 08:35:49 +0200 Subject: [PATCH] add a catch block Otherwise the script doesn't work properly if you don't have the permissions to restart the computer remotely --- scripts/computers/restart-computer.ps1 | 37 ++++++++++++++++---------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/scripts/computers/restart-computer.ps1 b/scripts/computers/restart-computer.ps1 index de115a0..5351969 100644 --- a/scripts/computers/restart-computer.ps1 +++ b/scripts/computers/restart-computer.ps1 @@ -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