2012-08-03 13 views
5

He escrito el siguiente script de PowerShell:Hacer script de PowerShell cmdlets ejecutar en el ámbito global

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

El único problema con este guión es que Import-Module sólo se aplica dentro de alcance de ese guión - que no importa el módulo en el alcance global. ¿Hay alguna manera de hacer que un script importe un módulo para que permanezca después de que el script finalice?

Nota: dot-sourcing como ese: . Reload-Module MyModuleName no funciona.

+1

¿Has probado 'Import-Module -Scope Global'? – JohnL

+0

'Slap-Fronthead' No. No, no. Tal vez debería haber leído la ayuda más a fondo. El parámetro real es simplemente '-Global'. Si pones eso como respuesta, votaré y marcaré como respuesta. – Phil

+0

¡Hecho! La cosa '-Scope Global' es v3.0, supongo. – JohnL

Respuesta

4

De la ayuda 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 también añade el parámetro -Ámbito, que es un poco más general:

-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 

Nota: los fragmentos de ayuda anteriores son de la versión 3. 0 que es lo que he instalado en mi sistema. La ayuda de v2.0 está disponible en http://msdn.microsoft.com/en-us/library/windows/desktop/dd819454.aspx. Sinceramente, recomiendo obtener PowerShell v3.0 si puede, solo por el nuevo ISE.

+0

+1 Gracias por los detalles agregados – Phil

Cuestiones relacionadas