范例:PowerShell 操作脚本

PowerShell 操作脚本提供两个实体化的对象,使得执行操作能够成功:

环境变量

您可在 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 操作