I make use of a script that grabs licensing information which works fine. I wanted to grab group information into this script so I can see what groups a person is in while also getting other information on them, the important part of the script looks as the follows
$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
}
}
}
This works without issues but obviously, it isn't possible to grab group information from Get-MsolUser
which led me to make use of Get-MsolGroup
to keep in use of MSonline module. As such I've added the following:
$Groups1 = foreach ($Group in (Get-MsolGroup -all)){
if (Get-MsolGroupMember -all -GroupObjectId $Group.ObjectId | where {$_.Emailaddress -eq $i.UserPrincipalName}) {$Group.Displayname}
}
$groups2 = $Groups1 -join ", "
in combination, I've added this to be fed into the new-object area with Groups = $groups2
What I'm running into here is a massive stall where nothing is actually being done, when testing this via the debugger I can see that $groups1/2 is grabbing and outputting as I'd expect but it seems to stall at the line I've added.
What I'm expecting to happen is it to complete so that when I select-object against these new objects the groups field will be populated with $groups2 data, this is not happening.
What exactly could be causing this behavior?
CodePudding user response:
Continuing from my comment, I'd gather the groups and members first, then just reference that variable in your user loop.
$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 ', '
}
}
}