範例:PowerShell 主動監控工具指令碼
PowerShell 主動監控工具指令碼提供兩個實體化的物件,使得執行作業能夠成功:
- : IScriptContext 介面的實作。 這個物件讓您取得執行階段變數,也提供將結果傳回用戶端的機制。 以下列出幾個實用的方法:
- object GetProperty(string propertyName):可根據名稱擷取環境變數值。
- void SetResult(int resultCode):讓指令碼能設定代表執行成敗的值,通常 0 = 成功,1 = 失敗。
- : ILog 介面的實作。 此物件提供 C# 應用程式可使用的相同方法。 以下列出幾個實用的方法:
- void Error(字串訊息):建立特定錯誤的記錄項目,並包含訊息。
- void Information(字串訊息):建立特定資訊的記錄項目,並包含訊息。
- void WriteLine(字串訊息):建立一般性的記錄項目,並包含訊息。
環境變數
您可在 PowerShell 主動監控工具指令碼中使用以下環境變數:
- DeviceID
- DisplayName
- 位址
- NetworkName
- Timeout
- CredWindows:DomainAndUserid
- CredWindows:Password
- CredSnmpV1:ReadCommunity
- CredSnmpV1:WriteCommunity
- CredSnmpV2:ReadCommunity
- CredSnmpV2:WriteCommunity
- CredSnmpV3:AuthPassword
- CredSnmpV3:AuthProtocol (值:1 = 無、2 = MD5、3 = SHA)
- CredSnmpV3:EncryptProtocol (值:1 = 無、2 = DES56、3 = AES128、4 = AES192、5 = AES256、6 = THREEDES)
- CredSnmpV3:EncryptPassword
- CredSnmpV3:Username
- CredSnmpV3:Context
- CredADO:Password
- CredADO:Username
- CredSSH:Username
- CredSSH:Password
- CredSSH:EnablePassword
- CredSSH:Port
- CredSSH:Timeout
- CredVMware:Username
- CredVMware:Password
指令碼逾時
您可以設定指令碼的逾時值 (以秒為單位)。 若指令碼在逾時值到期之前還是沒有執行完成,作業就會直接中止。
最小值: 1
最大值: 60
預設值: 60
指令碼範例
#
# This example looks for a process named 'outlook' and reports if its
# responding
#
# Use the built-in cmdlet named 'Get-Process', also aliased as 'ps'
$processes = ps
$processName = "outlook"
$proc = $processes | where { $_.ProcessName -match $processName }
# Active monitors must call Context.SetResult() to report results
if ($proc -eq $Null)
{
$NotRunningMessage = "Process '" + $processName + "' is not running."
$Context.SetResult(1, $NotRunningMessage )
}
else
{
if ($proc.Responding)
{
$RespondingMessage = "Process '" + $processName + "' is responding."
$Context.SetResult(0, $RespondingMessage )
}
else
{
$NotRespondingMessage = "Process '" + $processName + "' is not responding."
$Context.SetResult(1, $NotRunningMessage )
}
}