Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json Can';t运行powershall脚本_Json_Windows_Powershell - Fatal编程技术网

Json Can';t运行powershall脚本

Json Can';t运行powershall脚本,json,windows,powershell,Json,Windows,Powershell,我有这样一个powershell脚本: Param( [Parameter(Mandatory=$True)] [String] $NEWPARAMVALUE, [Parameter(Mandatory=$True)] [String] $PARAM_NAME ) $FILEPATH = “arm_template.json” $JSON = Get-Content $FILEPATH | Out-String | ConvertFrom-Jso

我有这样一个powershell脚本:

Param(
    [Parameter(Mandatory=$True)]
    [String]
    $NEWPARAMVALUE,

    [Parameter(Mandatory=$True)]
    [String]
    $PARAM_NAME
)
$FILEPATH = “arm_template.json”
$JSON = Get-Content $FILEPATH | Out-String | ConvertFrom-Json
$JSON.$PARAM_NAME = $NEWPARAMVALUE
$JSON | ConvertTo-Json -Depth 10 | Set-Content $FILEPATH
当我在没有$PARAM_NAME变量的情况下使用这个脚本时(我只是将一个参数作为纯文本,例如:
$JSON.resources.properties.parameters.daasServiceBaseUrl.defaultValue
),它起了作用

我尝试了不同的方法:

  • $JSON.resources.$PARAM_NAME
    (其中
    $PARAM_NAME='properties.parameters.daasServiceBaseUrl.defaultValue'

  • $JSON.“$PARAM_NAME”

  • 我想那是因为两个环境变量之间的点。请有人帮帮忙好吗

    更新:

    我从Azure DevOps获取的错误:

    财产 |无法使用“properties.parameters.parametername.defaultValue” |无法在此对象上找到。请验证该属性是否存在,以及 |可以设置

    我是如何手动尝试的:

    $env:PARAM_NAME="resources.properties.parameters.daasServiceBaseUrl.defaultValue"
    
    
    ./test.ps1 -NEWPARAMVALUE "[parameters('DaaS_Contract_Daily_Trigger_properties_Daas-UI-to-Contract_parameters_daasServiceBaseUrl')]" -PARAM_NAME $env:PARAM_NAME
    
    我得到的是:

    Line |
      12 |  $JSON.$PARAM_NAME = $NEWPARAMVALUE
         |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         | Exception setting "resources.properties.parameters.daasServiceBaseUrl.defaultValue": "The property
         | 'resources.properties.parameters.daasServiceBaseUrl.defaultValue' cannot be found on this object. Verify that the property exists and can be
         | set."
    
    我的JSON:

    {
        "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
        "contentVersion": "1.0.0.0",
        "parameters": {
            "dataFactoryName": {
                "type": "string",
                "defaultValue": ""
            },
            "location": {
                "type": "string",
                "defaultValue": "[resourceGroup().location]",
                "metadata": {
                    "description": "Data Factory location matches Resource group location"
                }
            },
            "project": {
                "type": "string",
                "defaultValue": "ct",
                "metadata": {
                    "description": "Azure Tag used to track projects"
                }
            },
            "environment": {
                "type": "string",
                "defaultValue": "default",
                "metadata": {
                    "description": "Azure Tag used to track environments"
                }
            },
            "sleepTime": {
                "type": "int",
                "defaultValue": "1"
            },
            "DaaS_Contract_Daily_Trigger_properties_Daas-UI-to-Contract_parameters_daasServiceBaseUrl": {
                "type": "string",
                "defaultValue": "http://digital-daas-service-om-sqa.nplabsusk8s.com/daas"
            }
        },
        "resources": [
            {
                "apiVersion": "2018-06-01",
                "name": "[parameters('dataFactoryName')]",
                "location": "[parameters('location')]",
                "tags": {
                    "project": "[parameters('project')]",
                    "environment": "[parameters('environment')]"
                },
                "type": "Microsoft.DataFactory/factories",
                "identity": {
                    "type": "SystemAssigned"
                },
                "properties": {}
            },
            {
                "name": "[concat(parameters('dataFactoryName'), '/pipeline1')]",
                "type": "Microsoft.DataFactory/factories/pipelines",
                "apiVersion": "2018-06-01",
                "properties": {
                    "activities": [
                        {
                            "name": "Wait1",
                            "type": "Wait",
                            "dependsOn": [],
                            "userProperties": [],
                            "typeProperties": {
                                "waitTimeInSeconds": "[parameters('sleepTime')]"
                            }
                        },
                        {
                            "name": "Copy Delivery Request",
                            "type": "Copy",
                            "dependsOn": [
                                {
                                    "activity": "Delete old Req File",
                                    "dependencyConditions": [
                                        "Succeeded"
                                    ]
                                }
                            ],
                            "policy": {
                                "timeout": "7.00:00:00",
                                "retry": 0,
                                "retryIntervalInSeconds": 30,
                                "secureOutput": false,
                                "secureInput": false
                            },
                            "userProperties": [],
                            "typeProperties": {
                                "source": {
                                    "type": "RestSource",
                                    "httpRequestTimeout": "00:01:40",
                                    "requestInterval": "00.00:00:00.010",
                                    "requestMethod": "GET"
                                },
                                "sink": {
                                    "type": "JsonSink",
                                    "storeSettings": {
                                        "type": "AzureBlobFSWriteSettings"
                                    },
                                    "formatSettings": {
                                        "type": "JsonWriteSettings",
                                        "quoteAllText": true
                                    }
                                },
                                "enableStaging": false
                            },
                            "inputs": [
                                {
                                    "referenceName": "Rest_Json_File",
                                    "type": "DatasetReference",
                                    "parameters": {
                                        "relativeURL": {
                                            "value": "@{pipeline().parameters.daasServiceRelURL}/@{variables('createdDate')}",
                                            "type": "Expression"
                                        },
                                        "BaseUrl": {
                                            "value": "@pipeline().parameters.daasServiceBaseUrl",
                                            "type": "Expression"
                                        }
                                    }
                                }
                            ]
                        }
                    ],
                    "parameters": {
                        "deliveryReqFileName": {
                            "type": "string",
                            "defaultValue": "delivery_request.json"
                        },
                        "daasServiceBaseUrl": {
                            "type": "string",
                            "defaultValue": "http://digital-daas-service-om-sqa.nplabsusk8s.com/daas"
                        }
                    },
                    "annotations": []
                },
                "dependsOn": []
            }
        ]
    }
    
    

    当您传递字符串
    “a.b.c.d”
    作为
    成员访问运算符的右操作数时,PowerShell将尝试解析确切的成员名称
    a.b.c.d
    ,而不是“
    a
    ,然后
    b
    ,然后
    c
    ,然后
    d

    虽然可以通过对
    “${JSON}.${PARAM_NAME}”
    执行
    调用表达式
    来解决这一问题,但这也会打开脚本,任意执行用户可能作为
    $PARAM_NAME
    传入的任何内容

    要避免这种情况,您需要手动执行以下操作:

    $env:PARAM_NAME="resources.properties.parameters.daasServiceBaseUrl.defaultValue"
    
    
    ./test.ps1 -NEWPARAMVALUE "[parameters('DaaS_Contract_Daily_Trigger_properties_Daas-UI-to-Contract_parameters_daasServiceBaseUrl')]" -PARAM_NAME $env:PARAM_NAME
    
  • $PARAM_NAME
    拆分为单个成员名称,如
    $NAME=$PARAM_NAME.Split('.')
    ,然后
  • 手动解析除最后一个以外的每个属性名称
  • 分配给
    $resolvedObject.“$last”
  • 下面是处理步骤2和3的通用帮助器函数中的情况:

    函数集属性链接
    {
    param(
    [参数(必需=$true,ValueFromPipeline=$true,位置=0)]
    [ValidateScript({$\不是[ValueType]})]
    [对象]$RootObject,
    [参数(必需=$true,位置=1)]
    [string[]]$MemberChain,
    [参数(必需=$true,位置=2)]
    [对象]$Value,
    [交换机]$PassThru
    )
    开始{
    $resolutionChain=@($MemberChain| Select-SkipLast 1)
    $terminalMember=$($MemberChain |选择-最后1个)
    }
    过程{
    $obj=$RootObject
    #直到最后一个属性的父级的成员解析
    foreach($resolutionChain中的memberName){
    编写Vebose“解析$obj上的$memberName”
    $obj=$obj.$memberName
    }
    #设置其父属性上最后一个属性的值
    编写Vebose“将$obj上的$memberName设置为$Value”
    $obj.$terminalMember=$Value
    如果($PassThru){
    #如果请求,写回修改过的对象
    写入输出$RootObject-NoEnumerate
    }
    }
    }
    
    然后在脚本中:

    Param(
        [Parameter(Mandatory=$True)]
        [String]
        $NEWPARAMVALUE,
    
        [Parameter(Mandatory=$True)]
        [String]
        $PARAM_NAME
    )
    
    $FILEPATH = “arm_template.json”
    $JSON = Get-Content $FILEPATH | Out-String | ConvertFrom-Json
    
    # Step 1, split the resolution chain into individual components
    $MemberNames = $PARAM_NAME.Split('.')
    
    # Step 2 + 3, resolve n-2 members in chain, then assign to n-1
    $JSON | Set-PropertyChained -MemberChain $MemberNames -Value $NEWPARAMVALUE
    
    $JSON | ConvertTo-Json -Depth 10 | Set-Content $FILEPATH
    

    当您传递字符串
    “a.b.c.d”
    作为
    成员访问运算符的右操作数时,PowerShell将尝试解析确切的成员名称
    a.b.c.d
    ,而不是“
    a
    ,然后
    b
    ,然后
    c
    ,然后
    d

    虽然可以通过对
    “${JSON}.${PARAM_NAME}”
    执行
    调用表达式
    来解决这一问题,但这也会打开脚本,任意执行用户可能作为
    $PARAM_NAME
    传入的任何内容

    要避免这种情况,您需要手动执行以下操作:

    $env:PARAM_NAME="resources.properties.parameters.daasServiceBaseUrl.defaultValue"
    
    
    ./test.ps1 -NEWPARAMVALUE "[parameters('DaaS_Contract_Daily_Trigger_properties_Daas-UI-to-Contract_parameters_daasServiceBaseUrl')]" -PARAM_NAME $env:PARAM_NAME
    
  • $PARAM_NAME
    拆分为单个成员名称,如
    $NAME=$PARAM_NAME.Split('.')
    ,然后
  • 手动解析除最后一个以外的每个属性名称
  • 分配给
    $resolvedObject.“$last”
  • 下面是处理步骤2和3的通用帮助器函数中的情况:

    函数集属性链接
    {
    param(
    [参数(必需=$true,ValueFromPipeline=$true,位置=0)]
    [ValidateScript({$\不是[ValueType]})]
    [对象]$RootObject,
    [参数(必需=$true,位置=1)]
    [string[]]$MemberChain,
    [参数(必需=$true,位置=2)]
    [对象]$Value,
    [交换机]$PassThru
    )
    开始{
    $resolutionChain=@($MemberChain| Select-SkipLast 1)
    $terminalMember=$($MemberChain |选择-最后1个)
    }
    过程{
    $obj=$RootObject
    #直到最后一个属性的父级的成员解析
    foreach($resolutionChain中的memberName){
    编写Vebose“解析$obj上的$memberName”
    $obj=$obj.$memberName
    }
    #设置其父属性上最后一个属性的值
    编写Vebose“将$obj上的$memberName设置为$Value”
    $obj.$terminalMember=$Value
    如果($PassThru){
    #如果请求,写回修改过的对象
    写入输出$RootObject-NoEnumerate
    }
    }
    }
    
    然后在脚本中:

    Param(
        [Parameter(Mandatory=$True)]
        [String]
        $NEWPARAMVALUE,
    
        [Parameter(Mandatory=$True)]
        [String]
        $PARAM_NAME
    )
    
    $FILEPATH = “arm_template.json”
    $JSON = Get-Content $FILEPATH | Out-String | ConvertFrom-Json
    
    # Step 1, split the resolution chain into individual components
    $MemberNames = $PARAM_NAME.Split('.')
    
    # Step 2 + 3, resolve n-2 members in chain, then assign to n-1
    $JSON | Set-PropertyChained -MemberChain $MemberNames -Value $NEWPARAMVALUE
    
    $JSON | ConvertTo-Json -Depth 10 | Set-Content $FILEPATH
    

    对我有效。我唯一更改的是:将
    $FILEPATH
    设置为完整路径。您得到的错误是什么?请编辑您的问题并粘贴您得到的错误(也可以尝试设置完整路径)。谢谢。我已经编辑了我的问题“如何处理脚本参数?”让舒尔按名称提供参数您缺少te
    资源。
    部分
    $PARAM\u NAME='properties.parameters.parametername.defaultValue'
    。作为旁注,不要使用像
    ”这样的卷曲引用
    在代码中。将这些更改为直接引用请向我们展示一些JSON文件,这样我们就不必重新设计它的外观。我唯一更改的是:将
    $FILEPATH
    设置为完整路径。您得到的错误是什么?请编辑您的问题并粘贴您得到的错误(也可以尝试设置完整路径)。谢谢。我已经编辑了我的问题-如何处理脚本参数?让舒尔按名称提供参数您缺少te
    资源。
    部分
    $PARAM\u NAME='properties.parametername.defaultValue'
    。请注意,不要使用类似
    的卷曲引用内容<