尝试使用 Invoke-AzureRmVMRunCommand 通过 azure 自动化运行手册在 azure vm 上运行自定义脚本。但低于异常
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
最佳答案
好吧,我可以在我这边重现您的问题。
该问题是由-Debug
引起的,它会提示您确认操作,但在Azure Runbook中,它不支持用户交互,因此我们无法在Runbook中使用它。如果你想获得输出,你可以使用类似Write-Output
的东西。
此外,如果您愿意,我认为 wget "https://automationbackupstorage.blob.core.windows.net/scripts/$localmachineScript"-outfile $localmachineScript
不会在 Runbook 中工作要将存储中的 Blob 下载到 Runbook,您可以选择使用 Get-AzStorageBlobContent
将 Blob 下载到 Runbook 的临时文件夹 ($env:temp
)。
注意:在您的脚本中,您使用旧的 AzureRM
模块命令,它已被弃用并且不会更新,在我的示例中,我使用新的 Az
命令,我建议您也使用这个。
要解决该问题并正确运行命令,请按照以下步骤操作。
导航到门户中的自动化帐户 ->
模块
,确保您已安装Az.Accounts
、Az.Storage
code>、Az.Compute
模块,如果没有,请转到浏览库
-> 搜索模块名称并安装。在 powershell runbook 中,使用如下示例,它对我有用。如果您的脚本需要一些参数,只需传递它们即可。
$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 - 在 azure 自动化上执行 Invoke-AzureRmVMRunCommand 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62250778/