Home > Mobile >  Powershell: Iterate through arrays for multiple variables
Powershell: Iterate through arrays for multiple variables

Time:03-18

I need some mega guidance on my script below. I need to be able to iterate through a csv file that stores tenantNames, app_id, client_secret for my script and wrap a big ForEach loop around it in order for my script to get said data for each tenant inside the CSV:

enter image description here

I'm struggling to visualize the order of the For loops to be able to pass $Tenant, $customer_client_id and $customer_client_secret.

Arrays might be excessive, but it's the most stable way I know to avoid formatting issues etc...

Any assistance or ideas would be super helpful

$master_file = 'C:\temp\apps.csv'
$array_tenant = @()
$array_customer_client_id = @()
$array_customer_client_secret = @()

Import-Csv $master_file | ForEach-Object {
$array_tenant  = $_.tenant
$array_customer_client_id  = $_.app_id
$array_customer_client_secret  = $_.cs
}

$Tenant = ''
$customer_client_id = ''
$customer_client_secret = ''

$Body = @{    
    Grant_Type    = "client_credentials"
    Scope         = "https://graph.microsoft.com/.default"
    client_Id     = $customer_client_id
    Client_Secret = $customer_client_secret
    } 
$ConnectGraph = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$Tenant/oauth2/v2.0/token" -Method POST -Body $Body
$Token = $ConnectGraph.access_token
$file = "C:\temp\$Tenant._users_with_licenses.csv"
$final_results = "C:\temp\$Tenant._results.csv"

$users_array = @()
$user_list = 'https://graph.microsoft.com/beta/users'
$users = Invoke-RestMethod -Headers @{Authorization = "Bearer $($Token)"} -ContentType 'application/json' -Uri $user_list -Method 'GET'
$users.value | Where-Object {$_.assignedLicenses -ne "null"} | Select-Object userPrincipalName | Export-Csv $file -NoTypeInformation

Import-Csv $file | ForEach-Object {
    $users_array  = $_.userPrincipalName
}
foreach ($item in $users_array) {
    $auth_methods = "https://graph.microsoft.com/v1.0/users/$item/authentication/methods"
    $get_auth_methods = Invoke-RestMethod -Headers @{Authorization = "Bearer $($Token)"} -ContentType 'application/json' -Uri $auth_methods -Method 'GET'
    if (!$get_auth_methods.value) {$get_auth_methods | Export-Csv $final_results -Append -NoTypeInformation} 
}

CodePudding user response:

I am going on a whim here and guessing this is what you're after:

$masterFile = 'C:\temp\apps.csv'
Import-Csv -Path $masterFile | 
    ForEach-Object -Process {
        $tenant  = $_.tenant
        $request = @{
            Uri    = "https://login.microsoftonline.com/$Tenant/oauth2/v2.0/token"
            Method = "POST"
            Body   = @{
                Grant_Type    = "client_credentials"
                Scope         = "https://graph.microsoft.com/.default"
                client_Id     = $_.app_id
                Client_Secret = $_.cs
            } 
        }

        $connectGraph = Invoke-RestMethod @request
        $token = $connectGraph.access_token
        $filePath = "C:\temp\$Tenant._users_with_licenses.csv"
        $finalResults = "C:\temp\$Tenant._results.csv"
        $userRequest = @{
            Uri     = 'https://graph.microsoft.com/beta/users'
            Method  = "GET"
            Headers = @{
                Authorization = "Bearer $token"
                ContentType   = "application/json"
            }
        }

        $usersGet = Invoke-RestMethod @userRequest
        $users = $users.value | Where-Object -Property "assignedLicenses" -NE "null" | Select-Object -ExpandProperty "userPrincipalName"
        $users | Export-Csv -Path $filePath -NoTypeInformation -Force 
        foreach ($user in $users)
        {
            $finalRequest = @{
                Uri = "https://graph.microsoft.com/v1.0/users/$user/authentication/methods"
                ContentType = "application/json"
                Method = "GET"
                Headers = @{
                    Authorization = "Bearer $Token"
                }
            }

            $getAuthMethod = Invoke-RestMethod @finalRequest
            if (-not$getAuthMethod) {
                $getAuthMethod | Export-Csv -Path $finalResults -Append -NoTypeInformation
            }
        }        
    }

Without really seeing what you expect, quite hard to understand what you currently have. Hopefully this gets you in the right direction! I also made use of splatting as this is a good scenario on when to use it.

  • Related