简体   繁体   English

Powershell在VS Code终端中的行为与普通终端不同

[英]Powershell behaves differently in VS Code terminal than normal terminal

I have a script that pulls data from a web API like so: 我有一个脚本可以从Web API提取数据,如下所示:

$wc = New-Object System.Net.WebClient
$response = $wc.DownloadString("https://example.com")

$parser = New-Object Web.Script.Serialization.JavaScriptSerializer
$thing = $parser.DeserializeObject($response)

I am using VS Code as my IDE to write it and I use the PowerShell plugin's Integrated Console to test the script. 我使用VS Code作为IDE编写它,并使用PowerShell插件的Integrated Console测试脚本。 When I run there, everything goes well. 当我在那跑的时候,一切都很好。 When I try to run the script (on the same machine) using the windows powershell program (launched as admin from the start menu) I get the following error: 当我尝试使用Windows Powershell程序(在开始菜单中以admin启动)运行脚本(在同一台计算机上)时,出现以下错误:

PS C:\repo> .\script.ps1

New-Object : Cannot find type [Web.Script.Serialization.JavaScriptSerializer]: make sure the assembly containing thi
type is loaded.
At C:\repo\script.ps1:15 char:11
+ $parser = New-Object Web.Script.Serialization.JavaScriptSerializer
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidType: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

Is there a reason why this dependency cannot be resolved from the normal powershell terminal? 为什么不能从普通的Powershell终端解决此依赖关系? Is VS Code setting up a different environment that I do not know about? VS Code是否设置了我不知道的其他环境?

I am using Powershell V3.0 and I have .NET V4.0 installed on my machine. 我正在使用Powershell V3.0,并且在计算机上安装了.NET V4.0。

EDIT 编辑

The output of $PSVersionTable is the same from both environments: 在两种环境中, $PSVersionTable的输出是相同的:

Name                           Value
----                           -----
PSVersion                      3.0
WSManStackVersion              3.0
SerializationVersion           1.1.0.1
CLRVersion                     4.0.30319.42000
BuildVersion                   6.2.9200.16398
PSCompatibleVersions           {1.0, 2.0, 3.0}
PSRemotingProtocolVersion      2.2

EDIT2 EDIT2

Adding Add-Type -AssemblyName System.Web.Extensions to the top of the script seems to make it run from the normal terminal. 在脚本顶部添加Add-Type -AssemblyName System.Web.Extensions似乎使它从普通终端运行。 However, I would still like to know why it is able to run without this in VS Code. 但是,我仍然想知道为什么在VS Code中没有它就可以运行。 Is there an initialization script that runs when the Plugin is loaded? 加载插件时是否运行初始化脚本?

"PowerShell Integrated Console" terminal provided by special PowerShell process, which is also used to host PowerShell language server and provide editor services to VS Code. 特殊的PowerShell进程提供的“ PowerShell集成控制台”终端,还用于托管PowerShell语言服务器并为VS Code提供编辑器服务。 To do the job of hosting language server, it can load additional assemblies, which are not loaded by default. 要完成托管语言服务器的工作,它可以加载其他程序集,这些程序集默认情况下不会加载。

To inspect which assemblies already loaded in the process, you can use following command: 要检查过程中已经加载了哪些程序集,可以使用以下命令:

[AppDomain]::CurrentDomain.GetAssemblies() | % FullName

Is there an initialization script that runs when the Plugin is loaded? 加载插件时是否运行初始化脚本?

Yes, it is. 是的。 How otherwise it would know, that it need to start language server? 否则它将如何知道需要启动语言服务器? Simple way to inspect how instance of PowerShell was started is to use: 检查PowerShell实例如何启动的简单方法是使用:

[Environment]::CommandLine

command. 命令。

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

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