1

I am pulling data from an API using the New-WebServiceProxy in PowerShell 4.0 and then piping it out to a JSON file for review and import on another API service (same API version, etc, just a different host).

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

Gives me my XML containing the TaskConfig. In this case, TaskConfig is an object type automatically generated by the API I'm interfacing with. When I want to import the content I am using:

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

but when I run this it's unable to create the object. I assume this is because the JSON contains nested children, giving the error-

Cannot convert value "@{Name=plugindive; Value=;> Children=System.Object[]}" to type "Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1rcleWeb_WebClientAPI_asmx_wsdl.TaskConfig". Error: "Cannot convert the "@{Name=plugindive; Value=;Children=System.Object[]}" value of type "System.Management.Automation.PSCustomObject" to type "Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1rcleWeb_WebClientAPI_asmx_wsdl.TaskConfig"."

I've tried explictly stating the type of object:

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

as well as creating the object then trying to add the children from my JSON -

$taskconfig.children = $json.children

But these all fail in the same way.

I don't seem to get this same issue in PowerShell 5.0 interestingly enough, but I can't verify why - is there another way to approach this?

Added example JSON below

{"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": [

                ]
            }
        ]
    }
]
}
FoxDeploy
  • 12,569
  • 2
  • 33
  • 48
AskJarv
  • 184
  • 3
  • 15
  • You say that you're converting to JSON, but in actuality your examples feature `ConvertTo-Xml`. Could you share the JSON file (sanitize it first). – FoxDeploy Nov 29 '17 at 15:21
  • @FoxDeploy - you are, of course, 100% right- that'll teach me to pay attention when I copy and paste. I'll add JSON example shortly... – AskJarv Nov 29 '17 at 15:32
  • Can you post the whole (sanitized) JSON file on a secret GIST and post the link here so I can look for formatting issues? I think something went wonky in the processing. – FoxDeploy Nov 30 '17 at 14:56
  • @FoxDeploy - sure thing - https://gist.github.com/anonymous/6d546263436902ccbcebb3da095da792 (sorry for delay!). – AskJarv Dec 06 '17 at 04:54

1 Answers1

0

It seems as if this doesn't work in PowerShell v3.0, so I simply ended up making posts with the explicit XML directly, rather than converting to JSON.

AskJarv
  • 184
  • 3
  • 15