我开始学习 Powershell 并编写一个模块(psm1)来存储我的函数。
然后我将这段代码插入到模块中,以便在修改模块时重新加载模块:
function reload
{
Remove-Module init
Import-Module F:\Script\init.psm1
}
这个函数的结果对我来说有点奇怪:
PS F:\Script> Get-Module
ModuleType Name ExportedCommands
---------- ---- ----------------
Script init {cpu, ie, lol, outlook...}
Manifest Microsoft.PowerShell.Management {Add-Computer, Add-Content, Checkpoint-Computer, Clear-Content...}
Manifest Microsoft.PowerShell.Utility {Add-Member, Add-Type, Clear-Variable, Compare-Object...}
PS F:\Script> reload
PS F:\Script> Get-Module
ModuleType Name ExportedCommands
---------- ---- ----------------
Manifest Microsoft.PowerShell.Management {Add-Computer, Add-Content, Checkpoint-Computer, Clear-Content...}
Manifest Microsoft.PowerShell.Utility {Add-Member, Add-Type, Clear-Variable, Compare-Object...}
PS F:\Script>
为什么函数中的第二个命令没有效果?
我还注意到,如果我在函数末尾插入“Get-Module”,该模块就会出现在列表中,就像模块在其他 Powershell 实例/会话中“运行”一样。
如果是这样,有没有办法让效果持续下去?
谢谢你!
EDIT:
我暂时通过在导入函数中添加一个参数来指定加载模块的范围来解决:
Import-Module F:\Script\init.psm1 -Global
这是处理范围的正确方法吗?