It's only 7 months since I first installed Powershell. Hard to believe that last week I saw a copy of the OCS res kit with my name on the cover as a result of the PowerShell scripts I wrote, and worth remembering before criticizing other people's scripts.
Over on Ben Armstrong's blog he has applied his virtualization expertise to showing how to use the newly published WMI management interface, posting samples using VB and "VB in powershell syntax" for example
$VMs = gwmi -class "MSVM_ComputerSystem" -namespace "root\virtualization" -computername "."foreach ($VM in $VMs){ if ($VM.Caption -match "Microsoft Virtual Computer System"){ write-host "==================================" write-host "VM Name: " $VM.ElementName write-host "VM GUID: " $VM.Name write-host "VM State: " $VM.EnabledState } }
VMs = (get-wmiobject -class "MSVM_ComputerSystem" -namespace "root\virtualization") | where {$_.caption -match "Virtual"}
$VMs = get-wmiobject -namespace "root\virtualization" -query `"SELECT * FROM Msvm_ComputerSystem WHERE Caption Like '*virtual*' "
$VMs | Format-Table -autosize ElementName, Name, Enabledstate
$VMs | Format-Table -autosize @{Label="VM Name"; expression={$_.ElementName}} ,@{Label="VM GUID"; expression={$_.Name}} , Enabledstate
@{Label="State"; expression={switch ($_.EnabledState) { 2 {"Running"} 3 {"Stopped"} 32768 {"Paused"} 32769 {"Suspended"} 32770 {"Starting"} 32771 {"Snapshotting"} 32773 {"Saving"} 32774 {"Stopping"} } }}
Function Choose-VM{$global:Counter=-1 $VMs = get-wmiobject -namespace "root\virtualization" -query ` "SELECT * FROM Msvm_ComputerSystem WHERE Caption Like '%virtual%' " Format-Table -inputobject $VMS @{ Label = "ID"; Expression={($global:counter++) }} , @{Label="VM Name"; expression={$_.ElementName}} , @{Label="VM GUID"; expression={$_.Name}} , @{Label="State"; expression={switch ($_.EnabledState) { 2 {"Running"} 3 {"Stopped"} 32768 {"Paused"} 32769 {"Suspended"} 32770 {"Starting"} 32771 {"Snapshotting"} 32773 {"Saving"} 32774 {"Stopping"} } }} | out-host $VMs[ [int[]](Read-Host "Which ones? ").Split(",")]}
Ben did a second post to show starting a virtual machine - to do this I can just use
(choose-vm ) | foreach-object {$_.requestStateChange(2)}
You could use anything which returns Msvm_ComputerSystem WMI objects and you can stop or pause one a machine just by requesting a different state to change to. Shutting down a machine just by setting it's state to 3 for stopped is the virtual equivalent of hitting the power switch. The hyper-v integration components include one to trigger a clean shutdown and Ben shows how that can be used; here's my version
(choose-vm ) | foreach-object { (get-wmiobject -namespace "root\virtualization" -query ` "SELECT * FROM Msvm_ShutdownComponent WHERE SystemName='$($_.name)' ").InitiateShutdown($true,"Because I said so") }
... In which we find VMs, them choose one, start them, stop them , and connect to them. I spent more
Please excuse the bad pun... When I first wrote the function I posted to display the state of virtual
In which we see how to set the number of CPUs I started with getting MSVM Computer System objects - which
Arlindo just pointed me to a new ebook written by my friend Ilse Van Criekinge . Ilse won speaker idol