Solved

Powershell error onSend-MailMessage

Posted on 2015-02-20
4
21 Views
Last Modified: 2016-06-23
I have this script to get expiring accounts, and then send mail to the User's manager with the information in the body.  This works great if I specify an email address in the -To but if I use $mgr in the -To I get an error like below

Unexpected token 'Send-MailMessage' in expression or statement.
At line:2 char:73
+ $mgr = (Get-Aduser $user.manager -Properties mail).Mail Send-MailMessage <<<<

This is the script, any idea what I am missing that is causing this error?
$expired = Search-ADAccount  -AccountExpiring -TimeSpan 14.00:00:00 -usersonly | ? {$_.DistinguishedName -like "*ou=IT,*"} | Get-ADUser -Properties manager,mail,physicalDeliveryOfficeName,accountExpirationDate | Select-Object name,manager,mail,physicalDeliveryOfficeName,accountExpirationDate
 ForEach ($user in $expired) {
$mgr = (Get-Aduser $user.manager -Properties mail).Mail Send-MailMessage -To $mgr -From admin@admin.com -Body "*** System Access Expiration Notice ***

You are receiving this message because you are the hiring manager for $($user.name). This is to inform you that $($user.name)'s system/network access is set to expire on $($user.accountExpirationDate).  
 
" -Subject "The following User Account is expiring within 14 days"  -priority High -SmtpServer mailserver}
 

Open in new window

0
Comment
Question by:jennileePerez
4 Comments
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
Comment Utility
Please make use of code blocks for entering code on EE.
And by all means use line breaks in your PS code.

Basically you are missing a line break before Send-MailMessage. With some more formatting applied in addition, it looks like this:
$expired = Search-ADAccount  -AccountExpiring -TimeSpan 14.00:00:00 -usersonly |
  ? {$_.DistinguishedName -like "*ou=IT,*"} |
  Get-ADUser -Properties manager,mail,physicalDeliveryOfficeName,accountExpirationDate |
  Select-Object name,manager,mail,physicalDeliveryOfficeName,accountExpirationDate

ForEach ($user in $expired) {
  $mgr = (Get-Aduser $user.manager -Properties mail).Mail
  Send-MailMessage -To $mgr -From admin@admin.com -Body "*** System Access Expiration Notice ***

You are receiving this message because you are the hiring manager for $($user.name). This is to inform you that $($user.name)'s system/network access is set to expire on $($user.accountExpirationDate).  
 
" -Subject "The following User Account is expiring within 14 days"  -priority High -SmtpServer mailserver
}

Open in new window

0
 

Author Comment

by:jennileePerez
Comment Utility
Sorry bout that, thx for moving it into code block
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
Disabling the Directory Sync Service Account in Office 365 will stop directory synchronization from working.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now