Sending e-mail via powershell (Net.Mail.SmtpClient or Send-Mailmessage) works, but not when invoked by Task Scheduler

0

I have done a script to check UPS status, log it and send notification e-mail. Script works fine if run manually, but when executed via Task Scheduler, it does not send the e-mail. It does everything else, though (e.g. it logs correctly to the file), so I do not understand what's happening.

Any hint and/or explanation? Thanks a lot!

Here is the script:

<#
.SYNOPSIS
Skript pro test stavu napájení (AC, nebo UPS)
.DESCRIPTION
Zjistí stav napájení, a pokud to není AC, ale baterie, pole notifikační e-mail. Stejně tak ho pole, pokud ádná baterie připojena není.
.PARAMETER none
no parameters
.EXAMPLE
no example needed
.NOTES
crysman (copyleft) 2016
# changelog:
# ...         next version - don't forget to update the $scriptVersion variable!
# 2016-03-10  přidány barvy výstupů, help, upraveny exit codes a návratové hodnoty, upraven hostname
# 2016-03-09  opraveno logování, vč. loggingu nefunkčních mailnotifikací
# 2016-03-08  initial release
#>
$scriptVersion = '2016-03-10'
$scriptName = $MyInvocation.mycommand.name
$timestamp = [datetime]::now.tostring("yyyy-MM-dd_HH-mm")
$hostName = [system.environment]::MachineName.ToLower()


#funkce pro posílání notifikačních mailů
#v.1.2
function sendEmail($Subject, $Body){
  $EmailTo = "it@XXXYOURFAVOURITEDOMAIN.cz"
  $EmailFrom = "$global:hostName@XXXYOURFAVOURITEDOMAIN.cz"
  $SMTPServer = "smtp.XXXYOURFAVOURITEDOMAIN.cz"
  $SMTPClient = New-Object Net.Mail.SmtpClient($SMTPServer, 587)
  $SMTPClient.EnableSsl = $true
  $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("notifications@XXXYOURFAVOURITEDOMAIN.cz", "XXXYOURFAVOURITEPASSWORD");
  $Body = "$Body `n-----`nscript $global:scriptName v.$global:scriptVersion on host $global:hostName`ntimestamp: $global:timestamp"
  #Write-Host $EmailTo $EmailFrom $Subject $Body
  try {
    $SMTPClient.Send([string]$EmailFrom,[string]$EmailTo,[string]$Subject,[string]$Body)
  }
  catch {
    $msg = "$global:timestamp WARNING: notification e-mail has NOT been sent, please check SMTP credentials"
    Write-Host "ERR: " $Error[0].ToString() -ForegroundColor Red
    #následující řádek nefunguje - proč? :( - do souboru se zapisuje, ale na screen ne!
      #Write-Output $msg | Tee -Append -FilePath $global:errorLog
    Write-Host $msg -ForegroundColor Red
    Write-Output $msg | Out-File -Append $global:errorLog
    return $false
  }
  finally {
    $SMTPClient.Dispose()  
  }
  Write-Host "notification e-mail has been sent succesfully" -ForegroundColor Gray
  return $true
}

$scriptDir = "D:\scripts"
$log = "$scriptDir\checkPowerUPS-error.log"
$errorLog = $log


# kategorie stavů baterie viz https://msdn.microsoft.com/en-us/library/aa394074(v=vs.85).aspx
$bStats= @{"1"="battery is discharging";"2"="On AC";"3"="Fully Charged";"4"="Low";"5"="Critical";"6"="Charging";"7"="Charging and High";"8"="Charging and Low";"9"="Charging and Critical";"10"="Undefined";"11"="Partially Charged";} 

# aktuální stav:
$powerStat=Get-WmiObject -class Win32_Battery -ComputerName localhost
$bStat=$powerStat.BatteryStatus
$bStatText=$bStats["$bStat"]
$bStatRemaining=$powerStat.EstimatedRunTime

#zapíeme do EventLogu, popř. poleme mail
#nachystat EventLog
$eLog = $scriptName #pouijeme název skriptu
if (! $eLog) {
  Write-Output "$timestamp něco je patně s powershellem (?)" | tee -Append -FilePath $log
  exit 99  
}
$eLogExists = get-EventLog -LogName Application -Source "$eLog"
if (! $eLogExists){
  echo "WARNING: jetě neexistuje eventlog, vytvářím..."
  new-EventLog -LogName Application -Source "$eLog"
}
#zalogovat a notifikovat
if (!($powerStat) -or $bStat -eq 1 -or $bStat -eq 4 -or $bStat -eq 5) { 
  if(!$powerStat){
    #(nemáme UPS)
    $Subject = "IMPORTANT: Power without UPS on $hostName"
    $Body = "this host seems NOT to be connected to any battery/UPS"
  } else {
    #(UPS máme, ale nějaký non-AC status)
    $Subject = "IMPORTANT: Power failure on $hostName"
    $Body = "battery status is $bStat ($bStatText), estimated remaining battery time: $bStatRemaining min"
  }
  #vypsat na screen a zalogovat:
  Write-Output "$timestamp $Body" | Out-File -Append $log
  Write-Host "$timestamp $Body" -ForegroundColor Red
  Write-EventLog -LogName Application -Source "$eLog" -EntryType Warning -EventId 11 -Message "$eLog skončil neúspěně - $Body"
  #poslat maila:
  $mailsent = sendEmail $Subject $Body
  if ($mailsent) {$exitcode=1} else {$exitcode=2}
} else {
  $exitcode = 0
  Write-Host "OK, $hostName is on AC (status $bStat)`nscript $scriptName v.$scriptVersion" -ForegroundColor Green
}
exit $exitcode

Here is the task:

<Task xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task" version="1.2">
<RegistrationInfo>
<Date>2016-03-07T16:53:24.9000439</Date>
<Author>SERVER-XXX\myadminaccount</Author>
</RegistrationInfo>
<Triggers>
<CalendarTrigger>
<Repetition>
<Interval>PT1M</Interval>
<StopAtDurationEnd>false</StopAtDurationEnd>
</Repetition>
<StartBoundary>2016-03-07T00:00:00</StartBoundary>
<Enabled>true</Enabled>
<ScheduleByDay>
<DaysInterval>1</DaysInterval>
</ScheduleByDay>
</CalendarTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>SERVER-XXX\myadminaccount</UserId>
<LogonType>Password</LogonType>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>StopExisting</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>true</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT1H</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>powershell</Command>
<Arguments>
-NonInteractive -Noprofile -Command "&{D:\scripts\checkPowerUPS.ps1}"
</Arguments>
</Exec>
</Actions>
</Task>

Operating system: Windows Server 2012 R2

PS: I've browsed through both Technet and various Stack Exchange sites to solve this, but no luck :/ So trying here...

crysman

Posted 2016-03-14T10:24:47.867

Reputation: 393

Kinda off-topic but I am wondering, why are you using the .NET SmtpClient class to send email instead of the native Send-Mailmessage cmd-let? – Smeerpijp – 2016-03-14T10:35:30.327

@doenoe Because I've not figured out how to apply authentication using this cmd-let. If you know how, let me know. – crysman – 2016-03-14T10:38:51.477

Take a llook at this TechNet article. There is a -Credential parameter. There's also an example in the article: send-mailmessage -to "User01 <user01@example.com>" -from "ITGroup <itdept@example.com>" -cc "User02 <user02@example.com>" -bcc "ITMgr <itmgr@example.com>" -subject "Don't forget today's meeting!" -credential domain01\admin01 -useSSL

– Smeerpijp – 2016-03-14T10:47:56.817

OK, I've switched to Send-MailMessage cmd-let. However, nothing has changed. Running manually does everything as supposed, including sending the notification e-mail. Task Scheduler reports exit code 0, everything seems just fine, there are new lines in the file log, there are new logs in system Event Viewer, but NO E-MAIL is being sent! :( – crysman – 2016-03-14T13:02:45.737

Hmm, well i've got a sched task thats runs a PS script, and sends a mail when done, this are my task parameters in the actions tab

program script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe Arguments: -command "& 'C:\Scripts\asabackup\asabackup.ps1'" Start in: C:\Scripts\asabackup

Maybe you can adjust your values to match mine (with you own working directory etc.) and test it out again – Smeerpijp – 2016-03-14T13:26:35.943

Even when I added correctly filled-in corresponding "Start in", no effect :/ – crysman – 2016-03-14T14:57:36.583

Answers

1

After hours of debugging, I've got it.

The problem:

usage of -Command "..." argument in Task Scheduler.

Solution:

Use -File "..." argument instead. In this particular case, that would be:

<Exec>
<Command>powershell</Command>
<Arguments>
-NonInteractive -Noprofile -File "D:\scripts\checkPowerUPS.ps1"
</Arguments>
</Exec>

Details:

When using -Command "&{D:\testScript.ps1}", global variables declared in the script are unavailable, that is e.g. this will not work:

$myVar = "ham"
function myF(){
  Write-Output $global:myVar
}
myF

I've not found an answer to my bug-solved-following question: "What is the difference between -Command and -File PowerShell argument?" Invoking powershell /? does not help me anyhow, because it says nothing about global and/or any "semiglobal" variables. Moreover, it states that using -Command:

...Executes the specified commands (and any parameters) as though they were typed at the Windows PowerShell command prompt...

Which is not true, because starting a fresh PowerShell window and executing the lines I put in the example code above with $myVar will work, whereas executing the example with -Command ... will NOT.

I hope this will help you.

crysman

Posted 2016-03-14T10:24:47.867

Reputation: 393

Seems like the source of the problem is exactly where Invoke-Command draws a line between local and remote. (I assume Invoke-Command is used behind-the-scenes for PowerShell's -Command parameter.) You might be able to use -ArgumentList (halfway through this documentation) to work around the issue.

– jpaugh – 2018-06-11T21:49:50.560