1

I'm hoping you geniuses can help me out today. I'm not a savant with PowerShell, I only use it to automate some tasks to make life easier. The one thing I'm trying to automate is a daily password reminder to those employees whose password will be expiring within 14 days. The script I wrote (NewPasswordChange.ps1) works flawlessly when I run it via PowerShell, but will not run when put into Task Scheduler. Please let me know what I'm doing wrong.

Action: Start a program Program/Script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Add Arguments: -File 'NewPasswordChange.ps1' -smtpServer 'COMPANY-com.mail.protection.outlook.com' -expireInDays '14' -from 'IT Support it_support@COMPANY.com' -Logging -LogPath 'c:\logFiles'

  • 1
    You are sending the mail parameters to powershell.exe, not to the script. – vonPryz Oct 08 '20 at 12:39
  • So, how would I fix it? The argument I used (Minus -File) is exactly what I use in PS to run the script manually. – Shane Gibeault Oct 08 '20 at 13:11
  • 2
    It would probably be better to use the Send-MailMessage command in the PS script to send the email, then use -File to call only the script. – dno Oct 08 '20 at 13:26

1 Answers1

0

Only powershell understands the single-quotes. When used from a regular command line 'IT Support it_support@COMPANY.com' for example will be interpreted as 3 arguments:

  1. 'IT
  2. Support
  3. it_support@COMPANY.com'

Use double quotes instead:

-File "NewPasswordChange.ps1" -SmtpServer "COMPANY-com.mail.protection.outlook.com" -ExpireInDays 14 -From "IT Support it_support@COMPANY.com" -Logging -LogPath "c:\logFiles"
marsze
  • 15,079
  • 5
  • 45
  • 61