将哈希表传递给 PowerShell Runbook

Passing HashTable to PowerShell Runbook

我想,相关帖子我都看了,还是做不出来。 我有一个 Azure Runbook,需要将 HashTable 参数传递给配置脚本。这稍后由 Apply-PnPTemplate 函数使用。

参数在脚本中声明为

[System.Collections.Hashtable] $Parameters = @{}

不过我也试过了

[Object] $Parameters = @{}

我尝试测试我的脚本,添加 @{"customercode"="TEST"} 作为参数,但我收到此错误消息:

Cannot convert the "@{"customercode"="TEST"}" value of type "System.String" to type "System.Collections.Hashtable".

我尝试过的方法:使用和不使用 @,将分隔符更改为 ;;(我也需要在 PowerApps 中使用它)和 ,、none他们帮助了。请告知,将此对象传递给脚本的正确方法是什么。

试试这个;

[System.Collections.Hashtable] $Parameters = @{}
$Parameters.add("customercode","TEST")

我遇到过同样的问题,接缝总是将输入视为字符串。

我不知道原因,但这里有一个解决方法:传递一个假哈希表(字符串类型),然后在 runbook 中,将字符串转换为哈希表。

演示代码如下,希望对您有所帮助。

param([string]$mystr)

$mystr = $mystr.replace("=",":")

# pass the string to hashtable

$Parameters = @{}
$jsonobj = $mystr | ConvertFrom-Json
foreach($p in $jsonobj.psobject.properties){$Parameters[$p.name] = $p.value}

#print the keys

write-output "**the keys**"
$Parameters.keys

write-output "**the values**"

#print the values
$Parameters.values

我传递的参数:{"name"="jack","age"="11","city"="ddd"}

测试结果: