例 - PowerShell パフォーマンスモニタスクリプト
PowerShell パフォーマンスモニタスクリプトには、実行に成功するために利用できる 2 つのインスタンス化オブジェクトがあります。
- 。IScriptContext インターフェイスの実装。このオブジェクトを使用するとランタイム変数にアクセスでき、結果をクライアントに返すメカニズムも提供します。メソッドの一部を以下に示します。
- object GetReferenceVariable(string variableName) - 以前に設定した参照変数値を名前で取得できます。
- object GetProperty(string propertyName) - コンテキスト変数値を名前で取得できます。
- void SetResult(int resultCode) - 成功を示す値をスクリプトで設定できます。通常、0 は成功、1 は失敗です。
- 。ILog インターフェイスの実装。このオブジェクトは、C# アプリケーションで利用可能なメソッドと同じメソッドを提供します。便利なメソッドの一部を以下に示します。
- void Error(string message) - メッセージを含んでいるエラー固有のログエントリを作成します。
- void Information(string message) - メッセージを含んでいる情報固有のログエントリを作成します。
- void WriteLine(string message) - メッセージを含んでいる汎用ログエントリを作成します。
コンテキスト変数
PowerShell パフォーマンスモニタスクリプトで使用できるコンテキスト変数は次のとおりです。
- DeviceID
- DisplayName
- Address
- 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
スクリプト例 1
#
# This example looks for a process named 'outlook' and reports its
# current number of threads.
#
# Use the built-in cmdlet named 'Get-Process', also aliased as 'ps'
$processes = ps
$processName = "outlook"
$proc = $processes | where { $_.ProcessName -match $processName }
# Performance monitors must call Context.SetValue() to report results
$Context.SetValue($proc.Threads.Count)
スクリプト例 2
#
# This example uses a reference variable to look for idle time
# levels and logs the results
#
# Use available context variables
$resultText = "Address:" + $Context.GetProperty("Address");
# Access the reference variable
$monitorValue = $Context.GetReferenceVariable("IdleTime")
# Log if necessary
$resultText = $resultText + ", Idle time:" + $monitorValue.ToString()
$Logger.WriteLine($resultText)
# Always set the performance value
$Context.SetValue($monitorValue);