Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Powershell error onSend-MailMessage

Posted on 2015-02-20
4
Medium Priority
?
47 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
2 Comments
 
LVL 71

Accepted Solution

by:
Qlemo earned 2000 total points
ID: 40621697
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
ID: 40621915
Sorry bout that, thx for moving it into code block
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
Active Directory can easily get cluttered with unused service, user and computer accounts. In this article, I will show you the way I like to implement ADCleanup..
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.
Suggested Courses

824 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