Solved

Powershell error onSend-MailMessage

Posted on 2015-02-20
4
33 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 70

Accepted Solution

by:
Qlemo earned 500 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

This article demonstrates probably the easiest way to configure domain-wide tier isolation within Active Directory. If you do not know tier isolation read https://technet.microsoft.com/en-us/windows-server-docs/security/securing-privileged-access/s…
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

734 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