Saturday, April 14, 2012

PowerCLI: Start and Stop the VMs resides on a vCenter Folder

Starting all the VMs in the folder 'production1'

$vms = Get-VM -Location production1

ForEach($vm in $vms)                 # Start each VM in the folder 'production1'
{
start-vm -RunAsync -VM $vm -Confirm:$false
}

Stopping all the VMs in the folder 'production1'

$vms = Get-VM -Location production1
ForEach($vm in $vms)
# Try a graceful shutdown of VMGuest if vmware tools are installed, Otherwise just stop the VM
{
$vm_view = $vm | get-view
$vmtoolsstatus = $vm_view.summary.guest.toolsRunningStatus
Write-Host “VM $vm says tools status is $Vmtoolsstatus”
if ($vmtoolsstatus -eq “guestToolsRunning”)
{
Shutdown-VMGuest -VM $vm -Confirm:$false       # Shutting down VMGuest gracefully
}
else
{
Stop-VM -RunAsync -VM $vm -Confirm:$false      # Stopping the VM
}
}

No comments:

Post a Comment

Popular Posts

About Me

My photo
I have started this blog to share my work experience and spread some smart solutions on Linux to Internet community. I'm hoping more people will get benefited from this blog. Brief about me: I have 14+ years experience working as System Admin and currently work with VMware.