gpt4 book ai didi

azure - 有没有更好的方法来获取 Graph API Powershell 中特定 MemberOf 组的用户信息及其经理

转载 作者:行者123 更新时间:2023-12-03 00:54:03 26 4
gpt4 key购买 nike

是否有更好的方法可以在 Powershell 的 Graph API 中获取特定 MemberOf 组的用户信息及其经理?我在下面写了,它有效,但似乎不是最好的方法。我是新手,所以请放轻松!

理想情况下,我想要 Get-MgUser 中的所有字段以及用户的经理和我在 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-Object { $_.AdditionalProperties['displayName'] -like '*VIP*' } | Select-Object id, @{E = { $_.additionalProperties['displayName'] } }
$UserManager = Get-MgUserManager -UserId $CurrentID | Select-Object 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 , "DISPLAY NAME", "MANAGER", "Member Of GROUP"

最佳答案

你的代码非常困惑,但我认为你正在寻找的是与此类似的东西。确保您应该手动构建 CSV,您可以创建对象并将它们通过管道传递到 Export-Csv 来为您解析它们。在这两种情况下,您都可以使用 -ExpandProperty,而不是分别调用 Get-MgUserManagerGet-MgUserMemberOf

if (Get-Module Microsoft.Graph -ListAvailable) {
$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'

$getMgUserSplat = @{
Filter = "startsWith(DisplayName, 'Joe Bloggs')"
ExpandProperty = 'manager', 'memberOf'
}

Get-MgUser @getMgUserSplat | ForEach-Object {
[pscustomobject]@{
Id = $_.Id
DisplayName = $_.DisplayName
Manager = $_.Manager.AdditionalProperties.displayName
MemberOf = $_.memberOf.AdditionalProperties.displayName -like '*VIP*'
}
} | Export-Csv 'C:\Temp\Result.csv' -NoTypeInformation
}

关于azure - 有没有更好的方法来获取 Graph API Powershell 中特定 MemberOf 组的用户信息及其经理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74296145/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com