1

I have a big script where I need to add a condition that when ever below EM Agent check failed (if it's Unavailable or Stopped), the entire script status should change to Failed.

I have tried a condition like below, but it's not working. Can anyone please help me correcting the condition below?

#EM Agent Information=====================================================================

$EMAgent = get-wmiobject win32_service | where-object {($_.Name -eq 'Oracle12cAgent') -or ($_.Name -eq 'Oracleagent12c2Agent') -or ($_.Name -eq 'Oracleagent10gAgent') -or ($_.Name -eq 

'FarmEM10gAgent') -or ($_.Name -eq 'FarmEM11gAgent')} | format-list name | Out-String
$AgentName = $EMAgent.Split(":")[1].Trim()

$EMStatus = get-wmiobject win32_service | where-object {$_.Name -eq $AgentName} | format-list state | Out-String
$AgentStatus = $EMStatus.Split(":")[1].Trim()

if ($AgentName -eq $null)
{
$AgentName = "Unavailable"
Add-Content $report "<tr>"
Add-Content $report "<td bgcolor= 'White'  height='30' align=center><B>11</B></td>"
Add-Content $report "<td bgcolor= 'White'  height='30' align=left><B>EM Agent Version</B></td>"
Add-Content $report "<td bgcolor= 'red'  height='30' align=left><B>$AgentName</B></td>"
Add-Content $report "</tr>"
}
else
{
Add-Content $report "<tr>"
Add-Content $report "<td bgcolor= 'White'  height='30' align=center><B>11</B></td>"
Add-Content $report "<td bgcolor= 'White'  height='30' align=left><B>EM Agent Version</B></td>"
Add-Content $report "<td bgcolor= 'Aquamarine'  height='30' align=left><B>$AgentName</B></td>"
Add-Content $report "</tr>"
}

echo "EM Agent Version = $AgentName"

if ($AgentStatus -eq "Running")
{
Add-Content $report "<tr>"
Add-Content $report "<td bgcolor= 'White'  height='30' align=center><B>12</B></td>"
Add-Content $report "<td bgcolor= 'White'  height='30' align=left><B>EM Service Status</B></td>"
Add-Content $report "<td bgcolor= 'Aquamarine'  height='30' align=left><B>$AgentStatus</B></td>"
Add-Content $report "</tr>"
}
if ($AgentStatus -eq "Stopped")
{
Add-Content $report "<tr>"
Add-Content $report "<td bgcolor= 'White'  height='30' align=center><B>12</B></td>"
Add-Content $report "<td bgcolor= 'White'  height='30' align=left><B>EM Service Status</B></td>"
Add-Content $report "<td bgcolor= 'red'  height='30' align=left><B>$AgentStatus</B></td>"
Add-Content $report "</tr>"
}

echo "EM Service Status = $AgentStatus"

#Condition if agent stopped or unavailable script should fail ========================================================================

If ($AgentStatus -eq "Stopped" -or $AgentName -eq "unavailable") {

exit 1

}

else

{

exit 0

 }
henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
Shimith
  • 87
  • 1
  • 10
  • 1
    You can use `throw` ([documentation link](https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.core/about/about_throw) ) instead of `exit` if you want it to terminate the script. – henrycarteruk Mar 27 '17 at 11:11
  • Do the other if statements fire when the status is one of those states? Is this reflected in your output file? _Not working_ is not descriptive – Matt Mar 27 '17 at 12:08
  • Please have a look at this q on `Format-Table`: http://stackoverflow.com/questions/36358047/how-can-i-store-output-from-format-table-for-later-use/36358921#36358921. The first part of this code hurts my head. You do a lot of unnecessary work to get those values. I am not sure if that is the whole part of the issue though. – Matt Mar 27 '17 at 12:11

0 Answers0