Solved

Powershell error onSend-MailMessage

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Searching for GPO references of groups via PowerShell 3 23
Powershell Interpretation Request 2 10
Run Secure WMI query from CentOS 5 26
ACTIVE DIRECTORY 18 45
A procedure for exporting installed hotfix details of remote computers using powershell
While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
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.

786 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