Run a script on user connection on the VM host
- by Scott Chamberlain
I have a server running a Virtual Desktop Managed Pool, what I would like to do is when a user logs in I would like a script to check the number of available VMs and if below a threashold add additional VMs to the pool.
The script to check the load and add to the pool is not the problem, I have that already figured out:
$collectionName = "Test1";
$rdvh = "vmHost.example.com";
$minAvailableVMs = 2;
Import-Module RemoteDesktop;
$pool = Get-VirtualDesktopCollection -CollectionName $collectionName;
$availableVMs = $pool.Size - ($pool.Size * $pool.PercentInUse / 100);
$status = Get-VirtualDesktopCollectionJobStatus $collectionName
#only add new servers if we are below the threashold and in the JOB_COMPLETEED state
if($availableVMs -lt $minAvailableVMs -and $status.Status -eq [Microsoft.RemoteDesktopServices.Management.VirtualDesktopCollectionJobStatus]::JOB_COMPLETED)
{
Add-RDVirtualDesktopToCollection -CollectionName $collectionName -VirtualDesktopAllocation @{"$rdvh" = 1}
}
The problem I am having is, how do I run the above script on the Virtualization Host/Connection Broker/Some other server when a user connects?.
I don't think it would be appropriate to run this as a logon script inside the VM, I think there is a way to do this on the management side but I don't know the new scripting interface in Server 2012 R2 well enough to know which commandlets I should look for to schedule this.
EDIT: I know System Center is perfect for this but I do not have a license and was denied when I asked for it to be added to the budget.