Azure Powershell Runbook-在远程VM(ARM又名V2)上调用命令



我需要什么

我想要一个在远程VM上执行命令的自动化runbook(VM是V2或"资源管理器"VM)。

我找到了使其适用于经典虚拟机的示例,但我无法使其适用于RM虚拟机(我发现最好的示例是:https://alexandrebrisebois.wordpress.com/2015/08/14/azure-automation-remote-powershell-and-a-virtual-machine/)。

有人在自动化运行手册中有在远程V2虚拟机上运行powershell命令的例子吗?

我当前被困的位置

我试图调整示例代码的第二部分(调用命令的部分),但我得到了以下错误:

[vm-template] Connecting to remote server vm-template failed with the following error 
message : The WinRM client cannot process the request. If the authentication scheme is 
different from Kerberos, or if the client computer is not joined to a domain, then HTTPS 
transport must be used or the destination machine must be added to the TrustedHosts 
configuration setting. Use winrm.cmd to configure TrustedHosts. Note that computers in the 
TrustedHosts list might not be authenticated. You can get more information about that by
running the following command: winrm help config. For more information, see the 
about_Remote_Troubleshooting Help topic.
+ CategoryInfo          : OpenError: (vm-template:String) [], PSRemotingTransportException
+ FullyQualifiedErrorId : ServerNotTrusted,PSSessionStateBroken

我的理解是,由于我不使用Kerberos(甚至不知道那是什么),所以我必须使用HTTPS。为此,我必须完成示例代码的前半部分,这是关于导入证书的(导入where btw,因为runbook运行"in azure"?)。

我发现一些页面解释了如何启用HTTPS(从PowerShell使用WinRM连接到远程服务器失败)并创建证书(http://www.jayway.com/2011/11/21/winrm-w-self-signed-certificate-in-4-steps/)但它们需要在两台机器上运行一些命令;我当然可以在远程虚拟机上运行命令,但我不明白如何在客户端上运行,因为runbook直接在azure中运行,所以客户端并不存在。

非常感谢您的帮助,谢谢!

如果使用https,您的网络安全组是否配置为打开端口5985(winrmhttp端口)或5986?如果你计划使用winrm而不是Azure自动化,你可能还需要一个公共IP。您还应该能够使用http,所以我认为您看到的错误是一个常见的连接失败错误。

注意:默认情况下,应该在您的机器上设置winrmoverhttp和侦听器并进行侦听。winrm使用消息级加密,因此它不完全是明文。您可以使用进行验证

winrm e winrm/config/listener

它应该向你的听众展示这样的东西:

Listener [Source="GPO"]
    Address = *
    Transport = HTTP
    Port = 5985
    Hostname
    Enabled = true
    URLPrefix = wsman
    CertificateThumbprint
    ListeningOn = 1.1.1.1

一旦你验证了这一点,我会验证你是否可以从自己的计算机上使用winrm连接到远程机器。你可以很容易地做到这一点:

$username = '<admin-user>'
$pass = ConvertTo-SecureString -string '<password>' -AsPlainText -Force
$cred = New-Object -typename System.Management.Automation.PSCredential -argumentlist $username, $pass
Enter-PSSession -ComputerName <public-IP> -Credential $cred -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck)

请注意,您可能必须在自己的计算机上设置受信任的主机,以信任Azure计算机来创建winrm会话。这可以通过以下方式完成:Set-Item WSMan:localhostClientTrustedHosts -value * -Force

请注意,为了安全起见,您应该使用Azure虚拟机的实际名称,而不是通配符。

相关内容

最新更新