Solved

Powershell error onSend-MailMessage

Posted on 2015-02-20
4
35 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

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

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

A hard and fast method for reducing Active Directory Administrators members.
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

623 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