-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckAndRestart-VM.ps1
49 lines (45 loc) · 1.31 KB
/
CheckAndRestart-VM.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#Powershell HyperV VM-Check for N-able RMM
#Author: Andreas Walker [email protected]
#Licence: GNU General Public License v3.0
#Version: 1.0.1 / 12.08.2021
#Define paremeter
Param (
[string]$VM,
$Start = 'no'
)
#Get status of selected VM
$VMStatus = Get-VM | where {$_.Name -eq $VM}
#Check Status of selected VM
if ($VMStatus.State -like "Running")
{
Write-Host "OK - VM $VM is running for"$VMStatus.Uptime
Exit 0
}
else
{
Write-Host "Error - VM $VM is not running"
if ($Start -eq 'no')
{
Exit 1001
}
else
{
Write-Host "Trying to Start VM $VM"
Start-VM -Name $VM
Start-Sleep -Seconds 60
$NewVMStatus = Get-VM | where {$_.Name -eq $VM}
if ($VMStatus.State -like "Running")
{
Write-Host "VM $VM is running now"
Exit 0
}
else
{
Write-Host "VM $VM is still not running"
Exit 1001
}
}
}
#Catch unexpected end of Script
Write-Host "FAILURE - Script ended unexpected"
Exit 1001