powershell json tmsl 脚本中的格式变量

powershell json format variable in tmsl script

我使用 powershell,我想将 json 格式的变量放入 TMSL 创建角色脚本。我想把这个变量放入 TablePermission 参数

json变量:

$filter=
[

    {
          "name": "DimGeography",
          "filterExpression": "DimGeography[CountryRegionCode] = \"US\" "
        }
]

TMSL 脚本:

$Query='
    {  
    "create": {  
        "parentObject": {  
        "database": "AW Internet Sales"  
        },  
        "role": {
      "name": "test_filter",
      "modelPermission": "read",
      "tablePermissions": "'+$filter+'"
    }
        }
          
    }'

这是我要参数化的脚本

{
  "createOrReplace": {
    "object": {
      "database": "AW Internet Sales",
      "role": "SalesManagerUS"
    },
    "role": {
      "name": "SalesManagerUS",
      "modelPermission": "read",
      "tablePermissions": [
        {
          "name": "DimGeography",
          "filterExpression": "DimGeography[CountryRegionCode] = \"US\" "
        }
      ]
    }
  }
}

如何将 $filter 放入 $Query 以接收上面的工作脚本?

您可以尝试以下代码段:

    $filter=
'[

    {
          "name": "DimGeography",
          "filterExpression": "DimGeography[CountryRegionCode] = \"US\" "
        }
]'


function populate-tsml ($filter)
{


$Query='
    {  
    "create": {  
        "parentObject": {  
        "database": "AW Internet Sales"  
        },  
        "role": {
      "name": "test_filter",
      "modelPermission": "read",
      "tablePermissions": '+$filter+'
    }
        }
          
    }'

    return $Query

    }

  $Query =   populate-tsml -filter $filter

输出:

谢谢! 没有必要定义一个函数,引号中的一点变化就足够了。 以下脚本有效:

$filter=
'[

    {
          "name": "DimGeography",
          "filterExpression": "DimGeography[CountryRegionCode] = \"US\" "
        }
]'




$Query='
    {  
    "create": {  
        "parentObject": {  
        "database": "AW Internet Sales"  
        },  
        "role": {
      "name": "test_filter",
      "modelPermission": "read",
      "tablePermissions": '+$filter+'
    }
        }
          
    }'