You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell JSON Schema验证脚本在VS Code中失效求助

解决VS Code中PowerShell加载Newtonsoft DLL验证JSON Schema失败的问题

我之前也碰到过一模一样的情况——脚本在原生PowerShell和ISE里跑得好好的,一到VS Code就掉链子。核心问题大多出在VS Code的PowerShell环境和传统环境的差异上,结合你的代码片段,咱们来逐个排查:

1. 先确认VS Code用的是哪个PowerShell版本

VS Code默认可能搭载的是PowerShell Core(pwsh.exe),而你在ISE里用的是Windows PowerShell(powershell.exe)。这俩的.NET runtime不一样:Windows PowerShell用的是.NET Framework,Core用的是.NET Core/.NET 5+。

  • 打开VS Code终端,输入:
    $PSVersionTable.PSEdition
    
    如果输出是Core,那你得换对应.NET Standard版本的Newtonsoft.Json DLL,而不是.NET Framework版本的。可以去NuGet下载对应版本,或者用Install-Package Newtonsoft.Json命令安装到本地项目。

2. 检查DLL的路径是否正确

你的代码里$Json_dll = "..."路径没写全,VS Code的默认工作目录可能和你打开PowerShell/ISE时的目录不一样,导致找不到DLL。

  • 解决方法:
    • 直接用绝对路径指定DLL位置,比如:
      $Json_dll = "D:\Full\Path\To\Your\Newtonsoft.Json.dll"
      
    • 或者先切换到DLL所在目录再加载:
      Set-Location "D:\Path\To\DLL\Folder"
      Add-Type -Path $Json_dll
      

3. 排查执行策略和权限问题

VS Code可能以普通用户权限运行,或者执行策略比原生PowerShell更严格,导致无法加载外部DLL。

  • 检查当前执行策略:
    Get-ExecutionPolicy
    
  • 如果是Restricted,右键以管理员身份打开VS Code,然后设置成RemoteSigned(注意安全风险,用完可以改回去):
    Set-ExecutionPolicy RemoteSigned
    

4. 换一种DLL加载方式

有时候Add-Type在VS Code的环境里会有兼容性问题,试试用.NET反射加载:

[System.Reflection.Assembly]::LoadFile($Json_dll)

如果是通过NuGet安装的包,也可以直接从用户的NuGet缓存里加载,比如:

$nugetPath = "$env:USERPROFILE\.nuget\packages\newtonsoft.json\13.0.3\lib\netstandard2.0\Newtonsoft.Json.dll"
Add-Type -Path $nugetPath

5. 调整VS Code的PowerShell扩展设置

确认VS Code的PowerShell扩展是不是用了你习惯的Windows PowerShell:

  1. 打开VS Code设置(快捷键Ctrl+,)
  2. 搜索PowerShell: Executable Path
  3. 把路径改成Windows PowerShell的执行文件路径,比如:C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

最后,给你补全一下验证JSON Schema的完整代码片段,方便你测试:

$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_dll = "D:\Full\Path\To\Newtonsoft.Json.dll"
$JsonSchema_dll = "D:\Full\Path\To\Newtonsoft.Json.Schema.dll" # 注意还要加载Schema的DLL!

# 加载DLL
Add-Type -Path $Json_dll
Add-Type -Path $JsonSchema_dll

# 读取文件内容(一定要加-Raw,否则会读成字符串数组)
$Json = Get-Content $Json_file -Raw
$Json_wrong = Get-Content $Json_file_wrong -Raw
$SchemaJson = Get-Content $Json_Schema_file -Raw

# 验证正确的JSON
try {
    $schema = [Newtonsoft.Json.Schema.JsonSchema]::Parse($SchemaJson)
    $reader = New-Object Newtonsoft.Json.JsonTextReader([System.IO.StringReader]$Json)
    $validator = New-Object Newtonsoft.Json.Schema.JsonSchemaValidator
    while ($reader.Read()) {
        $validator.Validate($reader, $schema)
    }
    Write-Host "✅ 正确的JSON验证通过!" -ForegroundColor Green
} catch {
    Write-Host "❌ 正确的JSON验证失败:$_" -ForegroundColor Red
}

# 验证错误的JSON
try {
    $schema = [Newtonsoft.Json.Schema.JsonSchema]::Parse($SchemaJson)
    $reader = New-Object Newtonsoft.Json.JsonTextReader([System.IO.StringReader]$Json_wrong)
    $validator = New-Object Newtonsoft.Json.Schema.JsonSchemaValidator
    while ($reader.Read()) {
        $validator.Validate($reader, $schema)
    }
    Write-Host "✅ 错误的JSON验证通过?这不对啊!" -ForegroundColor Yellow
} catch {
    Write-Host "❌ 错误的JSON验证失败(符合预期):$_" -ForegroundColor Green
}

哦对了,别忘了还要加载Newtonsoft.Json.Schema.dll,这个是专门做Schema验证的,很多人会漏装这个。

内容的提问来源于stack exchange,提问作者Vlad Popov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:03:57