I wrote this script because Azure Remote App is shit and always causes problems for users. One of the bigger problems is when a user’s application within Azure Remote App freezes, the user can’t simple restart the remote app as the server keeps the session and frozen/crashed application alive.

The solution to this is to log the user off the server. This module is designed to be a super simple way to log off a user just by using their email address that is associated with the Azure Remote App account.

function global:end-azureSession{
    Param(
        [parameter(HelpMessage='your username/email for azure')]
        [string]$userEmail,
        [parameter(HelpMessage='your password for azure')]
        [string]$cred
    )

    #define our set variables for the penrith enviroment
    $azureSubscription = 'PCC-AAE'

    #create credentials
    #$secPass = ConvertTo-SecureString $password -AsPlainText -Force
    if($cred){
        #$AzureCred = New-Object System.Management.Automation.PSCredential ($username, $secPass)
    }else{
        $cred = Get-Credential
        #$AzureCred = New-Object System.Management.Automation.PSCredential
    }
    #this will throw exception even if success.... so yep... this is my work-around...
    try{Add-AzureAccount -Credential $cred}catch{write-host "Connected to Azure" -ForegroundColor green}

    #Select subscription
    Select-AzureSubscription $azureSubscription

    #disconnect user
    write-host "Disconnecting user, this may take a while....." -ForegroundColor green
    try{
        invoke-AzureRemoteAppSessionLogoff -CollectionName rappaaeprod -UserUpn $userEmail -confirm:$false >$null 2>&1
        write-host "diconnected..." -ForegroundColor green
    }catch{
        $errorMessage = $_.Exception.Message
        if($errorMessage -eq "InternalError: The server encountered an internal error. Please retry the request."){
            write-host "User has been disconnected" -ForegroundColor green
        }else{
            write-host "ERROR: User connection to Azure not found" -ForegroundColor red
        }
    }
}

 

Example 1: this will prompt you for credentials then end the users session

end-azureSession Nathan.kewley@penrith.city

Example 2: You can save and pass credentials to the module

$cred = get-credential
end-azureSession Nathan.kewley@penrith.city $cred