Listing virtual machines under Powershell

There are a lot of things you can do with Virtual Server and Powershell by just interacting with the COM object directly at the command line.  There are, however, a couple of things that are quite hard.  One of the first things you might run into is that it is relatively difficult to just get a list of available virtual machines on a system.  The reason for this is that in order to get a virtual machine object you either need to know its name, or to iterate over all virtual machines.

With that in mind here is a handy little script that will list all virtual machine names under Powershell:

$vs=new-object –com VirtualServer.Application –Strict

$result = SetSecurity($vs)

$vms = $vs.VirtualMachines

$result = SetSecurity($vms)

write-host
write-host "The following virtual machines are configured:"
write-host

for ($i = 1; $i –le ($vms.count); $i++) {
$vm = $vms.Item($i)
$result = SetSecurity($vm)
write-host $vm.name
}

write-host

If you save this script as 'listVMs.ps1' it will allow you to easily list virtual machine names so that you can continue from there.  Once again, in order for this to work you need to have your Powershell environment setup as detailed in this post and this post.

Cheers,
Ben