【问题标题】:Passing HashTable to PowerShell Runbook将 HashTable 传递给 PowerShell Runbook
【发布时间】:2018-11-27 11:59:32
【问题描述】:

我想,我阅读了所有相关的帖子,仍然无法完成这项工作。 我有一个 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 中使用它)和,,它们都没有帮助。请告知,将此对象传递给脚本的正确方法是什么。

【问题讨论】:

    标签: azure-powershell azure-automation


    【解决方案1】:

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

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

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

    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"}

    测试结果:

    【讨论】:

      【解决方案2】:

      试试这个;

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

      【讨论】:

        猜你喜欢
        • 2021-07-27
        • 1970-01-01
        • 2022-12-07
        • 2013-06-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-01
        • 2019-02-09
        相关资源
        最近更新 更多