Jump to content

Recommended Posts

Posted

Hi there

 

I've got a small problem with my script below.

 

It used to lookup ad users from firstname and surname along with a username prefix and returns the UPN and group membership

It does what it works fine for a single user, however if used for multiple and they have a different amount of groups it only shows some. in Grid-view or export-csv, however if I call the variable $obj it has all the groups in

 

Thanks

 

 

 


$results = @()
$site = "U1" #User prefix

foreach ($a in $b)
{
$item = New-Object PSObject
$item | Add-Member -type NoteProperty -Name 'GivenName' -Value $a.Split("+")[0] 
$item | Add-Member -type NoteProperty -Name 'Surname' -Value $a.Split("+")[1]

$results += $item
}

$Output = @()

foreach ($user in $results) {
   $firstname = $user.GivenName
   $lastname = $user.Surname
   $aduser = Get-ADUser -Filter 'GivenName -eq $firstname -and sn -eq $lastname' -properties EmailAddress | where { $_.samaccountname -match $Site} | select GivenName, Surname, UserPrincipalName, EmailAddress
   $id = $aduser.UserPrincipalName.split("@")[0]
   $user = get-aduser $id -properties samaccountname,givenname,surname
   $groups = Get-ADPrincipalGroupMembership $id | select -expand name 
	

$obj = New-Object PSObject
$obj | Add-Member -type NoteProperty -Name 'Username' -Value $user.samaccountname 
$obj | Add-Member -type NoteProperty -Name 'Firstname' -Value $user.givenname
$obj | Add-Member -type NoteProperty -Name 'Surname' -Value $user.surname


$int = 0 
Foreach ($e in $groups) {

if ($int -eq $groups.Count) {break}


$obj | Add-Member -type NoteProperty -Name Group$int -Value $e
$int ++

}
#}
$Output += $obj
                           }
$export = $Output | Sort-Object UserPrincipalName
$export |  Out-GridView -PassThru | export-csv "d:\test\exuser.csv" -NoTypeInformation


Posted

This is because you are creating an unspecified number of group properties for each $obj which may result in each one having a different amount of properties. When you try to output multiple objects with differing properties it will get those properties from the first object it sees and only show those for the rest of the objects. A simple example showing this:

 

PS> $obj1 = [PSCustomobject]@{'group1'='test'}
PS> $obj2 = [PSCustomobject]@{'group1'='test';'group2'='test2'}

PS> $obj1,$obj2

group1
------
test
test

PS> $obj2,$obj1

group1 group2
------ ------
test   test2
test

 

Basically you need to know all of the groups that will appear in the objects upfront to display this correctly which isn't ideal.

Posted

Hi, Thanks for that. I guess that makes sense, I find it just strange that the variable has the data in but just doesn't display it.

 

Can you recommend a different way of doing it?

 

Thanks

Posted
It depends really what you intend to do with the output. Is the output just for a human-readable report or is it something you want to feed in to another tool/cmdlet? Does it have to be in tabular format with all the different group names on each column?
Posted
It depends really what you intend to do with the output. Is the output just for a human-readable report or is it something you want to feed in to another tool/cmdlet? Does it have to be in tabular format with all the different group names on each column?

 

It was really just to read to check membership, I intended to grid view for most of the time but with a passthur to be able to select users and export to csv occasionally

 

Thanks

Posted
It depends really what you intend to do with the output. Is the output just for a human-readable report or is it something you want to feed in to another tool/cmdlet? Does it have to be in tabular format with all the different group names on each column?

 

Hi just wondering if you had an idea for a method i could use?

 

Thanks

Nick

Posted
If you'd like to keep it in your format then you could feed all of the property names to Select-Objects property parameter which will then show them in the CSV even if the objects didn't contain said properties. You could build this property list by keeping track of the largest group number and then creating it at the end after looping through all the users.
Posted
If you'd like to keep it in your format then you could feed all of the property names to Select-Objects property parameter which will then show them in the CSV even if the objects didn't contain said properties. You could build this property list by keeping track of the largest group number and then creating it at the end after looping through all the users.

 

Thanks for that

 

can you give me a quick example please?

 

Thanks

  • 2 weeks later...
Posted

Sure, you can add a check in your foreach ($user in $results) loop that determines which user has the largest amount of groups:

 

if ($groups.count -gt $maxgroups) {$maxgroups = $groups.count}

 

and then at the end you can build a list of all the properties because you know what the largest number of groups was:

 

$props = 'Username','Firstname','Surname' + (0..($maxGroups-1) | ForEach-Object {'Group{0}' -f $_})
$export = $Output | Sort-Object UserName | Select -Property $props
$export | Out-GridView -PassThru | export-csv "d:\test\exuser.csv" -NoTypeInformation

Posted (edited)

Thanks for that, it works.

 

However when i look at the output the groups don't align. I understand that is the way i've created it.

 

So was looking at changing it.

 

so i've create a foreach loop that collects all the groupnames and then trying to add yes or no depending.

 


#Collect group names#######

$Allgroup = @()

foreach ($user in $results) {
$firstname = $user.GivenName
$lastname = $user.Surname
$aduser = Get-ADUser -Filter 'GivenName -eq $firstname -and sn -eq $lastname' -properties EmailAddress | where { $_.samaccountname -match $Site} | select GivenName, Surname, UserPrincipalName, EmailAddress
$id = $aduser.UserPrincipalName.split("@")[0]
$user = get-aduser $id -properties samaccountname,givenname,surname
$groups = Get-ADPrincipalGroupMembership $id | select -expand name
$Allgroup += $groups}

#############################

$Output = @()
$groups = ""
foreach ($user in $results) {
$firstname = $user.GivenName
$lastname = $user.Surname
$aduser = Get-ADUser -Filter 'GivenName -eq $firstname -and sn -eq $lastname' -properties EmailAddress | where { $_.samaccountname -match $Site} | select GivenName, Surname, UserPrincipalName, EmailAddress
$id = $aduser.UserPrincipalName.split("@")[0]
$user = get-aduser $id -properties samaccountname,givenname,surname
$groups = Get-ADPrincipalGroupMembership $id | select -expand name


$obj = New-Object PSObject
$obj | Add-Member -type NoteProperty -Name 'Username' -Value $user.samaccountname
$obj | Add-Member -type NoteProperty -Name 'Firstname' -Value $user.givenname
$obj | Add-Member -type NoteProperty -Name 'Surname' -Value $user.surname



Foreach ($1 in $Allgroup) {if ($1 -in $groups){$obj | Add-Member -type NoteProperty -Name $1 -Value "Yes"
}Else {$obj | Add-Member -type NoteProperty -Name $1 -Value "No"}






}
$Output += $obj
}

[/Code]

 

However I must be missing something as the first group that a user isn't a member of causes the whole list to have "No" and not sure why but if i run $output the "No"'s don't show

 

Thanks

Edited by tri_94
Posted

It's probably because your $Allgroup array has multiples of the same group name which is then causing errors when you are building the objects because each property name must be unique. Try to condense $Allgroup down after building it to only contain the unique group names:

 

$Allgroup = $Allgroup | Select -Unique | Sort

  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...