【问题标题】:PowerShell: Modules, their locations, and reloadingPowerShell:模块、它们的位置和重新加载
【发布时间】:2015-04-16 08:58:56
【问题描述】:

我有一个非常简单的 PS 脚本,它指的是一堆自己编写的模块 (psm1)。当我从 PowerShell ISE 运行我的脚本时,我经常把头发拉出来,因为我的模块的最新版本没有被执行(而是一些过时的-somewhere-stored-in-memory-version)。

我有 google-d 和 google-d,并且阅读并尝试过,现在是我非常感谢了解到底发生了什么的时候,而不是一直在解决这些问题。

一些示例代码:

(该脚本将配置将在我们的机器中服务的 PC、设置共享、创建本地用户、配置 NIC 等)

我首先将我的模块的位置添加到模块路径中,如下所示:

# Make sure .\Modules is part of the PSModulePath environment variable
$currentPSModulePath = [Environment]::GetEnvironmentVariable("PSModulePath", "Machine")
If (-Not ($currentPSModulePath -Like '*.\Modules*'))
{
    [Environment]::SetEnvironmentVariable("PSModulePath", $currentPSModulePath + ";.\Modules", "Machine")
}
Write-Host ("Environment variable for PSModulePath = {0}" -f [Environment]::GetEnvironmentVariable("PSModulePath", "Machine"))

然后,我加载所需的模块:

### Import Modules
Import-Module -DisableNameChecking ConfigureSystemPC
Import-Module -DisableNameChecking ConfigureChipPC
Import-Module -DisableNameChecking ConfigureEngravePC
Import-Module -DisableNameChecking ConfigureCInkPC
Import-Module -DisableNameChecking ConfigureIPPC
Import-Module -DisableNameChecking ConfigureNPPC

最后,我问用户应该配置哪台电脑:

### Start configuring new PC ###

Write-Host "`nChoose the PC type form the options below:`n"
Write-Host "1. System PC"
Write-Host "2. Chip PC"
Write-Host "3. Engrave PC"
Write-Host "4. CInkjet PC"
Write-Host "5. IP PC"
Write-Host "6. NP PC"
Write-Host "7. Output PC"

$pcType = Read-Host "Please enter the PC type and press [enter]"
Write-Host ("You choose option: {0}" -f $pcType)

switch ($pcType)
{
    1 { Configure-SystemPC }
    2 { Configure-ChipPC }
    3 { Configure-EngravePC }
    4 { Configure-CInkPC }
    5 { Configure-IPPC }
    6 { Configure-NPPC }
    7 { Configure-OutputPC }
}

当我更改配置模块中的某些内容时,问题就出现了。当我简单地添加(例如)Write-Host "bla bla",按下保存按钮,并再次调试我的主脚本(如上所示的脚本)时,PowerShell 将运行模块的 OLD 版本。

除非我通过以下任一方式重新加载模块:

  • rmo Configure-EngravePC 后跟 ipmo Configure-EngravePC
  • ipmo Configure-EngravePC -Force

将执行与我的模块完全相同的“旧”版本。

谁能指出我通常如何处理这个问题? 为什么,为什么,为什么,当我通过调试器运行我的脚本时,我什至必须重新加载模块?为什么它会“存储”在不同会话中运行的模块?难道我做错了什么?

非常感谢提前,我希望有人能详细说明这一点,我经常卡住..

我非常喜欢有正确解释的答案(或参考关于此主题的文档)

【问题讨论】:

    标签: powershell powershell-2.0 powershell-module


    【解决方案1】:

    您需要重新启动 ISE,因为它保留单个控制台 AFAIK。

    现在,为了在不重新启动的情况下执行此操作,您要么必须创建新的 ISE 运行空间(文件>新 PowerShell 选项卡),要么放弃 ISE(推荐,因为它 sux)并使用正常的东西,例如 Cmder 并输入 ISE 仅用于广泛的调试或实现某种形式的自动重新加载。

    糟糕的解决方案是在您输入 ISE 时替换 prompt 函数(或将其添加到您的个人资料中):

    rename-item Function:prompt Function:old_prompt -ErrorAction ignore
    function global:prompt() {
        import-module -force Configure-EngravePC
        Function:old_prompt
    }
    

    这将正常工作除了第一次提示更改后,因为提示功能尚未执行。您可以优化它以仅在文件最近更改(例如最近 10 分钟)时重新加载模块。

    由于 第一个提示 可能有问题,因此有一些方法可以使其自动化:) 创建 Autohotkey 脚本来监视模块的更改并在检测到更改后发送到其控制台。如果您这样做,那么甚至不需要立即更改,因为您可以使用 Autohotkey 本身 Send ipmo -force..。当我写这篇文章时,我很高兴,但它是史诗般的,如果 AHK 脚本正确完成,老实说它将完美地工作。

    我不确定是否有任何其他方法可以在活动会话中自动重新加载模块,除非您使用一些怪异的东西,例如为所有 cmdlet 创建 powershell 代理,这些代理基本上与提示功能相同。但是……

    编辑

    我发现这个东西是半相关的,但我仍然认为 AHK 解决方案是可行的方法

    https://stackoverflow.com/a/7341211/82660

    EDIT2

    这是完成最困难部分的Ahk 脚本(我没有编写文件修改检查,这很简单):

    ;Find window
    SetTitleMatchMode, 2
    ise := WinExist("PowerShell ISE")
    WinGetPos, x,y,w,h
    
    ;Click somewhere inside console to select it
    x := x + 20
    y := h - 100  ;you may need to tweak this number depending on windows theme etc...
    WinActivate
    CoordMode, Mouse ,Screen
    Click %x%, %y%
    SendInput {ESC}import-module -force Configure-EngravePC{ENTER}
    

    脚本可能会更好更精确,但我的 AHKfoo 现在很穷......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      • 2014-02-06
      • 1970-01-01
      • 2012-11-07
      • 1970-01-01
      相关资源
      最近更新 更多