PowerShell 動作指令碼提供兩個實體化的物件,使得執行作業能夠成功:
您可在 PowerShell 動作指令碼中使用以下環境變數:
請參閱百分比變數一節列出可用於 PowerShell 動作指令碼的百分比變數清單。
Important: 在 PowerShell 指令碼中將百分比變數當成字串文字時,請以雙引號 (" ") 包住字串文字,不要使用單引號 (' ')。 例如: $Message = "%Device.DisplayName changed state"。
使用者可以設定指令碼的逾時值 (以秒為單位)。 若指令碼在逾時值到期之前還是沒有執行完成,作業就會直接中止。
最小值: 1
最大值: 60
預設值: 10
例 1:
#
# This example plays a sound file
#
# Point to an existing wav file
$wavFile = "C:\temp\Sound1.wav"
# Create a .NET SoundPlayer object
$sound = new-Object System.Media.SoundPlayer;
$sound.SoundLocation=$wavFile;
# Play the file
$sound.Play();
# Report the action results. The text will also be logged
$Context.SetResult($result, "Sound action completed")
例 2:
#
# This example sends an email
#
# Change this value to the recipient
$to = "target_email"
# Change this value to the sender
$from = "source_email"
# This line creates a .NET object for the message
$message = New-Object system.Net.Mail.MailMessage $from, $to
$message.Subject = "Notification from " + $Context.GetProperty("DisplayName")
$message.Body = "Address is down: " + $Context.GetProperty("Address")
# Name the mail server
$server = "alpha.ipswitch.com"
# Create a .NET object to represent the mail client
$client = New-Object System.Net.Mail.SmtpClient $server
$client.UseDefaultCredentials = $true
$result = 1
# Send the message. If no exception is thrown, consider it a success
try {
$client.Send($message);
$result = 0
}
catch {
$result = 1
}
# Report the action results. The text will also be logged
$Context.SetResult($result, "Email Action Completed")