Powershell Code在Powershell.exe和Powershell ISE上运行良好,但不能在VS Code上运行

时间:2018-03-20 11:32:37

标签: json powershell json.net visual-studio-code

我试图使用Powershell和Newtonsoft dll来验证Json对Schema的影响。如果我在Powershell.exe或Powershell ISE上运行它,我的脚本运行正常,但如果我使用VS Code(在同一台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

如果我使用VS Code(版本1.12.1,Powershell扩展版本1.6.0)运行它,我有这样的错误:

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

我的操作系统完全更新了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文件:

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

架构文件:

{
  "$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"]
        }
      ]
    }
  }
}

我试图在Windows 7 x86上运行它 - 结果相同。我做错了什么?

1 个答案:

答案 0 :(得分:1)

我记得有一个错误在VS Code和PowerShell扩展中听到了类,但无法回想起它是什么。您的脚本正在通过集成终端在VS Code中执行,该终端是后端的PowerShell编辑器服务。

您的代码在该集成终端中执行的主机与通过PowerShell.exe或ISE获得的主机不同。我建议将VS Code日志的问题提交到PS Extension repo:https://github.com/powershell/vscode-powershell/issues