5

次の PowerShell スクリプトを作成しました。

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

このスクリプトの唯一の問題は、Import-Module がそのスクリプトのスコープ内でのみ適用されることです。グローバル スコープではモジュールをインポートしません。スクリプトがモジュールをインポートして、スクリプトが終了した後も残るようにする方法はありますか?

注: so: のようなドットソーシング. Reload-Module MyModuleNameは機能しません。

4

1 に答える 1

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 のものです。v2.0 のヘルプはhttp://msdn.microsoft.com/en-us/library/windows/desktop/dd819454.aspxで入手できます。新しい ISE のためだけに、できれば PowerShell v3.0 を入手することを心からお勧めします。

于 2012-08-03T19:26:41.887 に答える