在Azure Automation上执行Invoke-AzureRmVMRunCommand的问题 [英] Issue on executing Invoke-AzureRmVMRunCommand on azure automation

查看:96
本文介绍了在Azure Automation上执行Invoke-AzureRmVMRunCommand的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用Invoke-AzureRmVMRunCommand通过azure自动化运行手册在azure vm上运行自定义脚本.但是要低于例外

Trying to run custom script on azure vm through azure automation run-books using Invoke-AzureRmVMRunCommand. But getting below exception

InvokeAzureRmVMRunCommand开始使用ParameterSet处理 'DefaultParameter'.使用帐号 'qwerqe-xxxxx-4fde-9f1a-3d4d92ed055c'... System.Management.Automation.Host.HostException:一个命令 提示用户失败,因为主机程序或命令类型 不支持用户交互.主机正在尝试请求 确认并显示以下消息:提示输入以下内容的命令 用户失败,因为主机程序或命令类型不正确 支持用户交互.主机正在尝试请求 确认并显示以下消息:您确定要 执行此操作?对目标执行调用"操作 "VM_Name".

InvokeAzureRmVMRunCommand begin processing with ParameterSet 'DefaultParameter'. using account id 'qwerqe-xxxxx-4fde-9f1a-3d4d92ed055c'... System.Management.Automation.Host.HostException: A command that prompts the user failed because the host program or the command type does not support user interaction. The host was attempting to request confirmation with the following message: A command that prompts the user failed because the host program or the command type does not support user interaction. The host was attempting to request confirmation with the following message: Are you sure you want to perform this action? Performing the operation "Invoke" on target "VM_Name".

脚本:

# Get the connection "AzureRunAsConnection "

 $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName         

  $login =  Add-AzureRmAccount `
        -ServicePrincipal `
        -TenantId $servicePrincipalConnection.TenantId `
        -ApplicationId $servicePrincipalConnection.ApplicationId `
        -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint 


$rgname = 'RG-Name'
$vmname = 'VM-Name'
$localmachineScript = 'PowerShell script file on your local machine like script-test.ps1'
wget "https://automationbackupstorage.blob.core.windows.net/scripts/$localmachineScript" -outfile $localmachineScript 
Invoke-AzureRmVMRunCommand -ResourceGroupName $rgname -Name $vmname -CommandId 'RunPowerShellScript' -ScriptPath $localmachineScript -Parameter @{"arg1" = "var1";"arg2" = "var2"} -Debug

推荐答案

好,我可以站在您这边重现您的问题.

Well, I can reproduce your issue on my side.

该问题是由-Debug引起的,它将促使您确认操作,但是在Azure Runbook中,它不支持用户交互,因此我们无法在Runbook中使用它.如果要获得输出,可以使用Write-Output之类的东西.

The issue was caused by the -Debug, it will promote you to confirm the action, but in Azure Runbook, it does not support the user interaction, so we could not use it in runbook. If you want to get the output, you could use something like Write-Output.

此外,我认为wget "https://automationbackupstorage.blob.core.windows.net/scripts/$localmachineScript" -outfile $localmachineScript在runbook中不起作用,如果要将存储中的Blob下载到Runbook,您的选择是使用Get-AzStorageBlobContent将Blob下载到temp文件夹($env:temp).

Also, I don't think wget "https://automationbackupstorage.blob.core.windows.net/scripts/$localmachineScript" -outfile $localmachineScript will work in runbook, if you want to download the blob in the storage to the runbook, your option is to use Get-AzStorageBlobContent to download the blob to the temp folder($env:temp) of runbook.

注意:在您的脚本中,您使用了旧的AzureRM模块命令,该命令已被弃用,不会被更新,在我的示例中,我使用了新的

Note: In your script, you use the old AzureRM module commands, it was deprecated and will not be updated, in my sample, I use the new Az commands, I recommend you to also use this.

要解决此问题并正确运行命令,请按照以下步骤操作.

To fix the issue and run your command correctly, please follow the steps below.

  1. 导航至门户中的自动化帐户-> Modules,确保已安装Az.AccountsAz.StorageAz.Compute模块,如果没有,请转到Browse Gallery->搜索模块名称并安装.

  1. Navigate to the automation account in the portal -> Modules, make sure you have installed the Az.Accounts, Az.Storage, Az.Compute moudles, if not, go to the Browse Gallery -> search for the module name and install.

在powershell Runbook中,使用如下所示的示例,它对我有用.如果您的脚本需要一些参数,只需传递它们即可.

In the powershell runbook, use the sample like below, it works for me. If your script needs some parameters, just pass them.

$connectionName = "AzureRunAsConnection"
$servicePrincipalConnection=Get-AutomationConnection -Name $connectionName         

$login =  Connect-AzAccount `
        -ServicePrincipal `
        -TenantId $servicePrincipalConnection.TenantId `
        -ApplicationId $servicePrincipalConnection.ApplicationId `
        -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint 

$localmachineScript = "testrun.ps1"
$context = New-AzStorageContext -StorageAccountName "<StorageAccountName>" -StorageAccountKey "<StorageAccountKey>"
Get-AzStorageBlobContent -Container "<container-name>" -Blob $localmachineScript -Context $context -Destination $env:temp -Force

$result = Invoke-AzVMRunCommand -ResourceGroupName <group-name> -VMName <vm-name> -CommandId 'RunPowerShellScript' -ScriptPath "$env:temp\$localmachineScript"
Write-Output "The result:" $result.Value[0].Message

这篇关于在Azure Automation上执行Invoke-AzureRmVMRunCommand的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆