PowerShell 5.1 - How to uninstall module which is currently use

久未见 提交于 2019-12-08 14:44:55

问题


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?


回答1:


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

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.



来源:https://stackoverflow.com/questions/42626470/powershell-5-1-how-to-uninstall-module-which-is-currently-use

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!