bsharath
asked on
Powershell script that can query a OU and get all group name,Members and count of members into a csv/Excel.
Hi,
Powershell script that can query a OU and get all group name,Members and count of members into a csv/Excel.
Only Distribution groups.
Can anyone help me with a code that can do this.
REgards
Sharath
Powershell script that can query a OU and get all group name,Members and count of members into a csv/Excel.
Only Distribution groups.
Can anyone help me with a code that can do this.
REgards
Sharath
ASKER
thanks but how can i get the counts of each group members
try this:
$LDAP = "LDAP://OU=myOU,DC=myDomai n,DC=MySuf fx"
$RootPath = [ADSI] $LDAP
$groups = $RootPath.PSBase.Get_Child ren() | Where-Object {$_.Class -eq "Group"}
foreach ($group in $Groups){Write-host $group.Name"`,"$group.memb er.count"` ,"$group.m ember}
$LDAP = "LDAP://OU=myOU,DC=myDomai
$RootPath = [ADSI] $LDAP
$groups = $RootPath.PSBase.Get_Child
foreach ($group in $Groups){Write-host $group.Name"`,"$group.memb
ASKER
X-men is this powershell
yes
here is the entire script (powershell)
Edit Row1 with your LDAP path
the result will be writen in the file "c:\ee.csv" (line 16)
Edit Row1 with your LDAP path
the result will be writen in the file "c:\ee.csv" (line 16)
$LDAP = "LDAP://OU=myOU,DC=MyDomain,DC=org"
$RootPath = [ADSI] $LDAP
$groups = $RootPath.PSBase.Get_Children() | Where-Object {$_.Class -eq "Group"}
$xTable = @()
foreach ($group in $Groups)
{$o="" | select GName,GMembers,GCount;
$o.GName=$group.Name.ToString();
$o.GMembers=$group.member.ToString();
$o.GCount=$group.member.count;
$xTable += $o}
If ($xTable.Length -ne 0)
{foreach ($x in $xTable)
{$xGName=$x.GName;
$xGMembers=$x.GMembers;
$xGCount=$x.GCount
Add-Content C:\ee.csv -Value "$xGName`,$xGMembers`,$xGCount"}
}
ASKER
Thanks
I get each portion in each cell and its difficult to read or sort
OU=Named Users OU=Users
Can i get just these
groupname ,Member1, Count
Member2
Member3
Nested group name
Member1
Member2
Member3
The count should not include any nested group but should include members within the nested group
I get each portion in each cell and its difficult to read or sort
OU=Named Users OU=Users
Can i get just these
groupname ,Member1, Count
Member2
Member3
Nested group name
Member1
Member2
Member3
The count should not include any nested group but should include members within the nested group
If you add a string delimiter on line 16,
EX: delimiter: "|": Add-Content C:\ee.csv -Value "$xGName`,`|$xGMembers`|`, $xGCount"}
Wen you run the "Text to columns" in excel, place the "|" char as the string delimiter and the "," as the column delimiter
EX: delimiter: "|": Add-Content C:\ee.csv -Value "$xGName`,`|$xGMembers`|`,
Wen you run the "Text to columns" in excel, place the "|" char as the string delimiter and the "," as the column delimiter
ASKER
Thanks but still confusing
Can you remove all CN=,OU=,DN= all those paths and have just names
Can you remove all CN=,OU=,DN= all those paths and have just names
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I get this
You cannot call a method on a null-valued expression.
At line:3 char:34
+ $o.GName=$group.Name.ToStr ing <<<< ();
+ CategoryInfo : InvalidOperation: (ToString:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At line:4 char:53
+ $o.GMembers=$Group.member | foreach {$_.tostring <<<< ().substring(3,$_.tostring ().IndexOf (",")-3)};
+ CategoryInfo : InvalidOperation: (tostring:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At line:3 char:34
+ $o.GName=$group.Name.ToStr
+ CategoryInfo : InvalidOperation: (ToString:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At line:4 char:53
+ $o.GMembers=$Group.member | foreach {$_.tostring <<<< ().substring(3,$_.tostring
+ CategoryInfo : InvalidOperation: (tostring:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
have you altered line 1 with the correct LDAP path ?
Are there any AD objects of the class "Group" in the LDAP path you provided?
ASKER
Yes path is fine and it has the groups as well
well, the mesage
null-valued expression.
At line:3 char:34
+ $o.GName=$group.Name
tells me that there are no objects of class 'Group' in the LDAP Path provided at line 1. so you might want to double check it. The machine is always right you know...
null-valued expression.
At line:3 char:34
+ $o.GName=$group.Name
tells me that there are no objects of class 'Group' in the LDAP Path provided at line 1. so you might want to double check it. The machine is always right you know...
ASKER
Sorry for the delay will check and get back
This should help you do exactly what you want to do, and more!
Lee