Sunday, July 3, 2011

Start / Stop ESX VM's from PowerShell

Windows PowerShell is a powerful tool for handling the VM's from Local/Remote Systems.
Here, I write a simple batch file to call the powershell script(ps1). The powershell script contains the logic for connecting the ESX server, Getting a VM and start the VM. We can also control the VM for Stop/Suspend.

Stop-VM -VM VMName
Suspend-VM -VM VMName

The following Example, to start the VM from Powershell. We need to install Powershell and VSphere PowerCLI tool in our local PC where we are going to execute the command.

Create a Batch file test1.bat in anywhere in to your System.

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -psc
"C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1"
-c C:\Veeam\StartServer.ps1








Write a PowerCLI command to connect and start a VM.

Connect-VIServer -Server 192.168.0.55 -User root -Password veerasekar

#Checking if VM(CentOS5.2_thin) are running, if not powering the VM on

$VM = Get-VM CentOS5.2_thin

if ($VM.PowerState -ne "PoweredOn")
{

Write-Host "$VM is starting"

Start-VM -VM $VM

write-output "$VM was started" (get-date)

sleep 300
}

















Finally, You just click the test.bat file, it executes the powershell command what we have created in StartServer.ps1 file.



















Thanks,
Veera
Climb Inc,
System Engineer,
http://www.climb.co.jp
070-5573-3176