【发布时间】:2019-12-20 13:13:19
【问题描述】:
我的脚本接受两个参数,一个字符串arg1 和一个哈希表arg2:
Script.ps1
Param(
[string]$arg1,
[hashtable]$arg2,
)
Write-Host "@$(ConvertTo-Json $arg2)"
如何将字典从 powershell 传递到此脚本,方法是调用它以使其在单独的进程中运行。
Call.ps1
$d = @{a="bla"}
$s = "@$((ConvertTo-Json $a -Compress) -replace ':','=')"
powershell -File Script.ps1 "-arg2 $s" # We need another process (no access to the variables in this script)
我不知道我做错了什么(见question),但这不起作用。或者还有其他的
【问题讨论】:
-
可以把参数类型改成
string吗?那么在不“脏”的情况下与 JSON 相互转换会很容易。
标签: powershell hashtable