在 Azure 自动化上执行 Invoke-AzureRmVMRunCommand 的问题
Issue on executing Invoke-AzureRmVMRunCommand on azure automation
尝试 运行 通过 azure 自动化 运行 在 azure vm 上自定义脚本 - 使用 Invoke-AzureRmVMRunCommand 的书籍。但是低于异常
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中,它不支持用户交互,所以我们无法在[=58=中使用它]书。如果你想获得输出,你可以使用类似 Write-Output
.
此外,我认为 wget "https://automationbackupstorage.blob.core.windows.net/scripts/$localmachineScript" -outfile $localmachineScript
不会在 运行 书中工作,如果你想将存储中的 blob 下载到 运行 书中,你的选择是使用 Get-AzStorageBlobContent
将 blob 下载到 运行book.
的临时文件夹 ($env:temp
)
注意:在您的脚本中,您使用旧的 AzureRM
模块命令,它已被弃用并且不会更新,在我的示例中,我使用新的Az
命令,我推荐你也用这个。
要解决此问题并运行您的命令正确,请按照以下步骤操作。
导航到门户中的自动化帐户 -> Modules
,确保您已经安装了 Az.Accounts
、Az.Storage
、Az.Compute
模块,如果没有,去 Browse Gallery
-> 搜索模块名称并安装。
在 powershell 运行book 中,使用下面的示例,它适用于我。如果您的脚本需要一些参数,只需传递它们即可。
$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 vm 上自定义脚本 - 使用 Invoke-AzureRmVMRunCommand 的书籍。但是低于异常
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中,它不支持用户交互,所以我们无法在[=58=中使用它]书。如果你想获得输出,你可以使用类似 Write-Output
.
此外,我认为 wget "https://automationbackupstorage.blob.core.windows.net/scripts/$localmachineScript" -outfile $localmachineScript
不会在 运行 书中工作,如果你想将存储中的 blob 下载到 运行 书中,你的选择是使用 Get-AzStorageBlobContent
将 blob 下载到 运行book.
$env:temp
)
注意:在您的脚本中,您使用旧的 AzureRM
模块命令,它已被弃用并且不会更新,在我的示例中,我使用新的Az
命令,我推荐你也用这个。
要解决此问题并运行您的命令正确,请按照以下步骤操作。
导航到门户中的自动化帐户 ->
Modules
,确保您已经安装了Az.Accounts
、Az.Storage
、Az.Compute
模块,如果没有,去Browse Gallery
-> 搜索模块名称并安装。在 powershell 运行book 中,使用下面的示例,它适用于我。如果您的脚本需要一些参数,只需传递它们即可。
$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