【问题标题】:How can I uninstall older versions of .NET SDKs and runtimes with a PowerShell script?如何使用 PowerShell 脚本卸载旧版本的 .NET SDK 和运行时?
【发布时间】:2021-05-27 07:12:36
【问题描述】:

在一个 PowerShell 脚本中,我尝试安装最新版本的 .NET SDK 和运行时,然后卸载旧版本。

我目前正在使用来自 Microsoft 的 2 个资源:

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

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]

我尝试通过运行以下命令来删除这些旧版本:

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

错误输出:

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()

根据微软的说法,卸载工具有局限性..

在 Windows 上,该工具只能卸载使用以下安装程序之一安装的 SDK 和运行时:

  • .NET SDK 和运行时安装程序。
  • Visual Studio 之前版本中的 Visual Studio 安装程序 2019 版 16.3。

有没有人找到解决方法或其他方法来卸载这些由 dotnet-installer.ps1 脚本安装的 SDK 和运行时?我将不胜感激任何建议或提示。谢谢。

【问题讨论】:

    标签: .net powershell .net-core automation


    【解决方案1】:

    这并不完全是一个答案,而是我找到的有关 dotnet-installer 脚本及其“安装”的 SDK 的更多信息

    根据Microsoft Docs 为dotnet-installer.ps1 脚本

    安装脚本不会更新 Windows 上的注册表。他们只是 下载压缩的二进制文件并将它们复制到一个文件夹中。如果你想 要更新的注册表项值,请使用 .NET 安装程序。

    安装目录默认为 Windows 上的 %LocalAppData%\Microsoft\dotnet 和 Linux/macOS 上的 /usr/share/dotnet。

    -安装目录|--安装目录

    指定安装路径。该目录被创建,如果它 不存在。默认值为 %LocalAppData%\Microsoft\dotnet on Windows 和 Linux/macOS 上的 /usr/share/dotnet。二进制文件被放置 直接在这个目录下。

    也许答案是在此文件夹中搜索 SDK 安装并删除文件夹?

    但是,根据 2017 年的 dotnet sdk issue,这些“安装”并没有真正做任何事情。 'Dotnet' 不会在此路径中搜索 SDK。也许这已经改变了,但我没有发现任何不同的说法。

    'dotnet' 不考虑安装到用户的版本 应用数据文件夹;这是设计使然。

    似乎由 dotnet-installer.ps1 脚本完成的 SDK 安装只能在同一会话中使用,然后就被遗忘了!

    默认情况下,脚本会将安装位置添加到 $PATH 当前会话。通过指定 -NoPath|--无路径参数。 脚本没有设置 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.
    

    【讨论】:

    • 啊。我知道了。我可能需要找到另一种方法来安装最新的 .NET SDK 和运行时。谢谢你的信息。
    猜你喜欢
    • 2022-10-06
    • 2013-02-17
    • 2018-03-21
    • 1970-01-01
    • 2012-04-19
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    • 2013-01-09
    相关资源
    最近更新 更多