Hi ,
I was writing an powershell script to audit an active directory and send automatic report and I got a lot of help in this forum. below is the original code that is allowing you to receive report for specific AD group.
I would like to add another option that will attach on the report the same report but into an CSV or excel file; below is the script to do that part. I tried different combinaison of those two output but my script still failling. Can someone please help to explain how to two scripts can be merge in one to allow reporting with attahced file and same output will be also into the body email.
------------------------------------------------******************Initial Script start *******--------------------------
#Initialization
# Import Modules
Get-Module -ListAvailable | Import-Module
# Get domain name
$ADDomainName = $((Get-AdDomain).NetBIOSName)
#Initialization
$out = @()
# Get Membership in Domain Admins Groups
Get-ADGroupMember 'Domain Admins' | ForEach {
$userDetails = Get-ADUser -Identity $_.SamAccountName
$props = @{
SecurityGroup = 'Domain Admins'
FirstName = $userDetails.GivenName
LastName = $userDetails.SurName
Username = $userDetails.SamAccountName
}
$out += New-Object PsObject -Property $props
}
# Get Membership in ENterprise Admins Groups
Get-ADGroupMember 'Enterprise Admins' | ForEach {
$userDetails = Get-ADUser -Identity $_.SamAccountName
$props = @{
SecurityGroup = 'Enterprise Admins'
FirstName = $userDetails.GivenName
LastName = $userDetails.SurName
Username = $userDetails.SamAccountName
}
$out += New-Object PsObject -Property $props
}
# Get Membership in Shema Admins Groups
Get-ADGroupMember 'Schema Admins' | ForEach {
$userDetails = Get-ADUser -Identity $_.SamAccountName
$props = @{
SecurityGroup = 'Schema Admins'
FirstName = $userDetails.GivenName
LastName = $userDetails.SurName
Username = $userDetails.SamAccountName
}
$out += New-Object PsObject -Property $props
}
$body = $out | Out-String
Send-MailMessage -To email@domain -From email@domain -Subject 'Group Report' -Body $body -SmtpServer email@domain
------------------------------------------------******************Initial Script end*******--------------------------
----------------------------------****************new line to be merge into the initial script start*******-------------------------
$out | Export-Csv .\GroupReport.csv -NoTypeInformation
Send-MailMessage -To to@domain.com -From from@domain.com -Subject 'Group Report' -Body 'See attachment' -Attachments .\GroupReport.csv -SmtpServer smtp.domain.com
-----------------------------******************new line to be merge into the initial script End *******--------------------------