Powershell代码在Powershell.exe和Powershell ISE上运行良好,但在VS Code上无法运行 [英] Powershell Code is working well on Powershell.exe and Powershell ISE but not working on VS Code

查看:190
本文介绍了Powershell代码在Powershell.exe和Powershell ISE上运行良好,但在VS Code上无法运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Powershell和Newtonsoft dll对Json进行架构验证.如果我在Powershell.exe或Powershell ISE上运行它,脚本可以按预期运行,但是如果我在同一台PC上使用VS Code运行它,则该脚本不起作用.

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

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

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

我的操作系统已完全更新Windows 10 x64. $ PSVersionTable

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文件:

{
  "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上运行它-同样的结果.我在做什么错了?

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

推荐答案

我记得在类的VS Code和PowerShell扩展中听到了一个错误,但不记得是什么错误.您的脚本正在通过集成终端(位于后端的PowerShell Editor Services)以VS代码执行.

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.

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

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

这篇关于Powershell代码在Powershell.exe和Powershell ISE上运行良好,但在VS Code上无法运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆