有沒有更好的方法來獲取用戶資訊及其在 Powershell 中的 Graph API 中的特定 MemberOf 組的經理?我在下面寫過,它有效,但似乎不是最好的方法。我是新手,所以請放輕松!
理想情況下,我希望 Get-MgUser 中的所有欄位與用戶的 Manager 和我在 CSV 匯出結束時搜索的特定 MgUserMemberOf 組,但不確定是否可能。
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
} }
當前匯出 00000000-56fa-4638-9ff6-1dc85d3c9735 ,“顯示名稱”,“經理”,“組成員”
uj5u.com熱心網友回復:
您的代碼非常令人困惑,但我認為您正在尋找類似于以下內容的內容:
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
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/527274.html
標籤:天蓝色电源外壳微软图形 API
