skip to Main Content

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


  1. 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 :

    param(
         [string]$arg1,
         [string]$arg2
      )
    Write-Host This is a sample script with parameters $arg1 and $arg2
    

    enter image description here

    Now run the below command:

    az vm run-command invoke  --command-id RunPowerShellScript --name "vm-name" -g "resourcegrp-name" --scripts "C:test.ps1  rithwik bojja"     
    
    
     
    

    Output:

    enter image description here

    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.]

    Login or Signup to reply.
  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.

    az vm run-command invoke --command-id RunPowerShellScript --name my-vm-win -g myRG --scripts "C:test.ps1" --parameters test=Peter
    

    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.

    az vm run-command invoke --command-id RunPowerShellScript --name my-vm-win -g myRG --scripts "Param([string] $test); C:Test.ps1 -test $test" --parameters test=Peter
    

    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.

    az vm run-command invoke --command-id RunPowerShellScript --name my-vm-win -g myRG --scripts @C:Test.ps1 --parameters test=Peter
    

    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.

    --scripts "Param([string] $test) Write-Output "Hello $test""
    

    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".

    az vm run-command invoke --command-id RunPowerShellScript --name my-vm-win -g myRG --scripts "$args" --parameters test=Peter
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search