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
}