VMware Cloud Community
internetrush
Contributor
Contributor

PowerShell Modules / PSSnappin in Enterprise environment

i'm attempting to setup a 'Default Powershell Profile' for my fellow colleagues. I have so far wrote a function to determine the version of powercli and then load the module or pssnappin as appropriate. Another function then calls this function and connects to default server locations depending on business rules. I place this in Vmware-Resources.psm1. I then have an auto-updating profile that people setup when they arrive at the company call it defaultProfile.ps1

1.) DefaultProfile.ps1 Imports VMwareResources.psm1

    2.) VMwareResources.psm1 contains function named Add-PowerCLI and, connect-vcenterserver

            3.) Default Profile calls Connect-VCenterServer which then calls Add-PowerCLI which imports module vmware.vimautomation.core and connects to different campuses depending on gui selection

After this third step, i successfully connect to vcenter server; however, the resources in the module are not present (i.e. Get-VM or Get-VMhost).

Basically the problem is as such: one cannot load a module within a module and get the commands from the second module.

Anyone know how to get around this?

Reply
0 Kudos
9 Replies
LucD
Leadership
Leadership

I assume you are using at least PowerShell v3 ?

What is the value fo $PSModuleAutoloadingPreference ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
internetrush
Contributor
Contributor

$PSautoLoadingPreference is null when I load the ISE. Yes, we have must moved to PS version 4.0.

I'm not sure if I would want anything in here because it assumes that something is in $env:psmodulepath which I do not want. I would like everyone kept up to date via the NFS share we have setup for this.

Reply
0 Kudos
LucD
Leadership
Leadership

Just to make sure, did you include the Add-PSSnapin for the parts that are not yet fully converted to a module ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
internetrush
Contributor
Contributor

Ive tried to do both, but it still does not work for me. Even Get-VM is not available.

Have you got it to work with a 3-tier import as I have described above?

SCRIPT 1:

1.) Call a module A

2.) Call a function to connect to vi server in module A

3. ) inside function import module B (powerCLI)

4. ) inside script 1 powerCLI should now be available.

Reply
0 Kudos
internetrush
Contributor
Contributor

To be fair this never worked with PSSNAPPIN either; my work around at the moment is to place function Connect-VCenterServer and function Add-PowerCLI in DefaultProfile.ps1 and it works that way. It seems as long as you do not import within an import you are fine.

This is not a desirable state because that leaves duplicate code floating around.

Reply
0 Kudos
BenConrad
Expert
Expert

I've had the same issue.  Tried moving my vCenter connect code into a module and I don't get access to the CMDLets from the snap-in.

However, PowerCli 6.0 R1 now includes vmware.VimAutomation.Core as a module, have you tried that option?

VMware vSphere PowerCLI 6.0 Release 1 Release Notes

Ben

Reply
0 Kudos
Craig_Baltzer
Expert
Expert

If you dealing with modules (not snapins) then try sticking a -Global on your import-module command to make the contents visible to the parent scope. There was a feature request filed against PS to have the same type of flag added to add-pssnapin but it never happened (arguments just went back and forth on the con of "junking up" the global space vs pro being able to do what you want). So you're SOL with snap-ins you load from a function contained in a module (i.e. they'll only be available to functions within the module).

Reply
0 Kudos
internetrush
Contributor
Contributor

Sounds promising but it doesn't work.

Tried both -Global:$true and -Global

Reply
0 Kudos
Craig_Baltzer
Expert
Expert

Was -Global ineffective with PS4 and PowerCLI 6.0 U2?

Reply
0 Kudos