Assign VM to Tenant in Azure Pack with PowerShell

#Initialize Variables
$pattern = Read-Host -Prompt "Input Username or part of it"
$VMName = Read-Host -Prompt "Input VM Name"

#Establish Connection to VMM
$vmm = Get-VMMServer
$vmmSPF = Get-VMMServer -ForOnBehalfOf

#Construct Variables
$tmpRole = Get-SCUserRole -VMMServer $vmm | ? name -like "*$pattern*"
$Owner = ($tmpRole.Name).Split("_") | Select -First 1
$UserRole = $tmpRole.Name
$Role = Get-SCUserRole -Name $UserRole 

#Assign VM to Tenant
$vm | Set-SCVirtualMachine -UserRole $Role
$vm | Set-SCVirtualMachine -OnBehalfOfUser $Role -OnBehalfOfUserRole $role -Owner $Owner

I got really bored with assigning random VM’s by hand

Written on November 16, 2015