0

I'm trying to run a script using either PowerShell AZ or AZ Cli. The script is located in an Azure Blob Storage, it is supposed to download a zip file on the server, unzip it and launch the installation. The URL if the Zip file is provided as parameter of the script (this url contains a SAS token).

Everything works perfectly fine when using the "Run Command" blade of Azure Web Portal.

I now want to industrialize the actions a little bit better with a script that creates a SAS token and pass the parameters directly to initiate run commands.

I initially tried with Set-AzVMRunCommand, but had an issue to pass parameters: whatever the syntax used, the parameter value is never passed. The best I could achieve was to pass -Parameter @{name="URL";value="$ZipURI"} where I can see the "URL" parameter with Get-AzVMRunCommand but the value seems to never arrive.

I then tried with az cli as it seemed to solve the problem for many, but it does not look like az cli understands the --script-uri when it contains a SAS token, as I get errors like that:

'se' is not recognized as an internal or external command, operable program or batch file.

Edit: Many thanks for your answers. Here are the full commands, I hope that helps, do not hesitate to ask me for more details.

Set-AzVMRunCommand -ResourceGroupName $ResourceGroupName -VMName $VirtualMachineName -RunCommandName "Deploy" -SourceScriptUri $SourceScriptURI -Location $Location -Parameter $Parameters

$SourceScriptURI is an url generated with the script name and SAS token $Parameters = @{name="URL";value="$ZipURI"}

Command is similar in AZ Cli

az vm run-command create --run-command-name "TaniumInstall" --vm-name $VirtualMachineName --resource-group $ResourceGroupName --script-uri $SourceScriptURI -protected-parameters URL=$ZipURI

Has anyone ever achieved this?

Thanks.

Rodolphe Beck
  • 363
  • 1
  • 5
  • 14

1 Answers1

0

I initially tried with Set-AzVMRunCommand, but had an issue to pass parameters: whatever the syntax used, the parameter value is never passed. The best I could achieve was to pass -Parameter @{name="URL";value="$ZipURI"} where I can see the "URL" parameter with Get-AzVMRunCommand but the value seems to never arrive

To run the AzVMRunCommand Invoke command with parameter values and execute the script in a VM.

  1. Upload the powershell script in VM

Note: The script path parameter will only accept a path inside the VM, not a blob path.

Sample.ps1

    param (
        [string] $arg1
    )
    $folder = New-Item C:\Folder11\venkatscript.txt -Type File
    $outputMessage = "This is a sample script with parameters $arg1" 
    $outputMessage += "File has been created in VM Path $folder"
    Write-Output $outputMessage
  1. Execute the below command.
 az vm run-command invoke  --command-id RunPowerShellScript --name "Venkat" -g "Sri" --scripts "C:\Folder11\sample.ps1  Venkat"

Output:

enter image description here

Pass the value you want to pass an argument.

When I check in the VM, the script is executed, and the folder is also created.

enter image description here

Reference: Executing Powershell script on Remote VM on Azure with Parameters - Stack Overflow

Venkat V
  • 2,197
  • 1
  • 1
  • 10
  • Hi, I use -SourceScriptUri for PowerShellAZ or --script-uri for az CLI, which both are documented as parameters to pass a script in a blob storage. – Rodolphe Beck Jul 28 '23 at 12:26
  • Microsoft --parameters argument is not working, same is already verified. Refer same parameter Qns in [Stack](https://stackoverflow.com/questions/75326154/executing-powershell-script-on-remote-vm-on-azure-with-parameters/75331629#75331629) – Venkat V Jul 28 '23 at 12:39