问题描述:当您将参数创建为对象并传递给VM-Windows-PS时,一切都很好,您可以使用name = value,但是在linux VM上不是这样,您只能得到值的顺序未知。
准备工作
:代码:
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
尝试:
$Script_paramz = @{"lgs" = "ARB"; "Event"="1001"; "LogName"="System" }
[标准输出] 测试:ARB系统1001
$Script_paramz = @{"lgs" = "SARB"; "Event"="a1001"; "LogName"="xSystem" }
[标准输出] 测试:SARB xSystem a1001
$Script_paramz = @{"lgs" = "1SARB"; "Event"="C1001"; "LogName"="bSystem" }
[标准输出] 测试:1SARB bSystem C1001
$Script_paramz = @{"par1" = "val1"; "par2"="val2"; "par3"="val3"; "par4"="val4"; "par5"="val5"; "par6"="val6" }
[标准输出] 测试:val3 val6 val1 val2 val4 val5
Powershell输出:$ Script_paramz
Name Value
---- -----
par3 val3
par6 val6
par1 val1
par2 val2
par4 val4
par5 val5
有人对此有解决方案吗?
编辑:回答我如何解决此问题,仍然想知道是否有可能正确看到参数名称。
$ 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
答案 0 :(得分:0)
$ Script_paramz 是一个哈希表。默认情况下,HashTable实现中不保留顺序。您可以尝试的一种方法是使用Powershell HashTable中的OrderedDictionary。
$Script_paramz = [ordered]@{"par1" = "val1"; "par2"="val2"; "par3"="val3"; "par4"="val4"; "par5"="val5"; "par6"="val6" }
因此,PowerShell中的结果将是:
PS C:\Users\fielu> echo $Script_paramz
Name Value
---- -----
par1 val1
par2 val2
par3 val3
par4 val4
par5 val5
par6 val6