PowerCLI

 View Only
Expand all | Collapse all

Scheduled PowerCLI ps1 script not sending an email?

  • 1.  Scheduled PowerCLI ps1 script not sending an email?

    Posted May 19, 2014 10:47 AM

    So I've followed the posts on here about setting up a scheduled task within windows to run one of my scripts on a daily basis.  The script runs, but far too quickly - it returns a returncode of 4294705152

    Can anyone give any pointers as to why the script will send an email when called from the shell, but does not send an email via a scheduled task.

    I'm using the Send-MailMessage command to send the mail.

    Many Thanks,
    Stuart



  • 2.  RE: Scheduled PowerCLI ps1 script not sending an email?

    Posted May 19, 2014 10:52 AM

    You should start by verifying the account under which the scheduled task runs.

    Does that account have the required permissions, to vCenter, to the SMTP server....

    Did you already try an interactive session with that account ?

    Does it run the script correctly in that case ?



  • 3.  RE: Scheduled PowerCLI ps1 script not sending an email?

    Posted May 19, 2014 10:57 AM

    Hi LucD,

    The account has admin rights for vCentre and SMTP etc.

    yes the script works fine from the PowerCLI console - Actually I could be being a stupid idiot!  I'm telling the script to run using powershell.exe, not powercli.exe

    Let me just change that and see what happens.

    Stuart



  • 4.  RE: Scheduled PowerCLI ps1 script not sending an email?

    Posted May 19, 2014 10:59 AM

    Nope, PowerCLI just appears to be a ps1 script?



  • 5.  RE: Scheduled PowerCLI ps1 script not sending an email?

    Posted May 19, 2014 11:05 AM

    Could you perhaps include the important lines of your script here ?

    Including the line where you do the Send-MailMessage.

    And perhaps also the command line which you used in the scheduled task to run the script ?



  • 6.  RE: Scheduled PowerCLI ps1 script not sending an email?

    Posted May 19, 2014 11:18 AM

    Yeh sorry, I'm being a bit slack!

    So this test script is very simple;

    ---------------------------------------------------------------------------------------------------------
    Connect-VIServer servername -User username -Password password


    @mailParams = @{
    To = "email address"
    From = "email address"
    Subject = "Whatever"
    Body = "<HTML></HTML>"
    SmtpServer = "ipaddress"}

    Send-MailMessage @mailParams -BodyAsHtml

    --------------------------------------------------------------------------------------------------------

    The Scheduled Task command is;

    Start a program
    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

    Add Arguments

    -PSConsoleFile = "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" & "C:\DailyChecksEmail.ps1"


    I have the task running with Highest Privileges



  • 7.  RE: Scheduled PowerCLI ps1 script not sending an email?

    Posted May 19, 2014 02:13 PM

    Hi LucD, The script runs without error? But doesn't send the email.

    I presume this is what you meant to try?



  • 8.  RE: Scheduled PowerCLI ps1 script not sending an email?

    Posted May 19, 2014 03:34 PM

    I suspect it might be a typo, but the splatting should be like this.

    Can you check ?

    $mailParams = @{

    To = "email address"

    From = "email address"

    Subject = "Whatever"

    Body = "<HTML></HTML>"

    SmtpServer = "ipaddress"}



  • 9.  RE: Scheduled PowerCLI ps1 script not sending an email?

    Posted May 20, 2014 08:00 AM

    Hi Luc, sorry that's a typo.

    The script works if I run it from powercli using .\ notation.

    Stu



  • 10.  RE: Scheduled PowerCLI ps1 script not sending an email?

    Posted May 20, 2014 11:14 AM

    Ok, some things to try

    Add the -noprofile switch to the invocation of powershell.exe

    Add a line '$WarningPreference = "SilentlyContinue"'to the script (on the first line)



  • 11.  RE: Scheduled PowerCLI ps1 script not sending an email?

    Posted May 20, 2014 02:46 PM

    Interesting, I added the 2 items you suggested.

    The only noticeable difference was that the return code has changed from 4294705152 to 0

    Any ideas?



  • 12.  RE: Scheduled PowerCLI ps1 script not sending an email?

    Posted May 20, 2014 05:16 PM

    The WarningPreference variable would suppress all Warning types messages.

    Scheduled PoweCLI scripts have been known to hang on such a warning message.

    Running the PowerShell engine with the NoProfile parameter, avoids loading the user's PS profile.

    Some profiles, depending on what is in there, could have issues when run as a scheduled task.

    When does the RC change to 0, when both are used, or when only one of them is used ?

    But the mail is still not sent I understand.

    One more thing to try, try running the scheduled task under another account.

    But make sure that account can execute the script interactively without any issue.

    You could also consider adding Start-Transcript and Stop-Transcript cmdlets, perhaps the log shows something



  • 13.  RE: Scheduled PowerCLI ps1 script not sending an email?

    Posted May 21, 2014 11:11 PM

    Would attempting to drop the call to the console file and instead just include in the top of the script being called "Add-PSSnapin VMware.VimAutomation.Core"

    I have more logic in the top of my scripts to check for the snapin first etc but wasn't sure if this is a bad practice either and was a bit reluctant on posting as an option.

    Also not related to execution policy?