Azure Automation: Connect to WVD Environment

%3CLINGO-SUB%20id%3D%22lingo-sub-1416990%22%20slang%3D%22en-US%22%3EAzure%20Automation%3A%20Connect%20to%20WVD%20Environment%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1416990%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20trying%20to%20run%20a%20PowerShell%20script%20using%20the%20standard%20WVD%20PowerShell%20commands%20that%20detects%20all%20disconnected%20users%20in%20a%20remote%20desktop%20session%20on%20WVD%20and%20log%20them%20off%20as%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%24TenantName%20%3D%20%22%3COUR-TENANT-NAME%3E%22%0A%0A%24rdsContext%20%3D%20get-rdscontext%20-ErrorAction%20SilentlyContinue%0Aif%20(%24rdsContext%20-eq%20%24null)%20%7B%0A%20%20%20%20try%20%7B%0A%20%20%20%20%20%20%20%20Write-host%20%22Use%20the%20login%20window%20to%20connect%20to%20WVD%22%20-ForegroundColor%20Red%0A%20%20%20%20%20%20%20%20Add-RdsAccount%20-ErrorAction%20Stop%20-DeploymentUrl%20%22https%3A%2F%2Frdbroker.wvd.microsoft.com%22%0A%20%20%20%20%7D%0A%20%20%20%20catch%20%7B%0A%20%20%20%20%20%20%20%20%24ErrorMessage%20%3D%20%24_.Exception.message%0A%20%20%20%20%20%20%20%20write-host%20('Error%20logging%20into%20the%20WVD%20account%20'%20%2B%20%24ErrorMessage)%0A%20%20%20%20%20%20%20%20exit%0A%20%20%20%20%7D%0A%7D%0A%0A%0AGet-RdsUserSession%20-TenantName%20%24TenantName%20-HostPoolName%20%24HostPoolName%0A%24AllDiscUsers%20%3D%20(Get-RdsUserSession%20-TenantName%20%24TenantName%20-HostPoolName%20%24HostPoolName%20%7C%20where%20%7B%20%24_.SessionState%20-eq%20%22Disconnected%22%20%7D).UserPrincipalName%20%0A%0AForeach%20(%24DiscUser%20in%20%24AllDiscUsers)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20Get-RdsUserSession%20-TenantName%20%24TenantName%20-HostPoolName%20%24HostPoolName%20%7C%20where%20%7B%20%24_.UserPrincipalName%20-eq%20%24DiscUser%20%7D%20%7C%20Invoke-RdsUserSessionLogoff%20-NoUserPrompt%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%7D%3C%2FOUR-TENANT-NAME%3E%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20running%20this%20as%20a%20PowerShell%20Runbook%20using%20the%20Azure%20Automation%20account%20which%20is%20enabled%20for%20RUN%20AS.%20the%20script%20is%20executed%20with%20a%20status%20of%20%22Complete%22%20by%20it%20doesn't%20action%20anything.%20any%20ideas%20of%20how%20to%20get%20this%20working%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20in%20advance.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1416990%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAutomation%20%26amp%3B%20Control%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Highlighted
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