Some time ago i wanted to to run some build scripts in my home lab which needed to power off and on a virtual machine running Independent Non-Persistent disks in order for the VM to be in a clean state before each build. However i was running the free hypervisor version of ESXI so the api was not available, then i figured that i could do this via SSH and Powershell. In order for the example script below to work you will need to do a couple of things to the esxi server and the machine executing the script.
You will first need to make sure you have the powershell ssh module copied to the following locations on the machine executing the script:
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\Modules
C:\Windows\System32\WindowsPowerShell\v1.0\Modules
You can download the file from here: SSH-Sessions
*Note i am not the creator of the SSH-Module and the original is located at http://www.powershelladmin.com
Then you will need to generate a public/private key for the SSH user you are going to use and load the public key into ESXI. VMware has made a nice set of instruction on how to do it here.
Keep the private key as you will need it when running the script. You will also need to enable SSH and allow it thru the firewall on the ESXI server.
The below script is an example of what you can do with powershell and ssh on an ESXI server. The script will look for a vm on a specific ESXI server you have specified, it will then check if it’s powered on, if so it will power it off, then it will power it on and wait for VMware tools to be up before finishing:
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
<#
.SYNOPSIS
This script is used to powerdown and powerup a virtual machine on an ESXI host (Luben Kirov)
.PARAMETER vname
The name Of the virtual machine
.PARAMETER vmhost
The name of the virtual machine host(ESXI)
.PARAMETER username
The username used to log into the ESXI server
.PARAMETER keyfile
The location of the private key
.EXAMPLE
Powercycle.ps1 -vmname MyTestVm -vmhost "test-esxi01" -username root -keyfile private1
#>
param([string]$vmname = "vmname", [string]$vmhost = "vmhost", [string]$username = "username", [string]$keyfile = "keyfile")
Import-Module SSH-Sessions #import the powershell module
#Disconnect session in case one is opened
remove-sshsession -computername $vmhost
#Connect to ESXI host
new-SSHSession -computername $vmhost -username $username -KeyFile $keyfile
#Check and get the vm id, if vm does not exist exit
$result = invoke-sshcommand -computername $vmhost -command "vim-cmd vmsvc/getallvms|grep -i $vmname" -quiet
$test = $result.split(" ")
$vmid = $test[0]
If ($vmid -eq ""){
Write-Host " VM not found on host"
remove-sshsession -computername $vmhost
exit 1
}
#Check vm power state if powered on power off the vm
$vmPowerState = invoke-sshcommand -computername $vmhost -command "vim-cmd vmsvc/power.getstate $vmid |grep Powered" -quiet
if ($vmPowerState -eq "Powered on"){
invoke-sshcommand -computername $vmhost -command "vim-cmd vmsvc/power.off $vmid" -quiet
$VMtoolsNot = "False"
$VMtoolsNotc = 0
While($VMtoolsNot -eq "False"){
If ($VMtoolsNotc -eq 40){
Write-Host " VM did not power down in 120 seconds, exiting"
remove-sshsession -computername $vmhost
exit 1
}
$VMtoolsNotResult = invoke-sshcommand -computername $vmhost -command "vim-cmd vmsvc/get.summary $vmid |grep toolsRunningStatus" -quiet
$VMtoolsNotResult1 = $VMtoolsNotResult.split('"')
If($VMtoolsNotResult1[1] -eq "guestToolsNotRunning"){
$VMtoolsNot = "True"
Write-Host " VM Powered Down"
}else{
Start-Sleep -s 3
$VMtoolsNotc++
}
}
}
#Power on the Vm
invoke-sshcommand -computername $vmhost -command "vim-cmd vmsvc/power.on $vmid" -quiet
$VMtools = "False"
$VMtoolsc = 0
While($VMtools -eq "False"){
If ($VMtoolsc -eq 40){
Write-Host " VM did not start in 120 seconds exiting"
remove-sshsession -computername $vmhost
exit 1
}
$VMtoolsResult = invoke-sshcommand -computername $vmhost -command "vim-cmd vmsvc/get.summary $vmid |grep toolsRunningStatus" -quiet
$VMtoolsResult1 = $VMtoolsResult.split('"')
If($VMtoolsResult1[1] -eq "guestToolsRunning"){
$VMtools = "True"
Write-Host " VM Powered Up"
}else{
Start-Sleep -s 3
$VMtoolsc++
}
}
#Disconnect session
remove-sshsession -computername $vmhost
exit 0
|