Q: How can I easily set a cloud and owner for virtual machines in System Center Virtual Machine Manager?

A: It's possible to manually set the cloud and owner of a virtual machine in the System Center Virtual Machine Manager (VMM) management console, but I had a lot of virtual machines that I wanted to set in a cloud and set an owner for. I came up with a basic script that searches for virtual machines in a certain host group, and if the virtual machine contains a string of characters it then sets a particular user to be the owner. In my example, I search for johnsa within the computername and then set the new owner.

  1. $UserRole = Get-SCUserRole -VMMServer localhost -Name "Lab Tenant"
  2. $Cloud = Get-SCCloud -VMMServer localhost | where {$_.Name -eq "Lab Cloud"}
  3. $AllVMs = Get-SCVirtualMachine
  5. foreach ($VM in $AllVMs)
  6. {
  8.     if ($VM.HostGroupPath.ToLower().contains("hosts`\lab hosts"))
  9.     {
  10.         if ($VM.HostGroupPath.ToLower().Contains("johnsa") )
  11.         {
  12.             write-host $VM.ComputerName
  13.             Set-SCVirtualMachine -VM $VM -Owner "savilltech\john" -UserRole $UserRole -Cloud $Cloud
  14.         }
  15.     }
  16. }

Note that in the script I check the host group, which is a path (e.g., Hosts\Lab Hosts); however, in PowerShell you must "escape" the slash so I use `\ and I write all as lowercase. If you had a more specific naming scheme you could change the script to extract the username from the virtual machine name then apply as owner.

You can use the following PowerShell code to quickly check the cloud and owner of all virtual machines:

  1. Get-SCVirtualMachine | Format-Table ComputerName, HostGroupPath, Cloud, Owner -AutoSize