连接

 

发布时间: 2016年3月

适用对象:Windows Azure Pack for Windows Server, System Center 2012 R2 Orchestrator

自动化 连接包含从 runbook 连接到服务或应用程序所需的信息。 此信息在应用程序的模块中定义,通常包含诸如用户名和密码以及要连接到的计算机等信息。 可能还需要证书或订阅 ID 之类的其他信息。 连接的属性安全地存储在 自动化 数据库中,可以通过 Get-AutomationConnection 活动在 Runbook 中进行访问。

Windows PowerShell Cmdlet

下表中的 cmdlet 用于通过 Service Management Automation 中的 Windows PowerShell 创建和管理凭据。

Cmdlet

描述

Get-SmaConnection

检索特定连接中每个字段的值。

Get-SmaConnectionField

检索特定连接类型的字段定义。

Get-SmaConnectionType

检索可用连接类型。

New-SmaConnection

创建新连接。

Remove-SmaConnection

删除现有连接。

Set-SmaConnectionFieldValue

为现有连接设置特定字段的值。

Runbook 活动

下表中的活动用于在 runbook 中访问连接。

活动

描述

Get-AutomationConnection

获取要在 runbook 中使用的连接。

创建新连接

使用管理门户创建新连接

  1. 选择“自动化”工作区。

  2. 在窗口顶部,单击“资产”。

  3. 在窗口底部,单击“添加设置”。

  4. 单击“添加连接”。

  5. 在“连接类型”下拉列表中,选择连接类型。

  6. 在“名称”框中为连接输入名称。

  7. 单击右箭头。

  8. 为每个属性输入值。

  9. 单击复选标记以保存连接。

使用 Service Management Automation 中的 Windows PowerShell 创建新连接

下面的示例命令创建一个名称为 MyVMMConnection 的新 Virtual Machine Manager 连接。 请注意,我们使用哈希表定义连接的属性。 这是因为不同类型的连接需要不同的属性集。 另一种类型的连接会使用另一组字段值。

有关哈希表的详细信息,请参阅 about_Hash_Tables

$webServer = 'https://MyWebServer'
$port = 9090
$connectionName = 'MyConnection'
$fieldValues = @{"Username"="MyUser";"Password"="password";"ComputerName"="MyComputer"} 
New-SmaConnection –WebServiceEndpoint $webServer –port $port –Name $connectionName –ConnectionTypeName "VirtualMachineManager" –ConnectionFieldValues $fieldValues

在 runbook 中使用连接

使用 Get-AutomationConnection 活动可在 runbook 中使用连接。 此活动检索连接中不同字段的值,并将它们作为哈希表返回,随后可以在 runbook 中与适当命令配合使用。

有关哈希表的详细信息,请参阅 about_Hash_Tables

下面的示例代码演示如何使用连接为在另一台计算机上运行命令的 a8b7e82f-e3fc-4286-8570-8d5ded944b27#bkmk_InlineScript 块提供计算机名和凭据。

$con = Get-AutomationConnection -Name 'MyConnection'
$securepassword = ConvertTo-SecureString -AsPlainText -String $con.Password -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $con.Username, $securepassword
InlineScript {
   <Commands>
} -PSComputerName $con.ComputerName -PSCredential $cred