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"
$lastlogondate=[datetime]::Today.AddDays(-30)
$datecreated=[datetime]::Today.AddDays(-30)
$currentdate = Get-Date
$recipients = "First.Last-BG@**.**.***", "First.Last-NG@**.**.***", "First.Last-FT@**.**.***"
$sites |
ForEach-Object{
$site=$_
$ouList |
ForEach-Object{
$o=$_
$props=@{
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 |
ForEach-Object{
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:\temp\NoLogOnForthePast30DaysReport-BG.csv" -NoTypeInformation
$attachfile = "c:\temp\NoLogOnForthePast30DaysReport-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:\temp\NoLogOnForthePast30DaysReport-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.To.Add($recipients)
$msg.subject = $msgsubject
$msg.attachments.add($attachfile)
$msg.body =$msgbody
#Sending email
$smtp.Send($msg)