0

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
  • Create a ps custom object (and then add those to an array) or create a hashtable – guiwhatsthat Apr 28 '21 at 12:51
  • Can you give me a generic example of how to do that? That's kind of what I assumed but I don't have experience doing that. – jsolj3x Apr 28 '21 at 13:06
  • 1
    google is your friend ;) [hashtables](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_hash_tables?view=powershell-7.1) [pscustomobject](https://ridicurious.com/2018/10/15/4-ways-to-create-powershell-objects/) – guiwhatsthat Apr 28 '21 at 13:12
  • take a look at the links they provide generic examples – guiwhatsthat Apr 28 '21 at 13:18
  • 1
    I genuinely apologize. New to stackoverflow and didn't realize links were there and have been frustrated with this. Thought I got a RTFM reply with no help so I popped off. Thank you for your links. – jsolj3x Apr 28 '21 at 13:30

1 Answers1

0

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 user.example@domain.com
Tenant2 HostPool2 user.example2@domain.com
Santiago Squarzon
  • 2,552
  • 1
  • 2
  • 17
  • 1
    Oh TIL about calling a function like that. I'm sure that's a simple thing that most know but it never occurred to me to call to it that way inside of a script so that is really useful. Thanks for that and I think that works after making some super small adjustments. That example of a pscustomobject makes more sense in my brain too. Thanks for the help. – jsolj3x Apr 28 '21 at 15:11
  • @jsolj3x That's good to know, feel free to [edit my post](https://stackoverflow.com/posts/67302579/edit) with your adjustments so anyone can find it helpful in the future :) – Santiago Squarzon Apr 28 '21 at 15:20
  • What you have is correct and works great. So this is answered. Thank you again. – jsolj3x Apr 28 '21 at 15:24