範例:PowerShell 效能監控工具指令碼
PowerShell 效能監控工具指令碼提供兩個實體化的物件,使得執行作業能夠成功:
- : IScriptContext 介面的實作。 這個物件讓您取得執行階段變數,也提供將結果傳回用戶端的機制。 以下列出幾個方法:
- object GetReferenceVariable(string variableName):可根據名稱擷取先前設定的參考變數值。
 - 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
指令碼範例 #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);