使PowerShell脚本在全局范围内运行cmdlet

使PowerShell脚本在全局范围内运行cmdlet,powershell,Powershell,我编写了以下PowerShell脚本: function Reload-Module ([string]$moduleName) { $module = Get-Module $moduleName Remove-Module $moduleName -ErrorAction SilentlyContinue Import-Module $module } 此脚本的唯一问题是导入模块仅在该脚本的作用域内应用-它不在全局作用域中导入模块。有没有办法让脚本导入一个模块,以便

我编写了以下PowerShell脚本:

function Reload-Module ([string]$moduleName) {
    $module = Get-Module $moduleName
    Remove-Module $moduleName -ErrorAction SilentlyContinue
    Import-Module $module
}
此脚本的唯一问题是导入模块仅在该脚本的作用域内应用-它不在全局作用域中导入模块。有没有办法让脚本导入一个模块,以便在脚本完成后留在模块中


注:点源代码如下:
。重新加载模块MyModuleName
不起作用。

来自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
-Global[]
将模块导入全局会话状态,以便会话中的所有命令都可以使用这些模块。通过
默认情况下,模块中的命令(包括来自嵌套模块的命令)将导入到
调用方的会话状态。要限制模块导出的命令,请使用导出模块成员
脚本模块中的命令。
全局参数相当于值为全局的范围参数。
必修的?假的
位置?命名
默认值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
-范围
仅将模块导入指定的范围。
有效值为:
--全局:可用于会话中的所有命令。相当于
全局参数。
--本地:仅在当前范围内可用。
默认情况下,模块导入到当前作用域中,该作用域可以是
脚本或模块。
此参数在Windows PowerShell 3.0中引入。
必修的?假的
位置?命名
默认值当前范围
接受管道输入?假的
是否接受通配符?假的

注意:以上帮助片段来自v3.0,这是我在系统上安装的。有关v2.0帮助,请访问。如果可以的话,我衷心建议您使用PowerShell v3.0,即使只是因为新的ISE。

您是否尝试过导入模块-全局范围?
拍拍前额
否。不,我没有。也许我应该更彻底地阅读帮助。实际参数只是
-Global
。如果你把它作为答案,我会投票并标记为答案。完成!我想,
-Scope-Global
应该是v3.0。