az vm run-command invoke ignoring credentials

Shane Walford 1 Reputation point
2021-03-09T14:58:32.2+00:00

Hi

I am trying to use the az vm run-command invoke command but i am having issues with the credentials.

Below is the script i am trying to use. The script works if it is run from the machine itself but when invoking remotely it gets stuck in a loop and times out after 90 minutes I think. I can invoke similar scripts that do not require credentials, so I am guessing its the creds causing this.

The account being used has the correct permissions as i can run the script on the VM

Does anyone have any ideas on why it doesnt like the credentials?

$User = "TestUser"
$PWord = ConvertTo-SecureString -String "TestPassword" -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord

az vm run-command invoke  --command-id RunPowerShellScript --resource-group MyRG --name Myserver --scripts  'param([string]$Credentials) Remove-Computer -UnjoinDomaincredential $Credentials --PassThru -Verbose -Restart -Force' --parameters "Credentials=$Credential"
Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,119 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. deherman-MSFT 33,296 Reputation points Microsoft Employee
    2021-03-10T17:20:49.873+00:00

    @Shane Walford
    According to the example supplied here, you are missing closing and opening single quotes. Please give this a try and see if it resolves your issue.

    az vm run-command invoke  --command-id RunPowerShellScript --resource-group MyRG --name Myserver --scripts  'param([string]$Credentials)' 'Remove-Computer -UnjoinDomaincredential $Credentials --PassThru -Verbose -Restart -Force' --parameters "Credentials=$Credential"  
    

    Please don’t forget to "Accept the answer" and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.


  2. Shane Walford 1 Reputation point
    2021-03-11T12:10:20.017+00:00

    I am wondering if the issue i am seeing is below

    76783-run-command.png

    0 comments No comments