# 1. Export Groupmembership
# Export selected Groups to one .CSV File
# PowerShell 2 and above
# Exports all Groups into one CSV File
#$s=New-PSSession -ComputerName srvsbs2011 -Credential (Get-Credential)
#Import-PSSession -Session $s -Module ActiveDirectory
# Get Date for Path
$date=get-date -Format dd.MM.yyyy
# Define Path for CSV
$path=“D:\Reports\ActiveDirectory\$date“
# Check if Path exists – if not the Directory will be created automatically
if (!(Test-Path -Path $path)) {
New-Item $path -ItemType directory
}
# Get Groups, Filter – Securitygroups, Groups which contain members, Groups that are not Local on the Server
$groups=Get-ADGroup -Filter „GroupScope -ne ‚DomainLocal‘ -AND Member -like ‚*'“ -Properties Member
# Get Member foreach Group, the result of the whole command will be saved into the variable $object
$object=foreach ($group in $groups) {
Write-Host -ForegroundColor Cyan „$($group.Name) wird exportiert…“
Start-Sleep -Seconds 2
# Get Member foreach Group and Save them into the variable $data
$data=$group.Member | % { Get-ADObject -Filter „DistinguishedName -like ‚$_'“ -Properties SamAccountname,Description |
Select Name,SamAccountName,Description,ObjectClass
}
# Create Hashtable
foreach ($d in $data ) {
$prop =@{
Group = $group.Name
GroupCategory = $group.GroupCategory
User = $d.SamAccountName
Description= $d.Description
ObjectClass = $d.ObjectClass
}
# Create object
New-Object-TypeName PSCustomObject -Property $prop
}
}
# Export Data to CSV File
$object | Select Group,GroupCategory,User,ObjectClass,Description | Export-Csv $path\GroupMembership.csv -Encoding Unicode -NoTypeInformation
# 2. Export all Users
$csvpathusers=“$path\ADUsers.csv“
$adusers = get-aduser -filter * -Properties * | select Name,GivenName,Surname,DisplayName,SamAccountName,Mail,@{n=’Exchange DB‘;e={(($_.HomeMDB).split(‚,‘)[0]).split(‚=‘)[1]}},@{n=’ExchangeServer‘;e={($_.msExchHomeServerName -split ‚Servers/cn=‘)[1]}},UserPrincipalName,Enabled,LastLogonDate,AccountLockoutTime,LockedOut,createTimeStamp,ProfilePath,DistinguishedName,Company,Department,Title,StreetAddress,State,Postalcode,City,Country,fax,info,OfficePhone,mobile
$adusers | Export-Csv $csvpathusers -Encoding Unicode -NoTypeInformation
# 3. Export all AD Groups
$csvpathgroups=“$path\ADGroups.csv“
# Get Groups, Filter – Securitygroups, Groups which contain members, Groups that are not Local on the Server
$groups=Get-ADGroup -Filter „GroupScope -ne ‚DomainLocal‘ -AND Member -like ‚*'“ -Properties CreateTimeStamp
$groups | Select Name,SamAccountName,CreateTimeStamp,GroupCategory,GroupScope,@{N=“Path“;e={$_.DistinguishedName -split („,“) | select -Skip 1}} | Export-Csv $csvpathgroups -Encoding Unicode -NoTypeInformation
# 4. Export AD DomainPasswordPolicy
$adpwpolicypath=“$path\AD_PWPolicy.csv“
$adpwpolicy=Get-ADDefaultDomainPasswordPolicy | select ComplexityEnabled,MaxPasswordAge,MinPasswordAge,MinPasswordLength,LockoutThreshold,LockoutDuration,LockoutObservationWindow,PasswordHistoryCount
$adpwpolicy | Export-Csv -Path $adpwpolicypath -Encoding Unicode -NoTypeInformation
# 5. Export AD DOmaincontroller Information
$addomaincontrollerpath=“$path\AD_DomainController.csv“
$addomaincontroller=Get-ADDomainController | select HostName,Forest,Enabled,Domain,DefaultPartition,IPv4Address,isGlobalCatalog,ISReadOnly,LdapPort,OperatingSystem,OperatingSystemServicePack,@{n=“Roles“;E={$_.OperationMasterRoles -as [string]}},Site
$addomaincontroller | Export-Csv -Path $addomaincontrollerpath -Encoding unicode -NoTypeInformation
# 6. Domain Infos
$addomainpath=“$path\AD_Domain.csv“
$addomain=Get-ADDomain | select DNSRoot,DomainMode,Forest
$addomain | Export-Csv -Path $addomainpath -Encoding unicode -NoTypeInformation
# 7. AD ComputerInfo
$adcomputerpath=“$path\AD_Computer.csv“
$adcomputers=Get-ADComputer -Filter * -Properties OperatingSystem,LastLogonDate,createTimeStamp,Description | select Name,DistinguishedName,Description,Enabled,OperatingSystem,LastLogonDate,createTimeStamp
$adcomputers | Export-Csv -Path $adcomputerpath -Encoding Unicode -NoTypeInformation
## 8. Get empty ADGroups
$empty= Get-ADGroup -Filter „Member -notlike ‚*'“ | select -ExpandProperty Name
if ($empty) {
$emptypath = „$path\ADGroups_Empty“
$empty | Export-Csv -Path $emptypath -Encoding Unicode -NoTypeInformation
}
##### Covert all CSV Files to XSLX
convertcsvto-Excel -csvpath $path -excelpath „$path\$date\AD_Info.xlsx“
$mailServer=“mail.swisscomdata.ch“
$recipients=“antoine.hauck@ckw.ch“
$zippath= „$(Get-Item -Path $path | select -ExpandProperty FullName)\Archive.zip“
Compress-Archive -Path $path -DestinationPath $zippath -Force -Verbose
Send-MailMessage -SmtpServer $mailServer -Attachments $zippath -To $recipients -Subject ‚SOBZ Active Directory‘ -From operating@sps.ch -Port 25