Powershell:遍历多个变量的数组
Powershell: Iterate through arrays for multiple variables
我需要一些关于我的脚本的大型指导。我需要能够遍历存储 tenantNames、app_id、client_secret 的 csv 文件,并围绕它包装一个大的 ForEach 循环,以便我的脚本获取内部每个租户的所述数据CSV:
我正在努力想象 For 循环的顺序,以便能够传递 $Tenant、$customer_client_id 和 $customer_client_secret。
数组可能过多,但这是我所知道的避免格式问题等的最稳定的方法...
任何帮助或想法都会非常有帮助
$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}
}
我一时兴起,猜想这就是你想要的:
$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
}
}
}
没有真正看到你所期望的,很难理解你目前拥有的东西。希望这能让你朝着正确的方向前进!我还使用了 splatting,因为这是何时使用它的好场景。
我需要一些关于我的脚本的大型指导。我需要能够遍历存储 tenantNames、app_id、client_secret 的 csv 文件,并围绕它包装一个大的 ForEach 循环,以便我的脚本获取内部每个租户的所述数据CSV:
我正在努力想象 For 循环的顺序,以便能够传递 $Tenant、$customer_client_id 和 $customer_client_secret。
数组可能过多,但这是我所知道的避免格式问题等的最稳定的方法...
任何帮助或想法都会非常有帮助
$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}
}
我一时兴起,猜想这就是你想要的:
$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
}
}
}
没有真正看到你所期望的,很难理解你目前拥有的东西。希望这能让你朝着正确的方向前进!我还使用了 splatting,因为这是何时使用它的好场景。