首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使PowerShell脚本在全局范围内运行cmdlet

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

Stack Overflow用户
提问于 2012-08-04 00:29:01
回答 1查看 3.9K关注 0票数 5

我已经编写了以下PowerShell脚本:

代码语言:javascript
运行
复制
function Reload-Module ([string]$moduleName) {
    $module = Get-Module $moduleName
    Remove-Module $moduleName -ErrorAction SilentlyContinue
    Import-Module $module
}

此脚本的唯一问题是Import-Module仅适用于该脚本的作用域-它不会导入全局作用域中的模块。有没有办法让脚本导入一个模块,让它在脚本结束后仍然存在?

注意:像这样的点采购:. Reload-Module MyModuleName不起作用。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-04 03:26:42

在Powershell帮助中:

代码语言:javascript
运行
复制
-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参数,该参数稍微更通用一些:

代码语言:javascript
运行
复制
-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,这是我在系统上安装的版本。v2.0帮助可在http://msdn.microsoft.com/en-us/library/windows/desktop/dd819454.aspx上找到。如果可以的话,我强烈建议您使用PowerShell v3.0,这仅仅是因为新的ISE。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11799697

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档