locked
unexpected token in expression or token RRS feed

  • Question

  • In the script below I'm getting the above message related to the last two lines of code (send-mailmessage -from "passwordreminders@saintpeters.edu" -to "dshephard@saintpeters.edu" -subject "Daily  Expired SPC Accounts" -body (Get-Content $LogPath | out-string ) -smtpServer smtp-relay.gmail.com") PLEASE HELP.

    mailer = new-object Net.Mail.SMTPclient($SMTPserver)
    $msg = new-object Net.Mail.MailMessage($from, $to, $subject, $emailbody)
    $mailer.send($msg)
    echo $($_.mail)  | Out-File $LogPath -append
    }
    send-mailmessage -from "passwordreminders@saintpeters.edu" -to "dshephard@saintpeters.edu" -subject "Daily  Expired SPC Accounts" -body (Get-Content $LogPath | out-string ) -smtpServer smtp-relay.gmail.com"


    IT consultant

    • Moved by Bill_Stewart Monday, March 12, 2018 8:15 PM This is not "teach me incrementally how to debug a script" forum
    Tuesday, January 16, 2018 4:28 PM

All replies

  • Why are you running two different mail methods.  Just use Send-MailMessage.


    \_(ツ)_/

    Tuesday, January 16, 2018 4:41 PM
  • send-mailmessage -from passwordreminders@saintpeters.edu -to dshephard@saintpeters.edu -subject 'Daily  Expired SPC Accounts' -body (Get-Content $LogPath | out-string) -smtpServer smtp-relay.gmail.com
    
    # or this:
    $mailprops = @{
    	from	 = 'passwordreminders@saintpeters.edu'
    	to	     = 'dshephard@saintpeters.edu'
    	subject  = 'Daily  Expired SPC Accounts'
    	body	 = (Get-Content $LogPath | out-string)
    	smtpServer = 'smtp-relay.gmail.com'
    }
    Send-MailMessage @mailprops


    \_(ツ)_/

    Tuesday, January 16, 2018 4:44 PM
  • Sorry but what change should I make above.

    IT consultant

    Tuesday, January 16, 2018 6:12 PM
  • Owe and this segment of the code throws the error message:

    "passwordreminders@saintpeters.edu" -to "mcristallo@saintpeters.edu" -subject "Daily


    IT consultant

    Tuesday, January 16, 2018 6:13 PM
  • Owe and this segment of the code throws the error message:

    "passwordreminders@saintpeters.edu" -to "mcristallo@saintpeters.edu" -subject "Daily


    IT consultant

    You didn't even try my examples. You don't need to quote everything with double quotes. Don't get in the bad habit of quoting everything that doesn't move. Learn the rules of quoting in PS.

    \_(ツ)_/

    Tuesday, January 16, 2018 7:01 PM
  • Thank you sir that worked like a charm...

    IT consultant

    Tuesday, January 16, 2018 7:41 PM
  • Hi sir,as I stated it runs with no errors but when I check the log file it doesn't show that any emails were send to users with expiring passwords and I did a trace on the send email address and no emails were sent from it:

    #Active Directory Group Name To Be Edited
    #Load Active Directory Module
    if(@(get-module | where-object {$_.Name -eq "ActiveDirectory"} ).count -eq 0) {import-module ActiveDirectory}
    # get domain maximumPasswordAge value
    $MaxPassAge = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge.days
    if($MaxPassAge -le 0)
    {
      throw "Domain 'MaximumPasswordAge' password policy is not configured."
    }
    #Send Alert to User
    $DaysToExpire = 14
    $Path = "C:\Scripts\Logs\PasswordExpire"
    $a=get-date -format "ssmmHHddMMyyyy"
    $LogPath = $Path + "\" + $a + ".txt"
    #Create Daily Log File
    echo "Daily Log for $a" | Out-File $LogPath -append
    echo "-----------------------" | Out-File $LogPath -append
    #Check users that have a password expiring in 14 days or less
    Get-ADUser -SearchBase (Get-ADRootDSE).defaultNamingContext -Filter {(Enabled -eq "True") -and (PasswordNeverExpires -eq "False") -and (mail -like "*")} -Properties * | Select-Object Name,SamAccountName,mail,@{Name="Expires";Expression={ $MaxPassAge - ((Get-Date) - ($_.PasswordLastSet)).days}} | Where-Object {$_.Expires -gt 0 -AND $_.Expires -le $DaysToExpire } | ForEach-Object {
    #Send Email to user that password is going to expire
    $SMTPserver = "smtp-relay.gmail.com"
    $from = "passwords@saintpeters.edu"
    $to =  $_.mail
    #$to =  "mcristallo@saintpeters.edu"
    $subject = "Password reminder: Your Saint Peter's University passwords will expire in $($_.Expires) days"
    $emailbody = "Dear ​member of the University community,
    Your Saint Peter's University passwords for the account $($_.SamAccountName) will expire in $($_.Expires) days. 

    Please use the online password reset tool at https://reset.saintpeters.edu to avoid being locked out of your accounts.

    After resetting your password, ​you will need to update your​ login
    credentials on all devices you use such as your smart phone, tablet​,
    laptop,​ etc. On these devices you will need to ​enter your new password
    ​for both your e-mail and access to our wireless (Wi-Fi) network. W​i-Fi
    passwords for SPU-EMPLOYEES and SPU-STUDENTS will have​ to be​ updated on
    each device​ you use to connect to our network.
    If you are unsure how to proceed, or experience any ​difficulties
    ​resetting your password​, please​ call the IT Client Services at (201)761-7800
    or e-mail us at servicedesk@saintpeters.edu​. You are also welcome to visit
    us in Pope Hall room 213.
    Thank you for your understanding and cooperation during this important
    biannual process. ​

    Sincerely,

    Division of Information Technology
    Saint Peter's University
    ​http://www.saintpeters.edu/its
    (201)761-7800​

    $mailer = new-object Net.Mail.SMTPclient($SMTPserver)
    $msg = new-object Net.Mail.MailMessage($from, $to, $subject, $emailbody)
    $mailer.send($msg)
    echo $($_.mail)  | Out-File $LogPath -append
    }
    send-mailmessage -from passwordreminders@saintpeters.edu -to mcristallo@saintpeters.edu -subject Daily  Expired SPC Accounts -body (Get-Content $LogPath | out-string ) -smtpServer smtp-relay.gmail.com"

    }


    IT consultant

    Wednesday, January 17, 2018 2:26 PM
  • You will need to place trace statements or run in debug mode to find out what is happening.

    Please post you code correctly. 


    \_(ツ)_/

    Wednesday, January 17, 2018 2:35 PM