Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PowerShell 5.1-如何卸载当前正在使用的模块_Powershell_Uninstallation_Powershell 5.0_Powershell Module - Fatal编程技术网

PowerShell 5.1-如何卸载当前正在使用的模块

PowerShell 5.1-如何卸载当前正在使用的模块,powershell,uninstallation,powershell-5.0,powershell-module,Powershell,Uninstallation,Powershell 5.0,Powershell Module,我们正在一个部署PowerShell脚本中使用一些PowerShell模块。使用以下命令,我们正在将模块(即XXXX)安装到“C:\Program Files\WindowsPowerShell\Modules”中 现在,一旦我们使用了这个模块的功能,我们将在部署脚本结束时使用以下命令卸载它 Remove-Module -Name "XXXX" -force Uninstall-Module -Name "XXXX" -AllVersions -force 但此卸载模块命令给出以下错误 WA

我们正在一个部署PowerShell脚本中使用一些PowerShell模块。使用以下命令,我们正在将模块(即XXXX)安装到“C:\Program Files\WindowsPowerShell\Modules”中

现在,一旦我们使用了这个模块的功能,我们将在部署脚本结束时使用以下命令卸载它

Remove-Module -Name "XXXX" -force
Uninstall-Module -Name "XXXX"  -AllVersions -force
但此卸载模块命令给出以下错误

WARNING: The version '###' of module 'XXXX' is currently in use. Retry the operation after closing the
applications.
PackageManagement\Uninstall-Package : Module 'XXXX' is in currently in use.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:2046 char:21
+ ...        $null = PackageManagement\Uninstall-Package @PSBoundParameters
+                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (Microsoft.Power...ninstallPackage:UninstallPackage) [Uninstall-Packag
   e], Exception
    + FullyQualifiedErrorId : ModuleIsInUse,Uninstall-Package,Microsoft.PowerShell.PackageManagement.Cmdlets.Uninstall
   Package

有人有办法解决这个问题吗

问题可能是,您的现有PowerShell会话正在通过从模块中加载可能的元素(如全局变量或常量)来“锁定”模块,即使您正在尝试卸载它(
删除模块

确保其未被锁定的最干净的方法是退出PowerShell会话。如果您需要保留会话以在之后进行“填充”,请在使用模块之前尝试启动新的PowerShell会话(嵌套会话),然后在结束时退出该会话。

如中所述,模块可能会在会话中卡住。 关闭PowerShell会话并在新会话中运行卸载将有所帮助

处理自动装载 如图所示,如果模块自动加载,则可能会被锁定。如果是通过配置文件发生的,则将
-NoProfile
添加到卸载脚本应有助于:

powershell -NoProfile -Command "Uninstall-Module X"
某些模块,如PSReadLine,即使在这样的会话中也会加载,并且可能需要额外的
-非交互式
参数:

powershell -NoProfile -NonInteractive -Command "Uninstall-Module X"
我还没有找到这种情况,但如果仍然没有帮助,可以使用
(Get InstalledModule-Name X).InstalledLocation
查找模块的安装位置,并通过其他方式将其删除(最后的手段)。如果该模块的另一个/旧版本与PowerShell捆绑在一起,则最好避免手动移除,而不要破坏它

权变措施
  • 在这种情况下,可能不需要卸载,您可以安装模块,保留模块,并在需要时使用
    updateinstalledModule
    进行更新
  • 许多模块无需安装即可导入<代码>导入模块路径为.psd1的文件可能会工作。不过,它并不能使
    删除模块
    100%工作
  • 如果存在安全问题,则只能使用
    安装模块-Scope CurrentUser
    为服务用户安装模块,和/或使用
    设置执行策略
    限制PowerShell的使用,这也可以使用
    -Scope
    参数运行

    • 就我而言,我是这样解决的:

    • 关闭PowerShell
    • 真的,关闭PowerShell。打开任务管理器并查找PowerShell的任何后台实例,如PowerShell.exe/pwsh.exe中的一个或两个
    • 从C:\Users\${USER}\Documents\PowerShell\Modules中删除不需要的模块文件夹

    • 我的背部很痛,但这似乎是原因和解决方案。如果PowerShell正在使用该软件包,而您需要退出PowerShell以删除它,那么如何在不重新启动PowerShell并重新锁定该软件包的情况下运行
      remove Module
      通常在powershell锁定某些模块时工作。找到解决方案了吗?真正关闭powershell是最重要的
      powershell -NoProfile -NonInteractive -Command "Uninstall-Module X"