-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRestart_VMware_guest.ps1
72 lines (60 loc) · 2.86 KB
/
Restart_VMware_guest.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# This script can be used to restart a guest VM (leverages VMware Tools) running on a VMware ESXi host.
# Notes are written to use this script as part of a System Center Orchestrator runbook.
# Set script parameters from runbook data bus and Orchestrator global variables
$vCenter = "insert value here"
$OrchestratorUser = "insert value here"
$OrchestratorPassword = "insert value here"
$ServerName = "insert value here"
$ErrorMessage = ""
$ResultStatus = "Success"
try
{
#Set Logging
. "insert path here\Loggingscript.ps1"
$LogfileName = $ServerName + "_" + (Get-Date -Format "MM-dd-yyyy") + ".log"
$LogfilePath = "insert path here\$LogfileName"
write-log -path $LogfilePath -Message "The Restart Guest OS script is starting."
# Load PowerCLI modules
Import-Module "VMware.VimAutomation.Core"
Import-Module "VMware.VimAutomation.Vds"
Import-Module "VMware.VimAutomation.Cis.Core"
Import-Module "VMware.VimAutomation.Storage"
Import-Module "VMware.VimAutomation.HA"
Import-Module "VMware.VimAutomation.vROps"
Import-Module "VMware.VumAutomation"
Import-Module "VMware.VimAutomation.License"
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -DisplayDeprecationWarnings $false -WebOperationTimeoutSeconds 90 -Confirm:$false | Out-Null
# Connect to vCenter server after disconnecting any existing sessions
if($global:DefaultVIServers)
{
Disconnect-VIServer -Server $global:DefaultVIServers -Force -Confirm:$false
}
$vcenterServer = Connect-VIServer -Server $vCenter -User $OrchestratorUser -Password $OrchestratorPassword
if($vcenterServer -eq $null)
{
write-log -path $LogfilePath -Message "The Restart Guest OS script failed to connect to $vCenter vCenter. The vCenter variable is null."
throw "ERROR: Failed to connect to vCenter server [$vCenter]"
}
else
{
if ($ServerName)
{
get-vm $ServerName | get-vmguest | restart-vmguest -Confirm:$false
write-log -path $LogfilePath -Message "The Restart Guest OS script just attempted to restart $ServerName via vCenter."
Disconnect-VIServer -Server $vCenter -Confirm:$false
}
else
{
write-log -path $LogfilePath -Message "ERROR: The ServerName variable is blank. Value = $ServerName"
throw "ERROR: The ServerName variable is blank. Value = $ServerName"
Disconnect-VIServer -Server $vCenter -Confirm:$false
}
}
}
catch
{
# Catch any errors thrown above, set result status.
$ResultStatus = "Failed"
$ErrorMessage = $error[0].Exception.Message
write-log -path $LogfilePath -Message "The Restart Guest OS script completed with a ResultStatus of $ResultStatus. Error: $ErrorMessage"
}