简体   繁体   English

Powershell代码在Powershell.exe和Powershell ISE上运行良好,但在VS Code上无法运行

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

I'm trying to validate Json against Schema using Powershell and Newtonsoft dlls. 我正在尝试使用Powershell和Newtonsoft dll对Json进行架构验证。 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). 如果我在Powershell.exe或Powershell ISE上运行它,脚本可以按预期运行,但是如果我在同一台PC上使用VS Code运行它,则该脚本不起作用。

$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: 如果我使用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

My OS is fully updates Windows 10 x64. 我的操作系统已完全更新Windows 10 x64。 $PSVersionTable $ 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: Json文件:

{
  "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. 我试图在Windows 7 x86上运行它-同样的结果。 What am I doing wrong? 我究竟做错了什么?

There is a bug I recall hearing in VS Code and PowerShell extension around classes, but can't recall what it was. 我记得在VS Code和PowerShell扩展中围绕类存在一个错误,但无法回忆起它是什么。 Your script is being executed in VS Code via the Integrated Terminal, which is PowerShell Editor Services in the back end. 您的脚本正在通过集成终端以VS Code执行,集成终端是后端的PowerShell Editor Services。

The host that your code executes in that integrated terminal is a different host than what you get via PowerShell.exe or the ISE. 您在该集成终端中执行代码的主机与通过PowerShell.exe或ISE获得的主机不同。 I would recommend submitting an issue with the VS Code logs to the PS Extension repo: https://github.com/powershell/vscode-powershell/issues 我建议将VS Code日志问题提交到PS扩展库: https : //github.com/powershell/vscode-powershell/issues

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM