如果脚本中的任何检查失败,则需要脚本失败

时间:2017-03-27 11:04:43

标签: powershell

我有一个大脚本,我需要添加一个条件,当EM代理检查失败时(如果它是UnavailableStopped),整个脚本状态应该更改为Failed。

我尝试过如下条件,但它不起作用。有谁可以帮我纠正下面的情况?

#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

 }

0 个答案:

没有答案