Powershell Code is working well on Powershell.exe and Powershell ISE but not working on VS Code

走远了吗. 提交于 2020-01-15 11:56:12

问题


I'm trying to validate Json against Schema using Powershell and Newtonsoft dlls. My script is working well as expected if I run it on Powershell.exe or Powershell ISE but it isn't working if I run it using VS Code (on same PC).

$Json_file = "D:\Json\file_sample.json"
$Json_file_wrong = "D:\Json\file_sample_wrong.json"
$Json_Schema_file = "D:\Json\schema_sample.json"

$Json = Get-Content $Json_file
$Json_wrong = Get-Content $Json_file_wrong
$SchemaJson = Get-Content $Json_Schema_file

$Json_dll = "D:\Json\Json110r1\Bin\net45\Newtonsoft.Json.dll"
$Json_Schema_dll = "D:\Json\JsonSchema30r9\Bin\net45\Newtonsoft.Json.Schema.dll"

Add-Type -Path $Json_dll
Add-Type -Path $Json_Schema_dll

$source = @'
    public class Validator
    {
        public static System.Collections.Generic.IList<string> Validate(Newtonsoft.Json.Linq.JToken token, Newtonsoft.Json.Schema.JSchema schema)
        {
            System.Collections.Generic.IList<string> messages;
            Newtonsoft.Json.Schema.SchemaExtensions.IsValid(token, schema, out messages);

            return messages;
        }
    }
'@
Add-Type -TypeDefinition $source -ReferencedAssemblies $Json_dll, $Json_Schema_dll

function Validate_Json_Against_Schema {
    param (
    [Parameter(Mandatory=$True)] $Json_param,
    [Parameter(Mandatory=$True)] $Schema_param
    )
$valid = $false

$Token = [Newtonsoft.Json.Linq.JToken]::Parse($Json_param)
$Schema = [Newtonsoft.Json.Schema.JSchema]::Parse($Schema_param)

$result = [Validator]::Validate($Token,$Schema)

if ($result.Count -eq 0)
    {
    $valid = $true
    }
return $valid
}

Validate_Json_Against_Schema $Json $SchemaJson
Validate_Json_Against_Schema $Json_wrong $SchemaJson

If I run it using VS Code (version 1.12.1, Powershell extension version 1.6.0) I have such error:

Cannot convert argument "token", with value: "{
  "shipping_address": {
    "street_address": "1600 Pennsylvania Avenue NW",
    "city": "Washington",
    "state": "DC"
  }
}", for "Validate" to type "Newtonsoft.Json.Linq.JToken": "Cannot convert the "{
  "shipping_address": {
    "street_address": "1600 Pennsylvania Avenue NW",
    "city": "Washington",
    "state": "DC"
  }
}" value of type "Newtonsoft.Json.Linq.JObject" to type "Newtonsoft.Json.Linq.JToken"."
At C:\Users\popovvg\Desktop\123.ps1:39 char:1
+ $result = [Validator]::Validate($Token,$Schema)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

My OS is fully updates Windows 10 x64. $PSVersionTable

Name                           Value                                                                                                                               
----                           -----                                                                                                                               
PSVersion                      5.1.16299.98                                                                                                                        
PSEdition                      Desktop                                                                                                                             
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                             
BuildVersion                   10.0.16299.98                                                                                                                       
CLRVersion                     4.0.30319.42000                                                                                                                     
WSManStackVersion              3.0                                                                                                                                 
PSRemotingProtocolVersion      2.3                                                                                                                                 
SerializationVersion           1.1.0.1 

Json file:

{
  "shipping_address": {
    "street_address": "1600 Pennsylvania Avenue NW",
    "city": "Washington",
    "state": "DC",
    "type": "business"
  }
}

Schema file:

{
  "$schema": "http://json-schema.org/draft-04/schema#",

  "definitions": {
    "address": {
      "type": "object",
      "properties": {
        "street_address": { "type": "string" },
        "city":           { "type": "string" },
        "state":          { "type": "string" }
      },
      "required": ["street_address", "city", "state"]
    }
  },

  "type": "object",

  "properties": {
    "billing_address": { "$ref": "#/definitions/address" },
    "shipping_address": {
      "allOf": [
        { "$ref": "#/definitions/address" },
        { "properties":
          { "type": { "enum": [ "residential", "business" ] } },
          "required": ["type"]
        }
      ]
    }
  }
}

I tried to run it on Windows 7 x86 - same result. What am I doing wrong?


回答1:


There is a bug I recall hearing in VS Code and PowerShell extension around classes, but can't recall what it was. Your script is being executed in VS Code via the Integrated Terminal, which is PowerShell Editor Services in the back end.

The host that your code executes in that integrated terminal is a different host than what you get via PowerShell.exe or the ISE. I would recommend submitting an issue with the VS Code logs to the PS Extension repo: https://github.com/powershell/vscode-powershell/issues



来源:https://stackoverflow.com/questions/49383121/powershell-code-is-working-well-on-powershell-exe-and-powershell-ise-but-not-wor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!