gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 19:14:13 27 4
gpt4 key购买 nike

我编写了以下 PowerShell 脚本:

function Reload-Module ([string]$moduleName) {
$module = Get-Module $moduleName
Remove-Module $moduleName -ErrorAction SilentlyContinue
Import-Module $module
}

此脚本的唯一问题是 Import-Module 仅适用于该脚本的范围内 - 它不会在全局范围内导入模块。有没有办法让脚本导入一个模块,以便在脚本完成后它仍然存在?

注意:点源是这样的: . Reload-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

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,这是我在我的系统上安装的。 v2.0 帮助位于 http://msdn.microsoft.com/en-us/library/windows/desktop/dd819454.aspx .如果可以的话,我衷心建议您购买 PowerShell v3.0,如果仅仅是因为新的 ISE。

关于powershell - 使 PowerShell 脚本在全局范围内运行 cmdlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11799697/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com