I have a problem trying to "fix" a PS script I got from a collegue to get me information from a customers AD. I'm trying to get a list of all distrobution mailboxes and members of these. But the script he gave me doesn't work, I've tried to change some but since I'm still learning PowerShell I'm finding myself stuck and really not understanding what his script is trying to do.
Groups = Get-ADGroup -Filter * -SearchBase OU=costumer,OU=companies,DC=domene,DC=oss"
$Results = foreach( $Group in $Groups ){
Get-ADGroupMember -Identity $Group | foreach {
[pscustomobject]@{
GroupName = $Group.Name
Name = $_.Name
}
}
}
$Results| Export-Csv -Path c:\data\groups.csv -NoTypeInformation? -Encoding
unicode
When I run this script I get an error from the very begining because of "Groups =" where Groups is not recognised.
So I have changed the start to:
Get-ADGroup -filter * -Searchbase "OU=Distribution,OU=Groups,OU=Costumer,OU=Companies,DC=domene,DC=oss"
This is the full path to the AD objects I want, and I have tested this commando and get the full list. But I don't get the information about members of the goupobjects. I've tried change on the bottom part of the script, but get an error returned on "-identity" all the time, it dosn't matter what I write after it.
Can someone help med with a better code to get out the information I need? And maybe explain what this script is trying to do?
Thank you very much for your help.
EDIT 1
Thank you for the help! I now get the script to run and I get the .cvs file. But I'm wondering if I have something else wrong. It should return the groups in AD and members of the groups, but the result I get is not this. I've attached screenshot from when I open this in excel.
Is there something else missing in my script to get the information?
Solution
I found a way changing the script to make it get what I needed, and it worked. It might to a little more than only getting the information that I need, but I got the result that I needed. :) Thank you!
$GruppeMedlemmer = @()
$Groups = Get-ADGroup -Filter * -properties * -SearchBase "ou=felles,OU=Groups,OU=costumer,OU=Companies,DC=domene,DC=oss"
foreach ($g in $Groups) {
$members = $g | Get-ADGroupMember
foreach ($m in $members) {
$Info = New-Object psObject
$Info | add-member -MemberType NoteProperty -Name "GroupName" -Value $g.Name
$Info | add-member -MemberType NoteProperty -Name "Description" -Value $g.description
$Info | add-member -MemberType NoteProperty -Name "Member of" -Value $g.MemberOf
$Info | Add-Member -MemberType NoteProperty -Name "Member" -Value $m.name
$GruppeMedlemmer+= $Info
}
}
$GruppeMedlemmer | Sort-Object GroupName | Export-CSV C:\temp\groupdist.csv -notype -Encoding unicode