Forum Discussion
PoconoNeo
May 15, 2020Copper Contributor
Create CSV & HTML showing AD Group, Group Description, ManagedBy (Friendly Name)
I need some help doing a search of AD groups with the Description starting with "Admin Group*" and get the Managedby friendly name. Some groups are managed by groups and other are by user. I've tried...
Erick A. Moreno R.
May 15, 2020Iron Contributor
PoconoNeo Hello Neo, my approach would be to use a HT and save everything to a new var and then export it to the CSV/HTML file.
$Results = @()
Get-ADGroup -filter { Description -like "Admin Group*"} -Properties CN, Description, ManagedBy | Sort-Object "CN" | ForEach-Object {
$GroupName = $_.CN;
$Description = $_.Description;
$Manager = $_.Managedby;
if (!$Manager) {
$Manager = 'N/A';
}
Get-ADUser -Filter * -SearchBase $Manager -Properties * {
$GivenName = $_.GivenName;
$Surname = $_.Surname;
$managerName = $GivenName, $Surname;
}
if (!$ManagerName) {
(Get-ADGroup -Filter * -SearchBase $Manager -Properties Name) | ForEach-Object {
$managerName = $_.Name;
}
}
#Write-Output $Groupname, $Description, $ManagerName
$Props = $null
$Props = [Ordered]@{
GroupName = $Groupname
Description = $Description
ManagerName = $ManagerName
}
$Results += New-Object PSObject -Property $Props
}
$Results | Export-Csv -Path "/Path to save csv/Report.csv" -NoTypeInformation -Encoding UTF8 -Force
$Results | ConvertTo-Html -Property GroupName,Description,ManagerName | Out-File "/Path to save html/Report.html"
Let Me know if you have further questions.
Regards
Erick Moreno
- PoconoNeoMay 15, 2020Copper Contributor
Erick A. Moreno R. Thanks for that, now that it is in correct formats, it is not outputting the groups that don't have a managedby field set, would like to display those groups as N/A.
- Erick A. Moreno R.May 15, 2020Iron Contributor
PoconoNeo This should cover that.
$Results = @() Get-ADGroup -filter { Description -like "Admin Group*"} -Properties CN, Description, ManagedBy | Sort-Object "CN" | ForEach-Object { $Manager = $GroupName = $Description = $null $GroupName = $_.CN; $Description = $_.Description; $Manager = $_.Managedby; If($Manager) { Get-ADUser -Filter * -SearchBase $Manager -Properties * { $GivenName = $_.GivenName; $Surname = $_.Surname; $ManagerName = $GivenName, $Surname; } if (!$ManagerName) { (Get-ADGroup -Filter * -SearchBase $Manager -Properties Name) | ForEach-Object { $managerName = $_.Name; } } } Else { $ManagerName = 'N/A'; } #Write-Output $Groupname, $Description, $ManagerName $Props = $null $Props = [Ordered]@{ GroupName = $Groupname Description = $Description ManagerName = $ManagerName } $Results += New-Object PSObject -Property $Props } $Results | Export-Csv -Path "/Path to save csv/Report.csv" -NoTypeInformation -Encoding UTF8 -Force $Results | ConvertTo-Html -Property GroupName,Description,ManagerName | Out-File "/Path to save html/Report.html"