Match a UPN list to assigned VM names using Powershell

226 Views Asked by At

I am trying to pass a list of UPNs in to a function to find all Sessionhosts (Virtual Machines) assigned to that UPN in Azure WVD. I would like to match those sessionhost names to the UPN in the list I am passing through and it's just beyond my skill level at this moment. Thank you to anyone who may be able to help me.

The input looks like this.

email1  
email2

Output looks like this.

vmname1  
vmname2  
othervmname1  
othervmname2  

The output I'd love to be able to figure out would be to create an array, or something, with two columns where id have the output like so:

email1 : vmname1  
email1 : vmname2  
email2 : othervmname1  
email2 : othervmname2  

My code is below.

Add-RdsAccount -DeploymentUrl "https://rdbroker.wvd.microsoft.com" | Out-Null
 
$upnlist = get-content -path c:\path\to\upnlist.txt
 
#Function to find the session hosts the user is a part of in the WVD Fall 2019 environment.
function Get-FallSessionName {
           
    $Tenants = "tenant,tenant2,tenant3"
    
    ForEach ($upn in $upnlist) {
       
        ForEach ($Tenant in $Tenants) {
           
            $Hostpools = (Get-RdsHostPool -TenantName $Tenant).HostPoolName
           
            foreach ($Hostpool in $Hostpools) {  
                    
                (Get-RdsSessionHost -TenantName $Tenant -HostPoolName $Hostpool | where-object {$_.AssignedUser -eq $upn}).SessionHostName)
            }
        }      
    }
    Return $SessionHostName
}
 
 
$2019SessionNames = Get-FallSessionName
 
$2019SessionNames | Out-GridView
1

There are 1 best solutions below

3
On

Unfortunately I have no way to try this function and see if it works as expected, I have modified your code and gave you a few hints.

IMPORTANT I'm not 100% sure that the property AssignedUser of Get-RdsSessionHost will return a UserPrincipalName. You would need to evaluate your code in case it is returning something different.

Try and see if it works:

Add-RdsAccount -DeploymentUrl "https://rdbroker.wvd.microsoft.com" | Out-Null
 
$upnlist = get-content -path c:\path\to\upnlist.txt

function Get-FallSessionName {
param(
    [string[]]$UserPrincipalName,
    [string[]]$Tenants
)

    ForEach ($Tenant in $Tenants)
    {
        $Hostpools = (Get-RdsHostPool -TenantName $Tenant).HostPoolName
        Foreach ($Hostpool in $Hostpools)
        {
            # This should find all the Hosts where the property 'AssignedUser' is equal to ANY
            # item on the 'UserPrincipalName' array.
            $sessionHosts = (Get-RdsSessionHost -TenantName $Tenant -HostPoolName $Hostpool |
                where-object {$_.AssignedUser -in $UserPrincipalName}).SessionHostName
            
            # Since Get-RdsSessionHost can return multiple SessionHostNames we need to
            # loop through the possible array $sessionHosts
            foreach($hostName in $sessionHosts)
            {
                # Here is where you can define how your object should look like

                [pscustomobject]@{
                    Tenant=$Tenant
                    HostPool=$Hostpool
                    SessionHostName=$hostName
                }
            }
        }
    }
}

# -> This is how your function should be called. Parameters should not be hardcoded inside a function
$2019SessionNames = Get-FallSessionName -UserPrincipalName $upnlist -Tenants 'tenant','tenant2','tenant3'
$2019SessionNames | Out-GridView

$2019SessionNames should look like this:

Tenant HostPool SessionHostName
Tenant1 HostPool1 [email protected]
Tenant2 HostPool2 [email protected]