Set-AzVMRunCommand 成功,但 Invoke-AzVMRunCommand 因未知原因失败

问题描述 投票:0回答:1

我可以使用

Set-AzVMRunCommand
在 Azure VM 上(成功)创建命令。我想知道如何调用它(多次)?

Set-AzVMRunCommand -ResourceGroupName $resourceGroup -VMName $vmName -Location $location -RunCommandName "foobar" -ScriptLocalPath ".\Run-App.ps1" -RunAsUser $vmUser -RunAsPassword $vmPass

我看到我的命令已成功创建:

>> az vm run-command list --resource-group "hvtt1" --vm-name "testvm"

[
  {
    "asyncExecution": false,
    "errorBlobManagedIdentity": null,
    "errorBlobUri": null,
    "id": "/subscriptions/c1097bf0-4f64-4ee0-b922-7dd05a5965fe/resourceGroups/hvtt1/providers/Microsoft.Compute/virtualMachines/testvm/runCommands/foobar",
    "instanceView": null,
    "location": "eastus2",
    "name": "foobar",
    "outputBlobManagedIdentity": null,
    "outputBlobUri": null,
    "parameters": null,
    "protectedParameters": null,
    "provisioningState": "Succeeded",
    "resourceGroup": "hvtt1",
    "runAsPassword": null,
    "runAsUser": "user",
    "source": {
      "commandId": null,
      "script": "Write-Host \"Finished script initialization\";",
      "scriptUri": null,
      "scriptUriManagedIdentity": null
    },
    "tags": null,
    "timeoutInSeconds": 0,
    "treatFailureAsDeploymentFailure": null,
    "type": "Microsoft.Compute/virtualMachines/runCommands"
  }
]

但是当我使用命令的 ARM ID 作为 $id 时,我无法调用它

Invoke-AzVMRunCommand         `
  -ResourceGroupName $resourceGroup        `
  -VMName $vmName                          `
  -CommandId $id -Verbose

我收到错误:

VERBOSE: Performing the operation "Invoke" on target "testvm".
Invoke-AzVMRunCommand : The entity was not found in this Azure location.
ErrorCode: NotFound
ErrorMessage: The entity was not found in this Azure location.
ErrorTarget: 
StatusCode: 404
ReasonPhrase: 
OperationID : 6ae3c2f5-7328-4de1-b1d2-a78d85576351
At line:12 char:1
+ Invoke-AzVMRunCommand         `
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Invoke-AzVMRunCommand], ComputeCloudException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Compute.Automation.InvokeAzureRmVMRunCommand
azure azure-powershell azure-virtual-machine azure-cli
1个回答
0
投票

您可以选择使用 Invoke-AzVMRunCommand 执行命令,如下所示:

$script = "Get-Services"
$vm = (Get-AzVm -status | Where-Object {$_.Name -eq "TestVM"})

Invoke-AzVMRunCommand -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name -CommandId 'RunShellScript' -ScriptString $script

如果您希望在所有虚拟机上执行命令,您可以使用 for 循环,例如;“

$script = "Get-Services"
$vms = (Get-AzVm -status | Where-Object {$_.PowerState -eq "VM running"})

foreach ($vm in $vms) {
    Invoke-AzVMRunCommand -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name -CommandId 'RunShellScript' -ScriptString $script
}
© www.soinside.com 2019 - 2024. All rights reserved.