1

Problem description: When you create parameters as object and pass to VM-Windows-PS all is good you can have name=value, but on linux VM that is not the case, you get only values in unknown order.

Preparation:

  • Source: Your PC: Windows: Login Azure in powershell.
  • Target: Azure: Have Linux VM, in my case ubuntu18.

Code:

echo '#!/bin/bash' >>.\msgtst.sh
echo 'echo test: $1 $2 $3 $4 $5 $6' >>.\msgtst.sh
$Script_paramz = @{"lgs" = "SARB"; "Event"="a1001"; "LogName"="xSystem" }
Invoke-AzVMRunCommand -ResourceGroupName VMResourceGroup -VMName LinuxVMName -ScriptPath ".\msgtst.sh" -CommandId RunShellScript -Parameter $Script_paramz -Verbose

Tries:

$Script_paramz = @{"lgs" = "ARB"; "Event"="1001"; "LogName"="System" }

[stdout] test: ARB System 1001

$Script_paramz = @{"lgs" = "SARB"; "Event"="a1001"; "LogName"="xSystem" }

[stdout] test: SARB xSystem a1001

$Script_paramz = @{"lgs" = "1SARB"; "Event"="C1001"; "LogName"="bSystem" }

[stdout] test: 1SARB bSystem C1001

$Script_paramz = @{"par1" = "val1"; "par2"="val2"; "par3"="val3"; "par4"="val4"; "par5"="val5"; "par6"="val6" }

[stdout] test: val3 val6 val1 val2 val4 val5

Powershell output: $Script_paramz

Name                           Value
----                           -----
par3                           val3
par6                           val6
par1                           val1
par2                           val2
par4                           val4
par5                           val5

Does anyone have a solution to this?

Edit: answer how i solved this, still would like to know if there is possibility to see parameter names correctly.

$Script_paramz = @{"par1" = "par1=val1"; "par2"="par2=val2"; "par3"="par3=val3"}

#!/bin/bash
par2=''; par1=''; par3='';
for var in "$@"
do
   IFS='=' read -r -a array <<< "$var"
   export "${array[0]}"="${array[1]}"
done
echo $par1, $par2, $par3

[stdout] val1, val2, val3

5
  • Please provide output of echo test: "$@" instead of echo test: $1 $2 $3 $4 $5 $6 Commented Dec 14, 2019 at 12:56
  • echo test: $@ $* Commented Dec 15, 2019 at 12:18
  • test: val6 val2 val5 val1 val4 val3 val6 val2 val5 val1 val4 val3 Commented Dec 15, 2019 at 12:18
  • i could ofcource make value as "par1" = "par1=val1" pair, but it means i would need to use exception for linux script, i am using this for windows also, no big deal but want to understand how this works Commented Dec 15, 2019 at 12:24
  • is it night sleep or actually your suggestion to use $@ led me to solution of parsing parameters, if anyone knows how to get parameter names from powershell i would like to hear. Commented Dec 15, 2019 at 14:36

1 Answer 1

1

$Script_paramz is a HashTable. Order is not kept by default in HashTable implementation. One approach you can try is to use OrderedDictionary from Powershell HashTable.

$Script_paramz = [ordered]@{"par1" = "val1"; "par2"="val2"; "par3"="val3"; "par4"="val4"; "par5"="val5"; "par6"="val6" }

So the result in powershell will be:

PS C:\Users\fielu> echo $Script_paramz 

Name                           Value
----                           -----
par1                           val1
par2                           val2
par3                           val3
par4                           val4
par5                           val5
par6                           val6
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, i have tried to order it by other means, this helps, but i want to match name=value somehow, if noone gives a solution then i will mark this as answer.
@DaliusV did you get this to work in the end? I am getting same issue where parameters from Powershell to bash is going in wrong order

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.