Powershell script problem with Task Scheduler

  powershell, server, taskscheduler, windows

This PowerShell script, when called from Task Scheduler always sends the email which is part of True Block, even when the condition is False. Each time, when script called from Task scheduler , it will print echo "PDB_STATUS=$PDB_STATUS", which is part of False Block, but it will also send email every time.

When running the script manually from command line, it works just fine.
Cannot figure out what is the problem.

$env:ORACLE_SID="SID"
$env:ORACLE_HOME="D:oracleappc_home"
$env:PATH+=";D:oracleappc_homebin"
Set-Location -Path D:Oracle_ScriptsDB_Status

#Check PDB Status
$PDB_STATUS=
@'
set heading off
select OPEN_MODE from v$pdbs where NAME='SID';
'@ | sqlplus -s / as sysdba

#Remove Whitespace from Status Variable
$PDB_STATUS=$PDB_STATUS | select -last 2 | select -first 1

if ($PDB_STATUS -ne "READ WRITE")
{
    echo "Pluggable Database is not open for Read Write"
    $body = "Pluggable Database is not open for Read Write"
    $sender = '[email protected]'+$env:computername
    $subject = 'Database Status Alert '+$env:computername
    Send-MailMessage -From $sender -Subject $subject -SmtpServer "xx.xx.xx.xx" -Body $body -BodyAsHtml -To 'user<[email protected]>' 
}
else
{
##Log DB Status
echo "PDB_STATUS=$PDB_STATUS"
@'
set lin 300
col name for a20
select DBID,name, open_mode, to_char(open_time,'HH24:MI:SS DD-MON-YY') as open_time , to_char(SYSDATE,'HH24:MI:SS DD-MON-YY') as NOW from v$pdbs where name='SID';
'@ | sqlplus -s / as sysdba >> DB_Status.log
}

Output:

PDB_STATUS=READ WRITE

Source: Windows Questions

LEAVE A COMMENT