Powershell卸载模块...完全 [英] Powershell Unload Module... completely

查看:94
本文介绍了Powershell卸载模块...完全的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调试 Powershell 项目.我正在使用 Import-Module 从我的 C# dll 加载 PS 模块,一切正常.调用 Remove-Module 并没有完全卸载模块,因为 DLL 仍然被锁定并且无法删除.

I'm working on debugging a Powershell project. I'm using Import-Module to load the PS module from my C# dll and everything works fine. Calling Remove-Module does not fully unload the module though as the DLL is still locked and can not be deleted.

有没有办法让 PSH 完全卸载模块并释放 DLL,以便我可以复制它并使用 Import-Module 重新加载它而无需重新启动 PSH 控制台?

Is there a way to get PSH to fully unload the module and release the DLL so that I can copy over it and reload it again using Import-Module without restarting the PSH console?

更新
因此,如果您将模块加载到单独的 AppDomain 中,它是否仍然像普通模块一样工作?谁能举个例子?

Update
So if you load a module into a seperate AppDomain does it still work like a normal module? Can anyone provide an example?

推荐答案

有一个解决方法.打开另一个 PowerShell 实例:

There is a workaround. Open up another instance of PowerShell:

PS > powershell
PS > [load DLL]
PS > [do work]
PS > exit

退出后,您将返回到从中进行此调用的 PowerShell 实例(假设您在 PowerShell 的内部和实例中进行了 powershell 调用).您可以将任何普通参数传递给 powershell,因此您可以使用 -Command 或 -File.例如,

After the exit, you'll be brought back to the instance of PowerShell from which you made this call (assuming you made the powershell call inside and instance of PowerShell). You can pass in any of the normal arguments to powershell, so you can use -Command or -File. E.g.,

PS > powershell -Command '[load DLL]; [do work]' # Executes a command and exits
PS > powershell -Command '.myscript.ps1 param1 param2' # Executes the script and exits
PS > powershell -File .myscript.ps1 param1 param2 # Executes a script and exits.

当 PowerShell 退出时,它会释放对 DLL 的锁定,让您继续工作.

When PowerShell exits, it will release the lock on the DLL, allowing you to continue working.

所有这些都是通过 PowerShell 命令行界面完成的.我还没有测试过如果你在脚本中间抛出 powershell 会发生什么,或者这在 ISE 中是否有效.(我怀疑它在 ISE 中有效.)即使在脚本中不起作用,这在开发过程中仍然很有用.

All of this was done from the PowerShell command line interface. I haven't tested what happens if you throw powershell in the middle of a script or if this works within ISE. (I suspect it works within ISE.) Even if if doesn't work inside a script, this is still useful during development.

做了一些检查.所以这在脚本和 ISE 中似乎工作正常,但在 ISE 中有一个警告.从 ISE 中,当您处于单独的 PowerShell 进程中时,您无法读取用户的任何输入.如果您尝试,脚本或命令会停止等待,但没有像正常那样显示输入框,当然,您不能直接在 ISE 的输出窗口中键入.所以如果你需要在[do work]中间提示输入,提示before启动一个新的PowerShell实例,并把它作为参数传入work.如果您使用的是常规 PowerShell 命令行,这些根本不是问题.

Did some checking. So this seems to work fine from within scripts and in ISE, but there's a caveat within ISE. From ISE, you can't read any input from the user while you're inside the separate PowerShell process. If you try, the script or commands stop to wait, but no input box is shown like normal, and of course, you can't type directly into the output window in ISE. So if you need to prompt for input in the middle of [do work], prompt before firing up a new instance of PowerShell, and pass it into the work as a parameter. These aren't problems at all if you're using the regular PowerShell command line.

这篇关于Powershell卸载模块...完全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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