Visual Studio 2017 - 无法初始化 powershell 主机

bdc*_*der 5 powershell visual-studio nuget-package

第一天:

System: Windows 10
Microsoft Visual Studio Community 2017 
Version 15.5.5
VisualStudio.15.Release/15.5.5+27130.2026
Run Code Online (Sandbox Code Playgroud)

我有一个正在使用 WindowsAzure.Storage NuGet 包的网站项目。

在 Visual Studio 中,我执行以下操作:

Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution ...
Run Code Online (Sandbox Code Playgroud)

系统显示有2个更新可用:

Newtonsoft.Json v10.0.3 (currently installed v9.0.1)
WindowsAzure.Storage v9.0 (currently installed v8.7)
Run Code Online (Sandbox Code Playgroud)

我选择选中“选择所有包”复选框,然后单击“更新”按钮。

失败(以前从未发生过这种情况)!

以下是从 Visual Studio 捕获的输出:

Retrieving package 'WindowsAzure.Storage 9.0.0' from 'nuget.org'.
...
The following error occurred while loading the extended type data file: Microsoft.PowerShell.Core, C:\Windows\SysWOW64\WindowsPowerShell\v1.0\types.ps1xml(3007) : Error in type "System.Security.AccessControl.ObjectSecurity": Exception: The getter method should be public, non void, static, and have one parameter of type PSObject.
The following error occurred while loading the extended type data file: Microsoft.PowerShell.Core, C:\Windows\SysWOW64\WindowsPowerShell\v1.0\types.ps1xml(3014) : Error in type "System.Security.AccessControl.ObjectSecurity": Exception: The getter method should be public, non void, static, and have one parameter of type PSObject.
The following error occurred while loading the extended type data file: Microsoft.PowerShell.Core, C:\Windows\SysWOW64\WindowsPowerShell\v1.0\types.ps1xml(3021) : Error in type "System.Security.AccessControl.ObjectSecurity": Exception: The getter method should be public, non void, static, and have one parameter of type PSObject.
The following error occurred while loading the extended type data file: Microsoft.PowerShell.Core, C:\Windows\SysWOW64\WindowsPowerShell\v1.0\types.ps1xml(3028) : Error in type "System.Security.AccessControl.ObjectSecurity": Exception: The getter method should be public, non void, static, and have one parameter of type PSObject.
The following error occurred while loading the extended type data file: Microsoft.PowerShell.Core, C:\Windows\SysWOW64\WindowsPowerShell\v1.0\types.ps1xml(3035) : Error in type "System.Security.AccessControl.ObjectSecurity": Exception: The getter method should be public, non void, static, and have one parameter of type PSObject.
The term 'Get-ExecutionPolicy' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
Install failed. Rolling back...
Run Code Online (Sandbox Code Playgroud)

错误列表窗口显示:

Failed to initialize the PowerShell host. If your PowerShell execution policy setting is set to AllSigned, open the Package Manager Console to initialize the host first.
Run Code Online (Sandbox Code Playgroud)

Powershell 输出来自:Get-ExecutionPolicy -List 输出:

        Scope ExecutionPolicy
        ----- ---------------
MachinePolicy       Undefined
   UserPolicy       Undefined
      Process       Undefined
  CurrentUser    Unrestricted
 LocalMachine    Unrestricted
Run Code Online (Sandbox Code Playgroud)

NuGet 包本身或我的系统有问题吗?

在寻找解决方案之前和之后我从未遇到过这个问题(到目前为止还没有找到)——我受 NuGet / Visual Studio 诸神的摆布!

提前致谢。

======

第 2 天:上午 8:00 ...

再试一次...让我们看看是否可以让包管理器控制台工作...

启动 Visual Studio(社区 2017)

TOOLS -> NuGet Package Manager -> Package Manager Console
Run Code Online (Sandbox Code Playgroud)

结果显示以下内容:

The following error occurred while loading the extended type data file: Microsoft.PowerShell.Core, C:\Windows\SysWOW64\WindowsPowerShell\v1.0\types.ps1xml(3007) : Error in type "System.Security.AccessControl.ObjectSecurity": Exception: The getter method should be public, non void, static, and have one parameter of type PSObject.
The following error occurred while loading the extended type data file: Microsoft.PowerShell.Core, C:\Windows\SysWOW64\WindowsPowerShell\v1.0\types.ps1xml(3014) : Error in type "System.Security.AccessControl.ObjectSecurity": Exception: The getter method should be public, non void, static, and have one parameter of type PSObject.
The following error occurred while loading the extended type data file: Microsoft.PowerShell.Core, C:\Windows\SysWOW64\WindowsPowerShell\v1.0\types.ps1xml(3021) : Error in type "System.Security.AccessControl.ObjectSecurity": Exception: The getter method should be public, non void, static, and have one parameter of type PSObject.
The following error occurred while loading the extended type data file: Microsoft.PowerShell.Core, C:\Windows\SysWOW64\WindowsPowerShell\v1.0\types.ps1xml(3028) : Error in type "System.Security.AccessControl.ObjectSecurity": Exception: The getter method should be public, non void, static, and have one parameter of type PSObject.
The following error occurred while loading the extended type data file: Microsoft.PowerShell.Core, C:\Windows\SysWOW64\WindowsPowerShell\v1.0\types.ps1xml(3035) : Error in type "System.Security.AccessControl.ObjectSecurity": Exception: The getter method should be public, non void, static, and have one parameter of type PSObject.
The term 'Get-ExecutionPolicy' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
Run Code Online (Sandbox Code Playgroud)

回到绘图板!将继续寻找解决方案...

那么,让我们尝试一下 Visual Studio 安装程序...

Launch Visual Studio Installer
Select "More" -> "Repair"
Run Code Online (Sandbox Code Playgroud)

结果是:

The dependent package cannot be found: Component.PowerShellTools.VS2017,version=3.0.585
Run Code Online (Sandbox Code Playgroud)

我点击“查看日志”,显示:

[1cb4:000a][2018-02-17T08:27:06] Error 0x80131500: The dependent package cannot be found: Component.PowerShellTools.VS2017,version=3.0.585
   at Microsoft.VisualStudio.Setup.Dependencies.DependencyManager.FindDependentPackages(Dependency dependency, DependencyComparer comparer, Dictionary`2 packageIndex)
   at Microsoft.VisualStudio.Setup.Dependencies.DependencyManager.BuildGraphRecursive(IPackage graphRoot, DependencyNode node, DependencyComparer comparer, Dictionary`2 packageIndex, Dictionary`2 nodeIndex, Boolean splitNodes)
   at Microsoft.VisualStudio.Setup.Dependencies.DependencyManager.BuildGraphCore(IPackage root, IEnumerable`1 packages, DependencyComparer comparer, Dictionary`2 packageIndex, Dictionary`2 nodeIndex, Boolean splitNodes, DependencyCollection deprecated)
   at Microsoft.VisualStudio.Setup.Dependencies.DependencyManager.BuildGraph(IPackage root, IEnumerable`1 packages, Boolean isUpdate, IDependencyComparisonSeed seed, DependencyCollection deprecated)
   at Microsoft.VisualStudio.Setup.Engine.GetDependencyGraph(Product product)
   at Microsoft.VisualStudio.Setup.Engine.PreChecks(String installationPath, Boolean isLayoutSet, Product product, ExecuteAction bootstrapperAction, ITelemetryOperation operation, IQuery query)
   at Microsoft.VisualStudio.Setup.Engine.Repair(CancellationToken token)
Run Code Online (Sandbox Code Playgroud)

好的 - 所以不知何故,我的系统丢失了一个依赖包(Component.PowerShellTools.VS2017) - 将尝试修改以查看是否可以添加该包...

======

第二天 - 上午 10:15(仍然如此) - 当前状态 - 束手无策......

Microsoft Visual Studio Community 2017 
Version 15.5.6
VisualStudio.15.Release/15.5.6+27130.2027
Microsoft .NET Framework
Version 4.7.02556
Run Code Online (Sandbox Code Playgroud)

我执行以下操作:

TOOLS -> NuGet Package Manager -> Package Manager Console
Run Code Online (Sandbox Code Playgroud)

结果显示以下内容:

在此输入图像描述

如果有人(微软)有一个解决方案——我真的很感激!

======

第二天:下午 3:09 ...

在最后一次(绝望的)尝试中,我启动了 Visual Studio 安装程序

Click on "More" -> "Repair" ....
Run Code Online (Sandbox Code Playgroud)

几个小时后,Visual Studio 被(重新)安装:

Microsoft Visual Studio Community 2017 
Version 15.5.6
VisualStudio.15.Release/15.5.6+27130.2027
Microsoft .NET Framework
Version 4.7.02556
Run Code Online (Sandbox Code Playgroud)

然后我会执行以下操作(点燃 12 支蜡烛并念诵“Bill-Bill-Bill”之后)...

TOOLS -> NuGet Package Manager -> NuGet Manager Console
Run Code Online (Sandbox Code Playgroud)

结果:

The following error occurred while loading the extended type data file: Microsoft.PowerShell.Core, C:\Windows\SysWOW64\WindowsPowerShell\v1.0\types.ps1xml(3007) : Error in type "System.Security.AccessControl.ObjectSecurity": Exception: The getter method should be public, non void, static, and have one parameter of type PSObject.
The following error occurred while loading the extended type data file: Microsoft.PowerShell.Core, C:\Windows\SysWOW64\WindowsPowerShell\v1.0\types.ps1xml(3014) : Error in type "System.Security.AccessControl.ObjectSecurity": Exception: The getter method should be public, non void, static, and have one parameter of type PSObject.
The following error occurred while loading the extended type data file: Microsoft.PowerShell.Core, C:\Windows\SysWOW64\WindowsPowerShell\v1.0\types.ps1xml(3021) : Error in type "System.Security.AccessControl.ObjectSecurity": Exception: The getter method should be public, non void, static, and have one parameter of type PSObject.
The following error occurred while loading the extended type data file: Microsoft.PowerShell.Core, C:\Windows\SysWOW64\WindowsPowerShell\v1.0\types.ps1xml(3028) : Error in type "System.Security.AccessControl.ObjectSecurity": Exception: The getter method should be public, non void, static, and have one parameter of type PSObject.
The following error occurred while loading the extended type data file: Microsoft.PowerShell.Core, C:\Windows\SysWOW64\WindowsPowerShell\v1.0\types.ps1xml(3035) : Error in type "System.Security.AccessControl.ObjectSecurity": Exception: The getter method should be public, non void, static, and have one parameter of type PSObject.
The term 'Get-ExecutionPolicy' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
Run Code Online (Sandbox Code Playgroud)

我注定要失败——在这个问题得到解决之前我无法使用 Visual Studio。

======

第 3 天 - 上午 7:49

我看到 MSFT 已对我的问题进行了分类:

https://developercommunity.visualstudio.com/content/problem/199976/visual-studio-failed-to-initialize-the-powershell.html

还尝试了这些资源中的“解决方案”:

https://developercommunity.visualstudio.com/content/problem/154563/failed-to-initialize-the-powershell-host.html

https://learn.microsoft.com/en-us/nuget/release-notes/known-issues#opening-package-manager-console-fails-with-objectsecurity-exception

不幸的是,以上都没有帮助解决这个问题。

大多数帖子都表明可能需要完全重新安装 VS ——天哪!

希望能找到一个不需要完全重新安装的解决方案!

======

第 3 天 - 下午 1:54

放弃 ...

  1. 已卸载的 Visual Studio。关闭(系统断电)
  2. 启动(系统通电)
  3. 从https://www.visualstudio.com/downloads/下载 Visual Studio 安装程序
  4. 按照安装程序说明运行磁盘清理应用程序。
  5. 关闭(系统断电)
  6. 启动(系统通电)
  7. 运行 Visual Studio 2017 社区安装程序 - 成功。
  8. 启动 Visual Studio
  9. 工具 -> NuGet 包管理器 -> 包管理器控制台

结果:

The following error occurred while loading the extended type data file: Microsoft.PowerShell.Core, C:\Windows\SysWOW64\WindowsPowerShell\v1.0\types.ps1xml(3007) : Error in type "System.Security.AccessControl.ObjectSecurity": Exception: The getter method should be public, non void, static, and have one parameter of type PSObject.
The following error occurred while loading the extended type data file: Microsoft.PowerShell.Core, C:\Windows\SysWOW64\WindowsPowerShell\v1.0\types.ps1xml(3014) : Error in type "System.Security.AccessControl.ObjectSecurity": Exception: The getter method should be public, non void, static, and have one parameter of type PSObject.
The following error occurred while loading the extended type data file: Microsoft.PowerShell.Core, C:\Windows\SysWOW64\WindowsPowerShell\v1.0\types.ps1xml(3021) : Error in type "System.Security.AccessControl.ObjectSecurity": Exception: The getter method should be public, non void, static, and have one parameter of type PSObject.
The following error occurred while loading the extended type data file: Microsoft.PowerShell.Core, C:\Windows\SysWOW64\WindowsPowerShell\v1.0\types.ps1xml(3028) : Error in type "System.Security.AccessControl.ObjectSecurity": Exception: The getter method should be public, non void, static, and have one parameter of type PSObject.
The following error occurred while loading the extended type data file: Microsoft.PowerShell.Core, C:\Windows\SysWOW64\WindowsPowerShell\v1.0\types.ps1xml(3035) : Error in type "System.Security.AccessControl.ObjectSecurity": Exception: The getter method should be public, non void, static, and have one parameter of type PSObject.
The term 'Get-ExecutionPolicy' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
Run Code Online (Sandbox Code Playgroud)

所以 - 即使是全新安装的 Visual Studio Community 也无法解决这个问题!!!!

======

第 4 天 - 上午 9:11

昨晚深夜,我在 GitHub 上针对 PowerShell 打开了一个新问题,请参阅:

https://github.com/PowerShell/PowerShell/issues/6189

今天早上找到回复,采纳了lzybkr的建议,禁用了Powershell V2;我在上面的 GitHub 帖子上发布了禁用 V2 的命令。

好消息- 我能够再次通过 NuGet 更新软件包!

坏消息- 如果我启动 Visual Studio 然后执行以下操作:

TOOLS -> NuGet Package Manager -> Package Manager Console
Run Code Online (Sandbox Code Playgroud)

控制台窗口打开,但只是挂起并显示以下消息:

Initializing Powershell host...
Run Code Online (Sandbox Code Playgroud)

我仍然不知道是什么导致了 Visual Studio 中的这种行为;但 4 天后,我觉得自己很幸运,我可以(再次)更新 NuGet 包并重新开始工作。

尽管我更新 NuGet 包的“问题”(大部分)已得到解决,但我仍然不认为这是一个解决方案,因为 Visual Studio / NuGet 包管理器控制台无法初始化 PowerShell 主机的根本原因尚未解决。

如果有人找到根本原因,请发布您的解决方案。