PowerShell 5.1 - 如何卸载当前使用的模块 [英] PowerShell 5.1 - How to uninstall module which is currently use

查看:163
本文介绍了PowerShell 5.1 - 如何卸载当前使用的模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We are using some PowerShell modules in one deployment PowerShell script. Using following command we are installing module (i.e. XXXX) into "C:\Program Files\WindowsPowerShell\Modules".

Install-Module -Name "XXXX" -AllowClobber -RequiredVersion "XXXX" -Repository "XXXX" -Scope AllUsers

现在一旦我们使用了这个模块的功能,我们就会在部署脚本的末尾使用以下命令卸载它.

Now once we used the functionality of this module, we uninstall it at the end of deployment script using following command.

Remove-Module -Name "XXXX" -force
Uninstall-Module -Name "XXXX"  -AllVersions -force

但是这个卸载模块命令给出了以下错误.

But this uninstall module command gives following error.

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

有没有人有任何想法来解决这个问题?

Does anybody have any idea to resolve this?

推荐答案

问题可能是您现有的 PowerShell 会话通过从中加载可能的元素(例如全局变量或常量)来锁定"模块,即使您是试图卸载它(Remove-Module).

The problem could be that your existing PowerShell session is "locking" the module by loading possible elements from it (such as global variables or constants) even though you are trying to unload it (Remove-Module).

确保它没有被锁定的最干净的方法是退出 PowerShell 会话.如果您需要保持会话在之后做事情",请在使用模块之前尝试启动一个新的 PowerShell 会话(嵌套会话),然后在最后退出.

The cleanest way to be sure it isn't locked is to exit the PowerShell session. If you need to keep the session around to do "stuff" afterwards, trying starting a new PowerShell session (nested session) just before you make use of the module, then exit it at the end.

这篇关于PowerShell 5.1 - 如何卸载当前使用的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆