Set Cloud and Owner for VMs in VMM

Use PowerShell to easily set the cloud and owner for VMs in VMM.

John Savill

July 18, 2014

1 Min Read
virtual machines

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.

$UserRole = Get-SCUserRole -VMMServer localhost -Name "Lab Tenant" $Cloud = Get-SCCloud -VMMServer localhost | where {$_.Name -eq "Lab Cloud"}$AllVMs = Get-SCVirtualMachineforeach ($VM in $AllVMs){    if ($VM.HostGroupPath.ToLower().contains("hosts`lab hosts"))    {        if ($VM.HostGroupPath.ToLower().Contains("johnsa") )        {            write-host $VM.ComputerName            Set-SCVirtualMachine -VM $VM -Owner "savilltechjohn" -UserRole $UserRole -Cloud $Cloud        }    }}

Note that in the script I check the host group, which is a path (e.g., HostsLab 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:

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

About the Author

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like