在使用WebServiceProxy

时间:2017-11-29 12:54:35

标签: json powershell new-webserviceproxy

我使用PowerShell 4.0中的New-WebServiceProxy从API中提取数据,然后将其传输到JSON文件以供查看并导入另一个API服务(相同的API版本等,只是一个不同的主机)。

$tasklist.Taskconfig | ConvertTo-JSON-Depth 50 -As String | Out-File -FilePath $exportpath\$name.xml -Force

给我的XML包含TaskConfig。在这种情况下,TaskConfig是由API I自动生成的对象类型。当我想导入我正在使用的内容时:

$taskconfig      = (Get-Content "$taskjson") -join "`n" | ConvertFrom-Json

但是当我运行它时,它无法创建对象。我假设这是因为JSON包含嵌套的子节点,给出错误 -

  

无法转换价值" @ {Name = plugindive;值=;>孩子= System.Object的[]}"键入" Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1rcleWeb_WebClientAPI_asmx_wsdl.TaskConfig"。错误:"无法转换" @ {Name = plugindive;值=;儿童= System.Object的[]}"类型的值" System.Management.Automation.PSCustomObject"键入" Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1rcleWeb_WebClientAPI_asmx_wsdl.TaskConfig"。"

我试图明确说明对象的类型:

$taskconfig      = [Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1rcleWeb_WebClientAPI_asmx_wsdl.TaskConfig](Get-Content "$taskjson" | Out-string | ConvertFrom-Json)

以及创建对象然后尝试添加来自我的JSON的孩子 -

$taskconfig.children = $json.children

但这些都以同样的方式失败。

我似乎没有足够有趣地在PowerShell 5.0中解决同样的问题,但我无法验证原因 - 还有其他方法可以解决这个问题吗?

下添加了示例JSON
{"Name": "plugindive",
"Value": null,
"Children": [{
        "Name": "auto",
        "Value": "False",
        "Children": [

        ]
    },
    {
        "Name": "categories",
        "Value": null,
        "Children": [{
                "Name": "Module Z",
                "Value": "False",
                "Children": [

                ]
            },
            {
                "Name": "Module A",
                "Value": "False",
                "Children": [

                ]
            },
            {
                "Name": "Module B",
                "Value": "False",
                "Children": [

                ]
            },
            {
                "Name": "Module C",
                "Value": "False",
                "Children": [

                ]
            }
        ]
    }
]
}

1 个答案:

答案 0 :(得分:0)

似乎这在PowerShell v3.0中不起作用,所以我最终直接使用显式XML制作帖子,而不是转换为JSON。