I have a simple script such that
Param(
[string] $test
)
Write-Output "Hello $test"
and when I run it from my pipeline, this is the command I am running:
az vm run-command invoke --command-id RunPowerShellScript --name my-vm-win -g myRG --scripts "C:test.ps1" --parameters test=Peter
My output:
{
"code": "ComponentStatus/StdOut/succeeded",
"displayStatus": "Provisioning succeeded",
"level": "Info",
"message": "Hello n",
"time": null
}
Clearly, I am not able to pass the parameter test and this is stopping me from going forward. I have tried the suggestion in this question as you can see but it is not working.
2
Answers
I have reproduced in my environment and I have got expected results as below and I followed Microsoft-Document:
In VM i have stored this ps1 file:
test.ps1 :
Now run the below command:
Output:
Here in scripts parameter give the string you want to pass as an argument as I did and you will get output as I got.
[Here rithwik in command is argument 1 and bojja is the argument 2.]
The parameters are working, but the way you are using them is not. It probably doesn’t help that they use the same parameter to allow you to pass in a file or pass in a powershell code snippet.
When using the command you ran above are basically running the PowerShell in between the quotes. Which is open file c:test.ps1 on the vm you are connecting to without parameters.
If you were to run the command like the following you’d see that the parameters are passed into the snippet between the quotes. In this case I’m capturing the parameters sent to the script between the quotes and not to the file directly until I pass it inline to your file on the VM.
Normally you’d run your snippet using the file that’s on the computer you are running it from like the following, and in this case the parameters are passed directly to the file. Note the @ character before the file name indicating that you’re using the file on your local client and not the VM.
Basically it would be the equivalent of using this for the script param where the contents of the local file are put in between the quotes like so.
For one more variation to show you what’s going on run this snippet and you’ll see that there are 2 arguments sent, one is "-test" and the other is "Peter".