例l - PowerShell アクションスクリプト

PowerShell アクションスクリプトには、実行に成功するために利用できる 2 つのインスタンス化オブジェクトがあります。

コンテキスト変数

PowerShell アクションスクリプトに使用できるコンテキスト変数は次のとおりです。

パーセント変数

PowerShell アクションスクリプトに使用できるパーセント変数のリストについては、「パーセント変数」を参照してください。

重要 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 アクションの追加および編集