问题
Trying to run custom script on azure vm through azure automation run-books using Invoke-AzureRmVMRunCommand. But getting below exception
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".
Script:
# 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
回答1:
Well, I can reproduce your issue on my side.
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
.
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.
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.
Navigate to the automation account in the portal ->
Modules
, make sure you have installed theAz.Accounts
,Az.Storage
,Az.Compute
moudles, if not, go to theBrowse Gallery
-> search for the module name and install.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
来源:https://stackoverflow.com/questions/62250778/issue-on-executing-invoke-azurermvmruncommand-on-azure-automation