Home > OS >  Is there a better way to get users information and their manager for a specific MemberOf Group in Gr
Is there a better way to get users information and their manager for a specific MemberOf Group in Gr

Time:11-03

Is there a better way to get users' information and their manager for a specific MemberOf Group in Graph API in Powershell? I have written below, it works but doesn't seem to be the best way to do this. I am new to this so please take it easy on me!

Ideally, I would like ALL fields from the Get-MgUser with the user's Manager and the specific MgUserMemberOf group I am searching for at the end of the CSV export, but not sure if it is possible.

            if (Get-InstalledModule Microsoft.Graph) {   
        # Connect to MS Graph    $appid = 'BLAH' $tenantid = 'BLAH' $secret = 'BLAH'
            
          $body =  @{
            Grant_Type    = "client_credentials"
            Scope         = "https://graph.microsoft.com/.default"
            Client_Id     = $appid
            Client_Secret = $secret }   $connection = Invoke-RestMethod `
            -Uri https://login.microsoftonline.com/$tenantid/oauth2/v2.0/token `
            -Method POST `
            -Body $body   $token = $connection.access_token   Connect-MgGraph -AccessToken $token
        
        
        ### Comment out below to use the production version of Azure AD
        
        Select-MgProfile -Name "beta"
        
        
    $users = Get-MgUser  -Filter "startsWith(DisplayName, 'Joe Bloggs')" foreach($Id in $users)
{ 

    $MemberOf = Get-MgUserMemberOf -UserId $CurrentID | Where {$_.AdditionalProperties['displayName'] -like "*VIP*"} | Select id, @{E={$_.additionalProperties['displayName']}} 

    
    $UserManager = Get-MgUserManager -UserId $CurrentID | Select id, @{E={$_.additionalProperties['displayName']}} 
    
    $Result =  "$($users.Id) , ""$($users.DisplayName)"", ""$($UserManager.'$_.additionalProperties[''displayName'']')"", ""$($MemberOf.'$_.additionalProperties[''displayName'']')"""  
    
    write-host $Result 
    
    Add-Content "C:\Temp\Result.csv" $Result 
              } }

Current Export 00000000-56fa-4638-9ff6-1dc85d3c9735 , "DISPLAY NAME", "MANAGER", "Member Of GROUP"

CodePudding user response:

Your code is very confusing but I think what you're looking for is something similar to this:

if (Get-InstalledModule Microsoft.Graph) {
    $params = @{
        Uri    = "https://login.microsoftonline.com/$tenantid/oauth2/v2.0/tokenMethod"
        Method = 'POST'
        Body = @{
            Grant_Type    = "client_credentials"
            Scope         = "https://graph.microsoft.com/.default"
            Client_Id     = $appid
            Client_Secret = $secret
        }
    }
    $connection = Invoke-RestMethod @params
    Connect-MgGraph -AccessToken $connection.access_token
    Select-MgProfile -Name "beta"

    Get-MgUser -Filter "startsWith(DisplayName, 'Joe Bloggs')" | ForEach-Object {
        [pscustomobject]@{
            Id          = $_.Id
            DisplayName = $_.DisplayName
            Manager     = (Get-MgUserManager -UserId $_).additionalProperties['displayName']
            MemberOf    = (Get-MgUserMemberOf -UserId $_).Where{ $_.AdditionalProperties['displayName'] -like "*VIP*" }.additionalProperties['displayName']
        }
    } | Export-Csv "C:\Temp\Result.csv" -NoTypeInformation
}
  • Related