Hi All,
I want to setup a script that will automatically audit some active directory group and send a email report.
Actually, the script is working fine but I would like to add the following below:
1- put a separation between name because the output gave just the name and sometime it's hard to distinguish..so I need to put a separation between name.
2-I want to put the output into a table with header as below before sending it by email:
username first name last name
below is the script
# Import ModulesGet-Module -ListAvailable | Import-Module
# Get domain name
$ADDomainName = $((Get-AdDomain).NetBIOSName)
# Get Membership in Domain Administrative Groups
set-variable domainAdmins -value (Get-ADGroupMember "Domain Admins" -recursive | foreach {$_.samaccountname}{$_.Name})
set-variable enterpriseAdmins -value (Get-ADGroupMember "Enterprise Admins" -recursive | foreach {$_.samaccountname}{$_.Name})
set-variable schemaAdmins -value (Get-ADGroupMember "Schema Admins" -recursive | foreach {$_.samaccountname}{$_.Name})
# Consolidated List of Accounts with Administrative Access
#$allAdmins = $($domainAdmins + $enterpriseAdmins + $schemaAdmins | sort -unique)
# Send Email
Write-Host 'Sending email'
# smtp server
$smtpServer = 'smtp server here'
# creating a mail object
$msg = new-object Net.Mail.MailMessage
# creating smtp server object
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
# email structure
$msg.From = 'test@test.com'
$msg.ReplyTo = 'doNotReply@test.com'
$msg.To.Add('destination add')
$msg.subject = 'Audit Active Directory Domain '
$msg.body = $('Domain Admin Users:' + '' + $domainAdmins | sort -unique ) + '' + 'Enterprise Admin Users:' + ''+ $($enterpriseAdmins | sort -unique) + '' + 'Shema Admin User:' + '' + ($schemaAdmins | sort -unique)
# sending email
$smtp.Send($msg)