在Azure VM上运行脚本

时间:2017-11-29 17:26:16

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

我尝试在Azure中的Windows VM上启用PSRemoting。虽然它已经工作了一段时间,但从上周开始,这个剧本就不再适用了。

我使用自定义脚本扩展功能运行脚本:

Set-AzureRmVMCustomScriptExtension -ResourceGroupName $ResourceGroupName -VMName $VMName -Name "EnableWinRM_HTTP0" -Location $vm.Location -StorageAccountName $storageaccountname -StorageAccountKey $key -FileName "ConfigureWinRM_HTTP1.ps1" -ContainerName "scripts" -RunFile "ConfigureWinRM_HTTP1.ps1"

ConfigureWinRM_HTTP1.ps1脚本如下所述:

    # Ensure PS remoting is enabled, although this is enabled by default for Azure VMs
    Enable-PSRemoting -Force

    # Create rule in Windows Firewall
    New-NetFirewallRule -Name "WinRM HTTP" -DisplayName "WinRM HTTP" -Enabled True -Profile Any -Action Allow -Direction Inbound -LocalPort 5985 -Protocol TCP

    # Run WinRM configuration on command line.
    $cmd = "winrm create winrm/config/Listener?Address=*+Transport=HTTP"
    cmd.exe /C $cmd

之前的某些行在同一个脚本中成功创建了虚拟机,但是当执行此脚本的部分时,我收到以下错误:

Set-AzureRmVMCustomScriptExtension : Long running operation failed with status 'Failed'. Additional Info:'VM has
reported a failure when processing extension 'EnableWinRM_HTTP0'. Error message: "Finished executing command".'
ErrorCode: VMExtensionProvisioningError
ErrorMessage: VM has reported a failure when processing extension 'EnableWinRM_HTTP0'. Error message: "Finished
executing command".
StartTime: 29/11/2017 15:07:24
EndTime: 29/11/2017 15:08:14
OperationID: aa418b4a-76b4-4482-93eb-16b734009388
Status: Failed
At C:\.....\SetupVM.ps1:107 char:2
+     Set-AzureRmVMCustomScriptExtension -ResourceGroupName $ResourceGr ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Set-AzureRmVMCustomScriptExtension], ComputeCloudException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Compute.SetAzureVMCustomScriptExtensionCommand

Azure管理门户中的监视器不显示任何错误消息。

为什么它不起作用?

1 个答案:

答案 0 :(得分:1)

很高兴听到您的问题已得到解决。

我将其添加到答案中,也许它会帮助其他社区成员获得与您相同的错误。

在Windows PowerShell 3.0中,Enable-PSRemoting cmdlet可以启用Windows PowerShell winrm。

启用-PSRemoting cmdlet执行以下操作:
1.运行Set-WSManQuickConfig cmdlet,执行以下任务

启动WinRM服务。
将WinRM服务上的启动类型设置为“自动” 创建一个侦听器以接受任何IP地址上的请求 为WS-Management通信启用防火墙例外 注册Microsoft.PowerShell和Microsoft.PowerShell.Workflow会话配置(如果它们尚未注册)。 在64位计算机上注册Microsoft.PowerShell32会话配置(如果尚未注册) 启用所有会话配置 更改所有会话配置的安全描述符以允许远程访问。

2.重新启动WinRM服务,使前面的更改生效。

希望这会有所帮助:)

相关问题