none
Running PowerShell script as schedule task in Task Scheduler RRS feed

  • Question

  • Hi,

    I created a PowerShell script that uses an imported module to test the integrity of our VMs VHDX files. It then updates a csv file and e-mails it to our distribution group. It runs and updates the csv but doesn't send the e-mail to the group (through the schedule task). If I run the script outside the scheduled task it updates the file and sends the e-mail like it should. Below is the code and the settings for the task.  I changed the names of the e-mail server name, the info is correct and does work outside of the scheduled task

    $Servers = gc "C:\HyperVservers.csv"

    Remove-Item C:\VHDXcurrentstatus.csv
    foreach ($server in $Servers) 
    {Get-VM -ComputerName $server | Test-SBVHDIntegrity | where {(-NOT $_.TestPath) -OR (-NOT $_.TestVHD)}| Out-File "C:\VHDXcurrentstatus.csv" -Append }
    Send-MailMessage -Attachments "C:\VHDXcurrentstatus.csv" -Body "This is a Test"  -From "E-mail Group" -Port "server port #" -Priority High -SmtpServer "E-mail Server" -Subject "Listing of the VM Hard Drive Failures" -To "E-mail Group"

    Schedule Task properties 

    Action is Start Program 

    Program/script = C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

    Argument = -NoProfile -Executionpolicy bypass -file "C:\VHD Integrity Check.ps1" -Parameter 'Value'

    I have it set to run with highest privileges, and to run whether user is logged on or not. 

    The script does run updates the file the issue is it doesn't send the e-mail. 

    Thank you 

    Saturday, June 18, 2016 12:36 AM

Answers

  • Yes, when it is ran outside the task scheduler it sends the e-mails 

    This is not a scripting issue.  It is a scheduler issue.

    That said - What account are you running the task under? You must enable the task for external authentication.  Did you do that?

    Try using only this script and run using your credentials including a password.

    $props=@{ To='group@domain.com' From='mailaccount@domain.com' Subject='Mail test' Body='This is a Test' SmtpServer='smtp.server.com' } Send-MailMessage @props

    Only use "Port" if it is not 25.


    This is the simplest form and should eliminate any script issues.


    \_(ツ)_/


    • Edited by jrv Saturday, June 18, 2016 4:42 AM
    • Marked as answer by NathanMagna Tuesday, June 21, 2016 12:32 AM
    Saturday, June 18, 2016 4:41 AM

All replies

  • Does the account you're running the script under have rights to send the mail, and send from "E-mail Group"?

    Ed Crowley MVP "There are seldom good technological solutions to behavioral problems."
    Celebrating 20 years of providing Exchange peer support!

    Saturday, June 18, 2016 1:01 AM
  • Yes, when it is ran outside the task scheduler it sends the e-mails 
    • Edited by NathanMagna Saturday, June 18, 2016 3:13 AM
    Saturday, June 18, 2016 3:13 AM
  • Yes, when it is ran outside the task scheduler it sends the e-mails 

    This is not a scripting issue.  It is a scheduler issue.

    That said - What account are you running the task under? You must enable the task for external authentication.  Did you do that?

    Try using only this script and run using your credentials including a password.

    $props=@{ To='group@domain.com' From='mailaccount@domain.com' Subject='Mail test' Body='This is a Test' SmtpServer='smtp.server.com' } Send-MailMessage @props

    Only use "Port" if it is not 25.


    This is the simplest form and should eliminate any script issues.


    \_(ツ)_/


    • Edited by jrv Saturday, June 18, 2016 4:42 AM
    • Marked as answer by NathanMagna Tuesday, June 21, 2016 12:32 AM
    Saturday, June 18, 2016 4:41 AM
  • This does seem to be a permissions or lack of e-mail account issue . I used the local and domain admin account to run it and it failed. I used my account and it works. Is it because my e-mail is attached to the distribution group I was trying to send from ? Because the local and domain admin account should have sufficient permissions, but it doesn't have an e-mail account. Also, we have computers on two different domains which makes things a little hairy sometimes.

    Thanks for the help

    Tuesday, June 21, 2016 12:41 AM
  • This does seem to be a permissions or lack of e-mail account issue . I used the local and domain admin account to run it and it failed. I used my account and it works. Is it because my e-mail is attached to the distribution group I was trying to send from ? Because the local and domain admin account should have sufficient permissions, but it doesn't have an e-mail account. Also, we have computers on two different domains which makes things a little hairy sometimes.

    Thanks for the help

    An account without email in an Exchange domain cannot send mail even if it is an Admin account.

    Sorry.


    \_(ツ)_/

    Tuesday, June 21, 2016 1:52 AM