fra
fra

Reputation: 3888

PowerShell Add-PSSnapIn from an advanced (cmdlet) function

I would like to create an advancedmodule with a cmdlet function which performs some logic and adds some pssnapins. This is the code:

function Add-DefaultSnapIns
{
    [CmdletBinding()]
    param()
    begin {}
    process {
        # ...
        Add-PsSnapIn SnapInName
    }
    end {}
}

export-module -function Add-DefaultSnapIns

If I invoke the function from any point (for instance, a powershell prompt), the operation succeeds, but the snapin is not available outside of the scope of the function. The snap-in appears registered, but none of its functions have been exported to the global scope. How could I solve it?

Upvotes: 2

Views: 1199

Answers (1)

Keith Hill
Keith Hill

Reputation: 202042

Well the idea is that Modules are self-contained and don't spill too much of their "stuff" into the global session space except the cmdlets, functions and aliases they export. It might be better to add the snapins yourself as part of the module initialization and then export those snapins' cmdlets via Export-ModuleMember.

Upvotes: 4

Related Questions