azure - 在 azure 自动化上执行 Invoke-AzureRmVMRunCommand 时出现问题

标签 azure azure-powershell azure-automation

尝试使用 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

最佳答案

好吧,我可以在我这边重现您的问题。

enter image description here

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

enter image description here

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

注意:在您的脚本中,您使用旧的 AzureRM 模块命令,它已被弃用并且不会更新,在我的示例中,我使用新的 Az 命令,我建议您也使用这个。

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

  1. 导航到门户中的自动化帐户 -> 模块,确保您已安装 Az.AccountsAz.Storage code>、Az.Compute 模块,如果没有,请转到浏览库 -> 搜索模块名称并安装。

    enter image description here

  2. 在 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
    

enter image description here

关于azure - 在 azure 自动化上执行 Invoke-AzureRmVMRunCommand 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62250778/

相关文章:

c# - 如何将 ASP.NET Web API 请求正文记录到 Application Insights 失败中?

powershell - 使用 powershell 删除 azure 存储帐户

azure - ARM 模板创建和发布 Azure Runbook

azure - 使用azure管理库时有没有办法缩小授权范围?

azure - 如何使用 PowerShell 将 nextmarker 参数用于 Azure 列表 blob Rest api

powershell - 检测脚本是否在 Azure 自动化中运行的正确方法是什么?

azure - CosmosDB SQL API : Delete element matching criteria via partial update

c# - [Azure 数据目录] : Read the data from Azure data catalog glossary Unauthorized error?

Azure 存储同步机制

error-handling - Powershell错误输出抑制(再次)