我创建了一个自定义的PowerShell .psm1模块
我在
中创建了一个自定义PowerShell模块
C:\ Program Files \ WindowsPowersHell \ Modules \ PennoniaPmanagement
Directory。每当我更改模块中的函数,然后将模块导入脚本,更新的代码将不会生效。有解决方案吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在重新插入之前,请确保从会话中删除已加载的模块版本:
Make sure you remove the already-loaded version of the module from the session before re-importing it:
通常,
-force
- 本身足以 force 重新加载更新的模块到当前会话。import-module -force
隐式执行remove module
在重新加载模块之前(如果当前未加载模块,-force
只是正常加载模块)。还要注意,如果您是通过使用模块语句通过
加载的强制装载模块不是不是的选项(至少在Powershell 7.1.2处)。值得注意的是,如果模块导出自定义
使用模块导入的方法 - 请参见此答案有关详细信息。class
定义的定义是Mathias'二 - step 方法 -
-
remove -module -force,其次是
import -module
- 显然是在某些情况下需要的,似乎是您的。当需要两步方法时,理解是一件好事。 Mathias认为它与缓存的版本有关
类
定义(使用模块内)徘徊,而不是重新加载和重新定义,当代码>被调用。也就是说,尽管整个模块可能会重新加载,但它可能会在 staleclass
es上操作。至少在下面的简单场景中,我 都无法在Windows PowerShell 5.1中或PowerShell(Core)7.2.1中重现此问题,但可能会有问题浮出水面。
。。。 P>文档描述
force
参数仅与 - 很少使用 -.accessmode
已加载 Module's Module's Module-Module-information-uneformation对象(您可以(您可以)用进行检查(
-Module...)。accessMode
)。默认值是readwrite
,它可以随时卸载(删除)。如果属性值为ReadOnly
,删除模块-force
被卸载;如果是常数
,则根本无法将模块从会话中删除,一旦加载 - 至少不是remove module
import> import -module -force
是不是不是 的情况下,即使在它的.Accessmode
是常数
(从PowerShell 7.1.2;我尚不清楚这是否是设计)。测试代码涉及使用修改的
类
定义重新加载模块,以查看import> import -module -force
就足够了:在两个 Windows PowerShell (其最新版本和最后一个版本是v5.1)和 powershell(core)7.2.1 (截至本文撰写本文时),上述产量,正如预期的:
Normally,
Import-Module
-Force
- by itself - is enough to force reloading of an updated module into the current session.Import-Module -Force
implicitly performsRemove-Module
before reloading the module (if the module isn't currently loaded,-Force
just loads the module normally).Also note that force-reloading a module is not an option if you're loading it via a
using module
statement (at least as of PowerShell 7.1.2). Notably theusing module
method of importing is required if a module exports customclass
definitions that the caller should see - see this answer for details.Mathias' two-step approach -
Remove-Module -Force
, followed byImport-Module
- is apparently needed in some cases, and seems to be required in yours.It would be good to understand when the two-step approach is needed. Mathias thinks it is related to cached versions of custom
class
definitions (used module-internally) lingering instead of getting reloaded and redefined whenImport-Module -Force
is called. That is, while the module overall may get reloaded, it may be operating on staleclass
es. At least in the simple scenario below I was not able to reproduce this problem, neither in Windows PowerShell 5.1, nor in PowerShell (Core) 7.2.1, but there may be scenarios where the problem does surface.The
Remove-Module
documentation describes the-Force
parameter solely as relating to the - rarely used -.AccessMode
property available on a loaded module's module-information object (you can inspect it with(
Get-Module
...).AccessMode
). The default value isReadWrite
, which allows unloading (removal) of the module anytime. If the property value isReadOnly
,Remove-Module -Force
is needed to unload; if it isConstant
, the module cannot be removed from the session at all, once loaded - at least not withRemove-Module
.Import-Module -Force
is not subject to these restrictions and implicitly unloads a module even if its.AccessMode
isConstant
(as of PowerShell 7.1.2; I am unclear on whether that is by design).Test code involving reloading a module with a modified
class
definition, to see ifImport-Module -Force
is enough:In both Windows PowerShell (whose latest and last version is v5.1) and PowerShell (Core) 7.2.1 (current as of this writing), the above yields, as expected: