I needed to export AD Security groups and their memberships, so made a new script based on previous script. I used table variable to sort the export data.
<#.Synopsis
This scripts exports Security Groups and member information from your active directory OU to CSV file
Selected information: Group Name, Info, Description, Username, distinguishedname.
.Description
Writen by Miikka Kallberg
.Parameter FilePath
Path to save the ouput CSV File
.Parameter csvfile
Filename of the output CSV File name
.Parameter OU
Organization Unit where you want to get the user information
.Example
.\groupinfo.ps1 -FilePath c:\temp\ -csvfile marvin.csv -OU Groups
This will export Security Group information from Groups OU to c:\temp\marvin.csv
#>
Param([String]
[Parameter(Mandatory=$true)]
$FilePath='',
[Parameter(Mandatory=$true)]
$csvfile='',
[Parameter(Mandatory=$true)]
$OU='')
#Clear output variables to avoid multiple entries
if ( $FilteredOutput) {
Remove-Variable FilteredOutput}
if ( $Output) {
Remove-Variable Output}
# Import active directory module for running AD cmdlets
Import-Module activedirectory
#Get domain name
$domainname= get-addomain | select distinguishedname
#Filter unneeded information from variable
foreach ($DN in $Domainname)
{
$domain = $Domainname.distinguishedname
}
# Create searchpath combining OU and domain name in correct format
$Oubase="OU=$OU,$Domain"
#Get AD Groups
[array]$Output += Get-ADgroup -filter * -properties CN,Description,Info,distinguishedname -SearchBase "$Oubase"
#Create table in variables
$Table = @()
$Record = @{
"Group Name" = ""
"Username" = ""
"Description" = ""
"Info" = ""
"distinguishedname" = ""
}
#Sort data and add it to the table
Foreach ($Group in $output)
{
$Arrayofmembers = Get-ADGroupMember -identity $Group
foreach ($Member in $Arrayofmembers)
{
$Record."Group Name" = $Group.CN
$Record."UserName" = $Member.samaccountname
$Record."Description" = $Group.Description
$Record."Info" = $Group.Info
$Record."distinguishedname" = $Member.distinguishedname
$objRecord = New-Object PSObject -property $Record
$Table += $objrecord
}
}
#Export table to the table
$Table | export-csv (Join-Path $filepath $csvfile) -NoTypeInformation
No comments:
Post a Comment