VMware Cloud Community
stuartgmilton
Enthusiast
Enthusiast

Scheduled PowerCLI ps1 script not sending an email?

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

Reply
0 Kudos
12 Replies
LucD
Leadership
Leadership

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 ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
stuartgmilton
Enthusiast
Enthusiast

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

Reply
0 Kudos
stuartgmilton
Enthusiast
Enthusiast

Nope, PowerCLI just appears to be a ps1 script?

Reply
0 Kudos
LucD
Leadership
Leadership

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 ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
stuartgmilton
Enthusiast
Enthusiast

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

Reply
0 Kudos
stuartgmilton
Enthusiast
Enthusiast

Untitled.png

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

I presume this is what you meant to try?

Reply
0 Kudos
LucD
Leadership
Leadership

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"}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
stuartgmilton
Enthusiast
Enthusiast

Hi Luc, sorry that's a typo.

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

Stu

Reply
0 Kudos
LucD
Leadership
Leadership

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)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
stuartgmilton
Enthusiast
Enthusiast

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?

Reply
0 Kudos
LucD
Leadership
Leadership

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ssaviers
Contributor
Contributor

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?

Reply
0 Kudos