Forum Discussion

FortyMegabytes's avatar
FortyMegabytes
Brass Contributor
Nov 06, 2020

Using Add-RdsAccount in a function

I want to use Add-RdsAccount in a function that's called by other scripts.  But for the life of me, I cannot make work.  The "Add-RdsAccount" call works and successfully authenticates, but *only* for the local scope.  I cannot get that authentication to persist within a script or globally.  Every single time I want to call a new function to perform some task with WVD, I have to call "Add-RdsAccount" directly from that function.

 

I've never had this problem before.  I can authenticate to Azure, for example, and have that authentication persist script-wide or globally.  There's something peculiar about Micorsoft.RDInfra.Powershell that doesn't allow "Add-RdsAccount" to persist outside a local scope.

Has anyone else come across this and found a solution?

  • BrianJones's avatar
    BrianJones
    Copper Contributor

    FortyMegabytes 

     

    I was able to get the Add-RdsAccount command to work inside a class method (and the same should work inside a function) like this:

     

    Class RdsClass {
            [void] RdsConnect ([string]$DeploymentUrl, [PSCredential]$AzureCredential) {
                    Add-RdsAccount -DeploymentUrl $DeploymentUrl -Credential $AzureCredential
    
                    $Global:RdMgmtContext = (Get-Variable -Name RdMgmtContext).Value
                    $Global:AdalContext = (Get-Variable -Name AdalContext).Value
            }
    }

     

    Then I am able to use:

     

    [RdsClass]::New().RdsConnect($MyDeploymentUrl, $MyAzureCredentials)

Resources