0

I'm working on a PowerShell script that creates a CSV file with Active Directory Organizational Unit, its Groups and its Members.

So far so good, I got the .csv

Now I want to include another column in the CSV that indicates whether the Member name contains a specific string text.

For example, member name:

Barack Obama (admin) Donald Trump (user) Richard Nixon 

Now I'd like to have another column that indicates "True" for all administrators based on the string piece "(admin)". If it contains "(admin)" it will say True. Else, it will say False.

This is how far I got but I'm a total newbie with PowerShell.

 $OU = 'OU=EMEA,OU=Admins,OU=XXXXX,OU=Admin,DC=emea,DC=xxxx,DC=xxxx' $Path = 'I:\Users\xxxx.xxx\Desktop\output.csv' $Groups = Get-ADGroup -Filter * -SearchBase $OU $Data = foreach ($Group in $Groups) { Get-ADGroupMember -Identity $Group -Recursive | Select-Object @{Name='Organizational Unit';Expression={$OU}}, @{Name='Group';Expression={$Group.Name}}, @{Name='Member';Expression={$_.Name}} --- // now I need some IF statement I guess \\ --- If ( $_.Name -contains '(admin)') { Write-Host "True" } Else { write-host "False" } } } 
0

2 Answers 2

2

You can use Add-Member to add new members to existing objects:

$_ | Add-Member -MemberType NoteProperty -Name IsAdmin -Value ($_.Name -contains '(admin)') 

or the shorter variant:

$_ | Add-Member IsAdmin ($_.Name -contains '(admin)') 
Sign up to request clarification or add additional context in comments.

Comments

0

I would simply add another calculated property:

Get-ADGroupMember -Identity $Group -Recursive | Select-Object @{Name='Organizational Unit'; Expression={$OU}}, @{Name='Group'; Expression={$Group.Name}}, @{Name='Member'; Expression={$_.Name}}, @{Name='IsAdmin'; Expression={$_.Name -like '*(admin)*'}} 

P.S. -contains is an operator for testing if an array contains an element of a certain value. This is NOT the same as the Strings own .Contains() method with which you search if a value is part of a string.. In this case the most appropriate operator to use is -like.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.