views:

22

answers:

1

Hi,

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?

Thank you

+1  A: 

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.

Keith Hill
Thank you.Where could I get reference about initialization?I put the code into the begin part of the script, followed by export-modulemember, but it doesn'w work yet.
fra
There are several ways to do this. Look at the docs on New-ModuleManifest (get-help new-modulemanifest -full). The two properties of interest are ModuleToProcess and NestedModules if you use a manifest file. If you don't use a manifest, do an Add-PSSnapin early in the processing of your PSM1 file and then Export-ModuleMember -Cmdlet the appropriate cmdlets from this snapin.
Keith Hill