我使用一個腳本來獲取運行良好的許可資訊。我想將組資訊抓取到這個腳本中,這樣我就可以看到一個人在哪個組中,同時還可以獲取有關他們的其他資訊,腳本的重要部分如下所示
$result = foreach($domain in $domains){
$users = Get-MsolUser -all | where {$_.islicensed -eq "$true" -and $_.Userprincipalname -match "$domain"}
foreach ($i in $users){
$licenses = $i.Licenses
$licenseArray = $licenses | foreach-Object {$_.AccountSkuId}
$licensear = Friendlyname
$licenseString = $licenseAr -join ", "
New-Object PSObject -Property @{
Displayname = $i.Displayname
Userprincipalname = $i.userprincipalname
UPN = ($i.userprincipalname -Split '@')[1]
Licenses = $licenseString
}
}
}
這沒有問題,但顯然,不可能從中獲取組資訊,Get-MsolUser從而使我Get-MsolGroup繼續使用 MSonline 模塊。因此,我添加了以下內容:
$Groups1 = foreach ($Group in (Get-MsolGroup -all)){
if (Get-MsolGroupMember -all -GroupObjectId $Group.ObjectId | where {$_.Emailaddress -eq $i.UserPrincipalName}) {$Group.Displayname}
}
$groups2 = $Groups1 -join ", "
結合起來,我已將其添加到新物件區域中 Groups = $groups2
我在這里遇到的是一個巨大的停頓,實際上什么也沒做,當通過除錯器測驗這個時,我可以看到 $groups1/2 正在按照我的預期抓取和輸出,但它似乎停在了我的行上'已經添加。
我期望發生的是它完成以便當我針對這些新物件選擇物件時,組欄位將填充 $groups2 資料,這不會發生。
究竟是什么導致了這種行為?
uj5u.com熱心網友回復:
繼續我的評論,我會先收集組和成員,然后在您的用戶回圈中參考該變數。
$groups = Get-MsolGroup -All
$groupsandmembers = $groups | ForEach-Object {
[PSCustomObject]@{
GroupName = $_.displayname
Members = Get-MsolGroupMember -All -GroupObjectId $_.objectid |
Select-Object -ExpandProperty emailaddress
}
}
$result = foreach($domain in $domains){
$users = Get-MsolUser -all | Where-Object {
$_.islicensed -eq $true -and
$_.Userprincipalname -match $domain
}
foreach ($i in $users){
$licenses = $i.Licenses
$licenseArray = $licenses.AccountSkuId
$licensear = Friendlyname
$licenseString = $licenseAr -join ", "
[PSCustomObject]@{
Displayname = $i.Displayname
Userprincipalname = $i.userprincipalname
UPN = ($i.userprincipalname -Split '@')[1]
Licenses = $licenseString
Groups = ($groupsandmembers | Where-Object members -contains $i.userprincipalname).groupname -join ', '
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/345229.html
上一篇:獲取DNS腳本在報告中缺少輸出
下一篇:命令列倒數計時器跳過一秒
