Invoke-AzVmRunCommand on linux VM passing parameters in wrong order

Viewed 582

I am using invoke-AzVmRunCommand to run a bash script on RHEL7 linux machine. My bash script is expecting more than 10 arguments in the correct order.

I have used parameters as per example in Run script using Invoke-AzVMRunCommand in VM? Passing parameter problem but still parameters are going in wrong orders.

$params = [ordered]@{"param1"="10.1.1";"param2"="Dev";"param3"="Development";"param4"="03";"param5"="172.30.0.27";"param6"="172.30.0.28";"param7"="01"}               

Invoke-AzVMRunCommand -ResourceGroupName "$(resourceGroupName)" -VMName "${vmName}" -CommandId "RunShellScript" -ScriptPath "$(System.ArtifactsDirectory)/scripts/bash.sh" `
    -Parameter $params

bash.sh:

#!/bin/bash

echo $@

I am expecting output as 10.1.1. Dv Development 03... etc however I am getting 03 dev 10.1.1 and other params.

2 Answers

This is a workaround and potential solution which is working well:

Update bash.sh to use flags for the arguments

#!/bin/bash

echo $@

while getups ":a:b:c:d:e" opted
 case $opt in
   a) version="OPTARG"
   ;;
   b) env="OPTARG"
   ;;
   c) team="OPTARG"
   ;;
   d) item_count="OPTARG"
   ;;
   e) ip_addr="OPTARG"
   ;;
   \?) echo "invalid options -$OPTARG" >&2
    ;;
 esac
done

and pass parameters as

$params = [ordered]@{"param1"="-a `"10.1.1`"";"param2"="-b `"Dev`"";"param3"="-c `"Development`"";"param4"="-d `"03`"";"param5"="-e `"172.30.0.27`""}               

Invoke-AzVMRunCommand -ResourceGroupName "$(resourceGroupName)" -VMName "${vmName}" -CommandId "RunShellScript" -ScriptPath "$(System.ArtifactsDirectory)/scripts/bash.sh" `
    -Parameter $params

Please use arg as a parameter instead of param and in reverese order:

$params = [ordered]@{"arg7"="10.1.1";"arg6"="Dev";"arg5"="Development";"arg4" ="03";"arg3"="172.30.0.27";"arg2"="172.30.0.28";"arg1"="01"}

It should work.

Related