【问题标题】:Error while running Azure runbook which executes PowerShell command on Virtual Machine运行在虚拟机上执行 PowerShell 命令的 Azure Runbook 时出错
【发布时间】:2017-09-13 09:14:28
【问题描述】:

我正在尝试在 Runbook 中执行此代码,使用“Invoke-Command”连接到 VM。

$connectionName = "AzureRunAsConnection"
try
{
    # Get the connection "AzureRunAsConnection "
    $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName

    "Logging in to Azure"
    Add-AzureRmAccount `
        -ServicePrincipal `
        -TenantId $servicePrincipalConnection.TenantId `
        -ApplicationId $servicePrincipalConnection.ApplicationId `
        -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint

    # Use the subscription that this Automation account is in
    $null = Select-AzureRmSubscription -SubscriptionId $servicePrincipalConnection.SubscriptionID
    Get-AzureRmVM | Select Name
    $dcred = Get-AutomationPSCredential -Name 'myvm1creds'
    Write-Output $DomainCred
    $opts = New-PSSessionOption -SkipCACheck
    Invoke-Command -Computername 'myVM1' -Credential $dcred -ScriptBlock {Get-Process} -SessionOption $opts
}
catch {
    if (!$servicePrincipalConnection)
    {
        $ErrorMessage = "Connection $connectionName not found."
        throw $ErrorMessage
    } else{
        Write-Error -Message $_.Exception
        throw $_.Exception
    } 
}

得到以下错误:

[myVM1] 连接到远程服务器 myVM1 失败并显示以下错误消息:WinRM 客户端无法处理 要求。如果身份验证方案与 Kerberos 不同,或者客户端计算机未加入域, 那么必须使用 HTTPS 传输,或者必须将目标计算机添加到 TrustedHosts 配置设置中。 使用 winrm.cmd 配置 TrustedHosts。请注意,TrustedHosts 列表中的计算机可能未经过身份验证。你 可以通过运行以下命令获得更多信息:winrm help config。有关详细信息,请参阅 about_Remote_Troubleshooting 帮助主题。 + CategoryInfo : OpenError: (myVM1:String) [], PSRemotingTransportException + FullyQualifiedErrorId : ServerNotTrusted,PSSessionStateBroken

知道在 Azure 虚拟机上通过 runbook 运行 powershell 脚本需要做什么

【问题讨论】:

标签: powershell azure azure-virtual-machine powershell-remoting runbook


【解决方案1】:

在 Azure Runbook 中,我们不能使用传输 HTTP 来连接 Azure VM,因为 Azure Runbook 无法添加信任主机,所以我们需要使用 HTTPS 来连接 Azure VM。

这是我的步骤:
1.创建自签名证书。

使用makecert.exe 创建它。

2.Config Winrm 监听 HTTPS,在 CMD 中运行这个脚本:

winrm create winrm/config/Listener?Address=*+Transport=HTTPS @{Port="5986" ;Hostname="jasonvm" ;CertificateThumbprint="98941E137CDF9553CCB0C28D5814EB9EDB1AC87D"}

3.在Azure NSG入站规则和windows防火墙入站规则中添加port 5986。 4.我们可以使用这个Runbook来连接Azure VM:

$connectionName = "AzureRunAsConnection"
try
{
    # Get the connection "AzureRunAsConnection "
    $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName         

    "Logging in to Azure..."
    Add-AzureRmAccount `
        -ServicePrincipal `
        -TenantId $servicePrincipalConnection.TenantId `
        -ApplicationId $servicePrincipalConnection.ApplicationId `
        -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint 


    $null = Select-AzureRmSubscription -SubscriptionId $servicePrincipalConnection.SubscriptionID
    Get-AzureRmVM | Select Name
    $dcred = Get-AutomationPSCredential -Name 'jasonvm'
    Write-Output $DomainCred
    $opts = New-PSSession -ConnectionUri 'https://52.185.148.177:5986' -Credential $dcred -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck)
    Invoke-Command -Session $opts -ScriptBlock {Get-Process}

}
catch {
    if (!$servicePrincipalConnection)
    {
        $ErrorMessage = "Connection $connectionName not found."
        throw $ErrorMessage
    } else{
        Write-Error -Message $_.Exception
        throw $_.Exception
    }
}

这是我的结果:

【讨论】:

  • 不使用“-ConnectionUri '52.185.148.177:5986'”,我们可以使用VM Name来获取它吗?我试过了,但没有为我工作。
猜你喜欢
  • 2017-01-07
  • 1970-01-01
  • 1970-01-01
  • 2020-07-24
  • 1970-01-01
  • 2020-04-22
  • 2021-03-08
  • 2019-05-05
相关资源
最近更新 更多