Azure Automation: Connect to WVD Environment

Brass Contributor

I am trying to run a PowerShell script using the standard WVD PowerShell commands that detects all disconnected users in a remote desktop session on WVD and log them off as:

 

$TenantName = "<Our-Tenant-Name"
$HostPoolName = "<Our-Hostpool-Name>"

$rdsContext = get-rdscontext -ErrorAction SilentlyContinue
if ($rdsContext -eq $null) {
    try {
        Write-host "Use the login window to connect to WVD" -ForegroundColor Red
        Add-RdsAccount -ErrorAction Stop -DeploymentUrl "https://rdbroker.wvd.microsoft.com"
    }
    catch {
        $ErrorMessage = $_.Exception.message
        write-host ('Error logging into the WVD account ' + $ErrorMessage)
        exit
    }
}


Get-RdsUserSession -TenantName $TenantName -HostPoolName $HostPoolName
$AllDiscUsers = (Get-RdsUserSession -TenantName $TenantName -HostPoolName $HostPoolName | where { $_.SessionState -eq "Disconnected" }).UserPrincipalName 

Foreach ($DiscUser in $AllDiscUsers)
    {
        
        Get-RdsUserSession -TenantName $TenantName -HostPoolName $HostPoolName | where { $_.UserPrincipalName -eq $DiscUser } | Invoke-RdsUserSessionLogoff -NoUserPrompt
                  
    }

 

I am running this as a PowerShell Runbook using the Azure Automation account which is enabled for RUN AS. the script is executed with a status of "Complete" by it doesn't action anything. any ideas of how to get this working?

 

Thanks in advance.

0 Replies