Need script to run for each site then create site report then email to site specific personnel.

by Nov 25, 2013

All I'm currently having issues with combining multiple scripts into one. What I need is to run my script against each site ou then create the site specific csv file by site name then email it to the site specific users. I have been told I need to use parameters and more functions the only problem is I don't know PS very much at all. The script I have is from cutting and pasting from different examples on the internet and trying this and that. Not sure if there is a better way to post the script if there is please let me know. Thanks.


$sites = "BG", "NG", "FT"

$ouList = "class"



$currentdate = Get-Date


$recipients = "First.Last-BG@**.**.***", "First.Last-NG@**.**.***", "First.Last-FT@**.**.***"

$sites |



$ouList |




SearchBase="OU=$o,OU=Users,OU=$site,OU=**,DC=**,DC =**,DC=**,DC=**"

Properties='lastlogondate','whencreated','descript ion'

Filter={lastlogondate -le $lastlogondate -and whencreated -le $datecreated }



Get-ADUser @props |


Write-Host ('{0}|{1}|{2}|{3}' -f $_.samaccountname,$_.Description,$_.LastLogondate, $_.whenCreated,$_.distinguishedname) -Fore green

$_ | Disable-ADAccount -whatif

$_ | Move-ADObject -TargetPath "OU=Stale Accounts,OU=USERS,OU=$site,OU=**,DC=**,DC=**,DC=** ,DC=**"




} | 

Select-Object SamAccountName,Description,LastLogonDate,whenCreat ed,distinguishedname |

Export-Csv -path "c:tempNoLogOnForthePast30DaysReport-BG.csv" -NoTypeInformation 


$attachfile = "c:tempNoLogOnForthePast30DaysReport-BG.csv"

$msgsubject = "No Log On For the Past 30 Days Report-BG – ", $currentdate

$msgbody = "Attached is a list of user accounts that have not been logged onto the domain in the last 30 days." 


function sendMail{


Write-Host "Sending Email"

$attachfile = "c:tempNoLogOnForthePast30DaysReport-BG.csv" 

#SMTP server name

$smtpServer = "***.***.***.***"

#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 = "powershell@**.**.***"

$msg.ReplyTo = "no-reply@**.**.***"


$msg.subject = $msgsubject


$msg.body =$msgbody

#Sending email