我编写了以下PowerShell脚本:
function Reload-Module ([string]$moduleName) {
$module = Get-Module $moduleName
Remove-Module $moduleName -ErrorAction SilentlyContinue
Import-Module $module
}
此脚本的唯一问题是Import-Module仅适用于该脚本的范围 - 它不会在全局范围内导入模块。有没有办法让脚本导入一个模块,以便它在脚本完成后保持不变?
注意:像这样的点源:. Reload-Module MyModuleName
不起作用。
答案 0 :(得分:5)
来自Powershell帮助:
-Global [<SwitchParameter>]
Imports modules into the global session state so they are available to all commands in the session. By
default, the commands in a module, including commands from nested modules, are imported into the
caller's session state. To restrict the commands that a module exports, use an Export-ModuleMember
command in the script module.
The Global parameter is equivalent to the Scope parameter with a value of Global.
Required? false
Position? named
Default value False
Accept pipeline input? false
Accept wildcard characters? false
v3还添加了-Scope参数,这更常见:
-Scope <String>
Imports the module only into the specified scope.
Valid values are:
-- Global: Available to all commands in the session. Equivalent to the
Global parameter.
-- Local: Available only in the current scope.
By default, the module is imported into the current scope, which could be
a script or module.
This parameter is introduced in Windows PowerShell 3.0.
Required? false
Position? named
Default value Current scope
Accept pipeline input? false
Accept wildcard characters? false
注意:以上帮助代码段来自v3.0,这是我在系统上安装的内容。 http://msdn.microsoft.com/en-us/library/windows/desktop/dd819454.aspx提供了v2.0帮助。如果可以的话,我衷心建议您使用PowerShell v3.0,如果只是因为新的ISE。