Home > Blockchain >  Powershell, Tying 2 variables together in one script
Powershell, Tying 2 variables together in one script

Time:10-11

I have the following script. It is getting me a system array. I need to tie the $id variable and the $upn variable together with each iteration. I am at a loss on how to get the loop to continue through and tie the 2 variables together. Any help would be appreciated.

$adroles = get-mgdirectoryrole | select displayname,Id
$upn = foreach ($id in $adroles.id) { 
    Get-mgdirectoryrolemember -DirectoryRoleId $id | 
        select-object -expandproperty additionalproperties |
            foreach-object -membername userPrincipalName
}
}

CodePudding user response:

I believe you're looking to merge the output from Get-MgDirectoryRole with the output from Get-MgDirectoryRoleMember, if that's the case this is how you can do it:

Get-MgDirectoRyrole | ForEach-Object {
    foreach($upn in (Get-MgDirectoryRoleMember -DirectoryRoleId $_.Id).additionalProperties.userPrincipalName) {
        [pscustomobject]@{
            DisplayName       = $_.DisplayName
            Id                = $_.Id
            UserPrincipalName = $upn
        }
    }
}
  • Related