Email dw Contents About dw
[Company Logo Image]

at The Data Center

"A space for AD, Exchange and other technical stuff"

 

 -- Where Information Technology Lives!  --

 


 

Using powershell to shut down my VMWare lab.

I didn't like the fact my VMWare lab was left on, even when I wasn't using it. I wanted to automate the task of shutting it down, and also wanted to make sure it would come back up right where I left off. Using the new VI Toolkit http://www.vmware.com/beta/vitk_win/index.html, here is what I did.

I downloaded and installed the VI Toolkit so I could use powershell from my laptop. I also downloaded plink.exe from the putty home page (http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html). The script assumes plink.exe is in c:\scripts.

Now, since my Virtual Center server is a virtual, I wanted to make sure that was shut down last, as well.

#This is the ESX server address or name

$servers="192.168.0.254"

$account="root"
$password="password"

get-viserver $servers -User $account -Password $password

$vms = get-vm | where {$_.PowerState -eq "PoweredOn" -and $_.name -ne "VC01"}
foreach ($v in $vms) {suspend-Vm $v.name -Confirm:$false}

start-sleep -s 180
suspend-Vm "VC01" -Confirm:$false

$servers | % { $a=C:\scripts\plink -pw $password $account@$_ "shutdown -h now" }




When you turn the ESX server back on, here is the script to restart the suspended machines. It basically brings you right back to the state you where in when you shut down.

$servers="192.168.0.254"
$account=”root”
$password=”password”
get-viserver $servers -User $account -Password $password

$vms = get-vm | where {$_.PowerState -eq “Suspended”}
foreach ($v in $vms) {start-Vm $v.name -Confirm:$false}
 

 

Update:

Hal Rottenberg sent me an email to give us an easier way. Here is what he said:

Easier way:

 

function Shutdown-VMHost {

    process {

        ( Get-View -VIObject $_ ).ShutdownHost_Task( $TRUE )

    }

}

 

or if you know what a filter is (it's identical to a function with only a process block)

 

filter Shutdown-VMHost { ( Get-View -VIObject $_ ).ShutdownHost_Task( $TRUE ) }

 

Be sure and hang around the VI toolkit community

(http://communities.vmware.com/community/developer/windows_toolkit)

lots of cool stuff going on there.  Also, FYI, I blog about vitk stuff, and I'm writing book on it as well (http://sapienpress.com/vmware.asp).

 

Thanks Hal......