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-SCVirtualMachine

foreach ($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 "savilltech\john" -UserRole $UserRole -Cloud $Cloud
        }
    }
}

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:

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