範例:PowerShell 動作指令碼

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")

另請參閱

設定動作

加入和編輯動態指令碼動作

加入和編輯呼叫器動作

加入和編輯電子郵件動作

加入和編輯記錄到文字檔動作

加入和編輯呼叫器動作

加入和編輯 PowerShell 動作

加入和編輯程式動作

加入和編輯服務重新啟動動作

加入和編輯簡訊動作

加入和編輯簡訊直送動作

加入和編輯 SNMP Set 動作

加入和編輯音效動作

加入和編輯 SSH 動作

加入和編輯 Syslog 動作

加入和編輯文字轉換語音動作

加入和編輯 VMware 動作

加入和編輯網頁警報動作

加入和編輯 Windows 事件記錄檔動作

使用 WinPopup 動作