Powershell: Use PS to email results
-
In taking on a new role, I'm running some PS scripts built by another, but I wish to expand on it to add functions currently done manually.
Once Connected to O365, the primary script I will be working with creates a new email and places them into their proper distro groups.
Once this is done, I manually send an email with the results.
I see there is a Send-MailMessage cmdlet, and I would like to see how best to add this to the script to increase the efficiency of the script, and reduce my Copy/Paste error.
-
-
Here is some of my script. Its just a 2 liner, but gets the job done by looking at AD and everybody who hasn't logged in in the last 3 weeks (or whatever set period of time you want to establish based on days:hours:minutes:seconds), puts it into a spreadsheet, stores it in a file server to hold it until the next line. The next line creates the email, and attaches it the csv it just created and sends it along.
Search-ADAccount -AccountInactive -TimeSpan 20.00:00:00 -UsersOnly -SearchBase "CN=Users,DC=Domain,DC=com" | Select-Object Name,Enabled,LastLogonDate | sort enabled | export-csv \\server\filelocation.csv Send-MailMessage -From [email protected] -To [email protected],[email protected] -Subject "Inactive Users" -SmtpServer tennant-com.mail.protection.outlook.com -Attachments \\server\filelocation.csv
Hope that helps.
-
In this case we have the following variables:
$domain
$defaultPassword
$firstname
$lastname
$alias
$teamgroup1
$teamgroup2
What I would like to end up with:
to: person
cc: person1, person2
Subject:
Body:
$firstname $last name has new email of $firstname + $lastname @ $domain
Alias of $alias + $domain
Password: $Defaultpassword
Group: teamgroup1-Regards
-
@nerdydad said in Powershell: Use PS to email results:
Hope that helps.
Really? I thought your reply was meant not to help.
-
-
@nerdydad said in Powershell: Use PS to email results:
https://mangolassi.it/topic/13325/creating-a-new-user-with-o365-with-powershell
Thanks - I'll have to see about working through that and seeing what I can merge into the script I have.
-
I use a PS script I found on to email users when passwords are expiring.
I would link to it but not allowed.
-
@jaredbusch said in Powershell: Use PS to email results:
I use a PS script I found on to email users when passwords are expiring.
I would link to it but not allowed.
Sorry that this is such a PITA!! But not my doing.
-
@jaredbusch said in Powershell: Use PS to email results:
I use a PS script I found on to email users when passwords are expiring.
I would link to it but not allowed.
I have one from somewhere else let me see if I can find it. Here it is: https://www.serveradventures.com/the-adventures/send-pwdnotification-powershell-script-to-notify-users-of-password-expiration
-
@jaredbusch said in Powershell: Use PS to email results:
I use a PS script I found on to email users when passwords are expiring.
I would link to it but not allowed.
Why don't you just copy and paste the script over here?
Its your knowledge. Who's to say that YOU can't share it in both places?
Sorry. Saw that important word there "found". I rest my case.
-
@nerdydad said in Powershell: Use PS to email results:
@jaredbusch said in Powershell: Use PS to email results:
I use a PS script I found on to email users when passwords are expiring.
I would link to it but not allowed.
Why don't you just copy and paste the script over here?
Its your knowledge. Who's to say that YOU can't share it in both places?
Sorry. Saw that important word there "found". I rest my case.
I find a lot of stuff on pastebin.com. No issues with sharing pastebin stuff on ML.
-
@tim_g said in Powershell: Use PS to email results:
@nerdydad said in Powershell: Use PS to email results:
@jaredbusch said in Powershell: Use PS to email results:
I use a PS script I found on to email users when passwords are expiring.
I would link to it but not allowed.
Why don't you just copy and paste the script over here?
Its your knowledge. Who's to say that YOU can't share it in both places?
Sorry. Saw that important word there "found". I rest my case.
I find a lot of stuff on pastebin.com. No issues with sharing pastebin stuff on ML.
Interesting.
-
I can't remember where is nabbed it from, but I use the following:
Function Mailer { $emailTo = "" $emailFrom = "" $subject="It's broken, damn it" $smtpserver="" $smtp=new-object System.Net.Mail.SmtpClient($smtpServer) $Message = @" Insert a useful/meaningful message here. "@ } Mailer
Last line calls the function (duh for you, but a visitor here might not know).