我创建了一个自定义的PowerShell .psm1模块

发布于 2025-02-01 04:11:13 字数 170 浏览 0 评论 0 原文

我在
中创建了一个自定义PowerShell模块 C:\ Program Files \ WindowsPowersHell \ Modules \ PennoniaPmanagement Directory。每当我更改模块中的函数,然后将模块导入脚本,更新的代码将不会生效。有解决方案吗?

I created a custom powershell module in the
C:\Program Files\WindowsPowerShell\Modules\PennoniAppManagement directory. Whenever I make changes to a function in the module, then import the module into a script, the updated code won't take effect. Any solutions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

隐诗 2025-02-08 04:11:13

在重新插入之前,请确保从会话中删除已加载的模块版本:

Remove-Module PennoniAppManagement -Force
Import-Module PennoniAppManagement

Make sure you remove the already-loaded version of the module from the session before re-importing it:

Remove-Module PennoniAppManagement -Force
Import-Module PennoniAppManagement
最笨的告白 2025-02-08 04:11:13
  • 通常 -force - 本身足以 force 重新加载更新的模块到当前会话。

    • import-module -force 隐式执行 remove module 在重新加载模块之前(如果当前未加载模块, -force 只是正常加载模块)。

    • 还要注意,如果您是通过使用模块语句通过加载的强制装载模块不是不是的选项(至少在Powershell 7.1.2处)。值得注意的是,如果模块导出自定义 class 定义的定义是使用模块导入的方法 - 请参见此答案有关详细信息。




  • Mathias'二 - step 方法 - - remove -module -force ,其次是 import -module - 显然是在某些情况下需要的,似乎是您的。


    • 当需要两步方法时,理解是一件好事。 Mathias认为它与缓存的版本有关定义(使用模块内)徘徊,而不是重新加载和重新定义,当代码>被调用。也就是说,尽管整个模块可能会重新加载,但它可能会在 stale class es上操作。至少在下面的简单场景中,我 都无法在Windows PowerShell 5.1中或PowerShell(Core)7.2.1中重现此问题,但可能会有问题浮出水面。

    • 文档描述 force 参数仅与 - 很少使用 - .accessmode 已加载 Module's Module's Module-Module-information-uneformation对象(您可以(您可以)用进行检查( -Module ...)。accessMode )。默认值是 readwrite ,它可以随时卸载(删除)。如果属性值为 ReadOnly 删除模块-force 被卸载;如果是常数,则根本无法将模块从会话中删除,一旦加载 - 至少不是 remove module 。 P>

      • 值得注意的是,隐式 import> import -module -force 不是不是 的情况下,即使在它的 .Accessmode 常数(从PowerShell 7.1.2;我尚不清楚这是否是设计)。





测试代码涉及使用修改的定义重新加载模块,以查看 import> import -module -force 就足够了:

# Create a template for the content of a sample script module.
# Note: The doubled { and } are needed for use of the string with
#       with the -f operator later.
$moduleContent = @'
class MyClass {{
  [string] $Foo{0}
}}

function Get-Foo {{
  # Print the property names of custom class [MyClass]
  [MyClass]::new().psobject.Properties.Name
}}
'@

# Create the module with property name .Foo1 in the [MyClass] class.
$moduleContent -f 1  > .\Foo.psm1

# Import the module and call Get-Foo to echo the property name.
Import-Module .\Foo.psm1; Get-Foo

# Now update the module on disk by changing the property name
# to .Foo2
$moduleContent -f 2  > .\Foo.psm1

# Force-import (reload) the module and 
# see if the property name changed.
Import-Module -Force .\Foo.psm1; Get-Foo

# Clean up.
Remove-Item .\Foo.psm1

在两个 Windows PowerShell (其最新版本和最后一个版本是v5.1)和 powershell(core)7.2.1 (截至本文撰写本文时),上述产量,正如预期的:

Foo1  # Original import.
Foo2  # After modifying the class and force-reloading
  • Normally, Import-Module-Force - by itself - is enough to force reloading of an updated module into the current session.

    • Import-Module -Force implicitly performs Remove-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 the using module method of importing is required if a module exports custom class definitions that the caller should see - see this answer for details.

  • Mathias' two-step approach - Remove-Module -Force, followed by Import-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 when Import-Module -Force is called. That is, while the module overall may get reloaded, it may be operating on stale classes. 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 is ReadWrite, which allows unloading (removal) of the module anytime. If the property value is ReadOnly, Remove-Module -Force is needed to unload; if it is Constant, the module cannot be removed from the session at all, once loaded - at least not with Remove-Module.

      • Notably, the implicit unloading that happens with Import-Module -Force is not subject to these restrictions and implicitly unloads a module even if its .AccessMode is Constant (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 if Import-Module -Force is enough:

# Create a template for the content of a sample script module.
# Note: The doubled { and } are needed for use of the string with
#       with the -f operator later.
$moduleContent = @'
class MyClass {{
  [string] $Foo{0}
}}

function Get-Foo {{
  # Print the property names of custom class [MyClass]
  [MyClass]::new().psobject.Properties.Name
}}
'@

# Create the module with property name .Foo1 in the [MyClass] class.
$moduleContent -f 1  > .\Foo.psm1

# Import the module and call Get-Foo to echo the property name.
Import-Module .\Foo.psm1; Get-Foo

# Now update the module on disk by changing the property name
# to .Foo2
$moduleContent -f 2  > .\Foo.psm1

# Force-import (reload) the module and 
# see if the property name changed.
Import-Module -Force .\Foo.psm1; Get-Foo

# Clean up.
Remove-Item .\Foo.psm1

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:

Foo1  # Original import.
Foo2  # After modifying the class and force-reloading
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文