2012-08-03 5 views
5

ich folgende Powershell-Skript geschrieben haben:Powershell-Skript ausführen Cmdlets in globalen Bereich

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

Das einzige Problem mit diesem Skript ist, dass Import-Module nur innerhalb dieses Skripts Umfang gilt - es ist nicht das Modul nicht importieren im globalen Umfang. Gibt es eine Möglichkeit, ein Skript dazu zu bringen, ein Modul so zu importieren, dass es nach dem Ende des Skripts weiterhin verfügbar ist?

Hinweis: dot-sourcing wie folgt: . Reload-Module MyModuleName funktioniert nicht.

+1

Haben Sie "Import-Module-Scope Global" probiert? – JohnL

+0

'Slap-Forehead' Nein. Nein, habe ich nicht. Vielleicht hätte ich die Hilfe gründlicher lesen sollen. Der eigentliche Parameter ist nur '-Global'. Wenn du das als Antwort aufnimmst, werde ich aufheben und als Antwort markieren. – Phil

+0

Fertig! Die 'Scope Global' Sache ist v3.0, denke ich. – JohnL

Antwort

4

Von Hilfe 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 fügt auch die -Scope Parameter, die ein wenig allgemeiner ist:

-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 

Hinweis: die oben Hilfe Schnipsel aus v3 sind. 0 was ich auf meinem System installiert habe. Die v2.0-Hilfe ist unter http://msdn.microsoft.com/en-us/library/windows/desktop/dd819454.aspx verfügbar. Ich würde wärmstens empfehlen PowerShell v3.0 zu bekommen, wenn Sie nur wegen der neuen ISE können.

+0

+1 Danke für die zusätzlichen Details – Phil

Verwandte Themen