Run a script on user connection on the VM host

Posted by Scott Chamberlain on Server Fault See other posts from Server Fault or by Scott Chamberlain
Published on 2013-10-30T20:08:38Z Indexed on 2013/10/30 21:59 UTC
Read the original article Hit count: 241

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.

© Server Fault or respective owner

Related posts about powershell

Related posts about hyper-v