简体   繁体   English

如何使用 PowerShell 脚本卸载旧版本的 .NET SDK 和运行时?

[英]How can I uninstall older versions of .NET SDKs and runtimes with a PowerShell script?

In one PowerShell script, I am trying to install the latest version of .NET SDKs and runtimes then uninstall the older versions.在一个 PowerShell 脚本中,我尝试安装最新版本的 .NET SDK 和运行时,然后卸载旧版本。

I am currently using 2 resources from Microsoft:我目前正在使用来自 Microsoft 的 2 个资源:

  • dotnet-install.ps1 dotnet-install.ps1
  • dotnet-core-uninstall-1.2.206301 dotnet-core-uninstall-1.2.206301

List of SDKs and runtimes: SDK 和运行时列表:

Host (useful for support):
  Version: 5.0.2
  Commit:  cb5f173b96

.NET SDKs installed:
  No SDKs were found.

.NET runtimes installed:
  Microsoft.AspNetCore.All 2.1.25 [..\AppData\Local\Microsoft\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.25 [..\AppData\Local\Microsoft\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.12 [..\AppData\Local\Microsoft\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.2 [..\AppData\Local\Microsoft\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.25 [..\AppData\Local\Microsoft\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.12 [..\AppData\Local\Microsoft\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.2 [..\AppData\Local\Microsoft\dotnet\shared\Microsoft.NETCore.App]

I try to remove these older versions by running this command:我尝试通过运行以下命令来删除这些旧版本:

dotnet-core-uninstall remove --all --runtime

Error Output:错误 Output:

dotnet-core-uninstall : Unhandled exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target
of an invocation.
At line:1 char:1
+ dotnet-core-uninstall remove --all --runtime
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Unhandled excep... an invocation.:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

 ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.DotNet.Tools.Uninstall.Shared.Commands.UninstallCommandExec.AskItAndReturnUserAnswer(IDictionary`2 bundles,
String userResponse)
   at Microsoft.DotNet.Tools.Uninstall.Shared.Commands.UninstallCommandExec.Execute(IBundleCollector bundleCollector)
   at Microsoft.DotNet.Tools.Uninstall.Shared.Configs.CommandLineConfigs.<>c__DisplayClass40_0.<.cctor>b__6()
   at Microsoft.DotNet.Tools.Uninstall.Shared.Exceptions.ExceptionHandler.<>c__DisplayClass0_0.<HandleException>b__0()
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean
wrapExceptions)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters,
CultureInfo culture)
   at System.Delegate.DynamicInvokeImpl(Object[] args)
   at System.Delegate.DynamicInvoke(Object[] args)
   at System.CommandLine.Invocation.ModelBindingCommandHandler.InvokeAsync(InvocationContext context)
   at System.CommandLine.Invocation.InvocationPipeline.<>c__DisplayClass4_0.<<BuildInvocationChain>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<UseParseErrorReporting>b__21_0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass16_0.<<UseHelp>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass25_0.<<UseVersionOption>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass23_0.<<UseTypoCorrections>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<UseSuggestDirective>b__22_0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<UseParseDirective>b__20_0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<UseDebugDirective>b__11_0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<RegisterWithDotnetSuggest>b__10_0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass14_0.<<UseExceptionHandler>b__0>d.MoveNext()

According to Microsoft, the uninstall tool has limitations..根据微软的说法,卸载工具有局限性。

On Windows, the tool can only uninstall SDKs and Runtimes that were installed using one of the following installers:在 Windows 上,该工具只能卸载使用以下安装程序之一安装的 SDK 和运行时:

  • The .NET SDK and runtime installer. .NET SDK 和运行时安装程序。
  • The Visual Studio installer in versions earlier than Visual Studio 2019 version 16.3.早于 Visual Studio 2019 版本 16.3 的版本中的 Visual Studio 安装程序。

Has anyone found a workaround for this or maybe another way to uninstall these SDKs and runtimes installed by the dotnet-installer.ps1 script?有没有人找到解决此问题的方法,或者找到其他方法来卸载由 dotnet-installer.ps1 脚本安装的这些 SDK 和运行时? I would greatly appreciate any suggestions or tips.我将不胜感激任何建议或提示。 Thank you.谢谢你。

This is not exactly an answer, but instead more of information I've found regarding the dotnet-installer script and the SDK it "installs"这不完全是一个答案,而是我发现的有关 dotnet-installer 脚本和它“安装”的 SDK 的更多信息

According to Microsoft Docs for dotnet-installer.ps1 script根据Microsoft Docs的 dotnet-installer.ps1 脚本

The install scripts do not update the registry on Windows.安装脚本不会更新 Windows 上的注册表。 They just download the zipped binaries and copy them to a folder.他们只是下载压缩的二进制文件并将它们复制到一个文件夹中。 If you want registry key values to be updated, use the .NET installers.如果要更新注册表项值,请使用 .NET 安装程序。

Install directory default is %LocalAppData%\Microsoft\dotnet on Windows and /usr/share/dotnet on Linux/macOS.安装目录默认为 Windows 上的 %LocalAppData%\Microsoft\dotnet 和 Linux/macOS 上的 /usr/share/dotnet。

-InstallDir|--install-dir -InstallDir|--安装目录

Specifies the installation path.指定安装路径。 The directory is created if it doesn't exist.如果目录不存在,则创建该目录。 The default value is %LocalAppData%\Microsoft\dotnet on Windows and /usr/share/dotnet on Linux/macOS.默认值为 Windows 上的 %LocalAppData%\Microsoft\dotnet 和 Linux/macOS 上的 /usr/share/dotnet。 Binaries are placed directly in this directory.二进制文件直接放在此目录中。

Perhaps the answer is to search this folder for SDK installations and remove the folders?也许答案是在此文件夹中搜索 SDK 安装并删除文件夹?

However, according to this dotnet sdk issue from 2017, these "installations" are not really doing anything.但是,根据 2017 年的这个dotnet sdk 问题,这些“安装”并没有真正做任何事情。 'Dotnet' does not search this path for SDKs. 'Dotnet' 不会在此路径中搜索 SDK。 Maybe this has changed, but I did not find anything saying otherwise.也许这已经改变了,但我没有发现任何不同的说法。

'dotnet' does not consider versions that were installed to the user's AppData folder; “dotnet”不考虑安装到用户 AppData 文件夹的版本; this is by design.这是设计使然。

It seems like the SDK installations done by the dotnet-installer.ps1 script are meant only to be used in that same session and then forgotten about!似乎由 dotnet-installer.ps1 脚本完成的 SDK 安装仅用于相同的 session,然后被遗忘!

By default, the script adds the install location to the $PATH for the current session .默认情况下,该脚本将安装位置添加到当前 session 的 $PATH 中 Override this default behavior by specifying the -NoPath|--no-path argument.通过指定 -NoPath|--no-path 参数覆盖此默认行为。 The script doesn't set the DOTNET_ROOT environment variable.该脚本未设置 DOTNET_ROOT 环境变量。

dotnet-install: Note that the intended use of this script is for Continuous Integration (CI) scenarios, where:
dotnet-install: - The SDK needs to be installed without user interaction and without admin rights.
dotnet-install: - The SDK installation doesn't need to persist across multiple CI runs.
dotnet-install: To set up a development environment or to run apps, use installers rather than this script. Visit https://dotnet.microsoft.com/download to get the installer.

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

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