As i recall, in Part 5 we had gathered all the account information, parsed that, disabled accounts that needed to be disabled, gathered information on all the disabled accounts, and removed the accounts from the domain that needed to be as well as documented that we did so. Now that leaves me with needing to accomplish one more thing. My business would like to have certain people notified about the account changes that have taken place. This is where the Mail function comes into play. Below is the code that I execute to send email when needed.
function Mail { $file1 = "c:\90-180\file1.Disabled.txt" $file2 = "c:\90-180\file2.Disabled.txt" $file3 = "c:\90-180\file3.Disabled.txt" $file4 = "c:\90-180\file1.Deleted.txt" $file5 = "c:\90-180\file2.Deleted.txt" $file6 = "c:\90-180\file3.Deleted.txt" $smtpServer = "mailhost" $smtp = New-Object Net.Mail.SmtpClient($smtpServer) $msg = New-Object Net.Mail.MailMessage ########################################################## ## Test for Attachment files. If the file is not ## available then do not create the attachment object ########################################################## if(Test-Path $file1) {$att1 = New-Object Net.Mail.Attachment($file1)} if(Test-Path $file2) {$att2 = New-Object Net.Mail.Attachment($file2)} if(Test-Path $file3) {$att3 = New-Object Net.Mail.Attachment($file3)} if(Test-Path $file4) {$att4 = New-Object Net.Mail.Attachment($file4)} if(Test-Path $file5) {$att5 = New-Object Net.Mail.Attachment($file5)} if(Test-Path $file6) {$att6 = New-Object Net.Mail.Attachment($file6)} $msg.From = "90-180Audit@pni.com" $msg.To.Add("somemail@host.com") $msg.Subject = "Disabled and Deleted Domain Accounts" $msg.Body = "Attached are files with names of disabled and deleted accounts." ########################################################## ## Test if the file is available again. If so then ## create the attachment. ########################################################## if(Test-Path $file1) {$msg.Attachments.Add($att1)} if(Test-Path $file2) {$msg.Attachments.Add($att2)} if(Test-Path $file3) {$msg.Attachments.Add($att3)} if(Test-Path $file4) {$msg.Attachments.Add($att4)} if(Test-Path $file5) {$msg.Attachments.Add($att5)} if(Test-Path $file6) {$msg.Attachments.Add($att6)} $smtp.Send($msg) $att1.Dispose() $att2.Dispose() $att3.Dispose() $att4.Dispose() $att5.Dispose() $att6.Dispose() }
first I define all 6 of the files that I may or may not want to attach to the email. Then I set up the SMTPserver and the mail objects. Now I test for each of the files that I may or may not want to attach. I only want to attach the file if the file exists. This way I do not get errors about files not being on the filesystem and I am not sending blank files to the people that need the information.
I then define who the message is from, who it is to, subject, and the body. Now that we have that I have to start attaching the files. After attaching the files that are needed I send the message and then close all the attachments just to make sure that no handles keep the files open. I want to be able to delete the files later.
Well that is it. Next post will have the script in its entirety so that you do not have to cut and paste so much. Hope all this helps you out. Let me know if you have any questions about any part of this script.