Confused by modules - not importing automatically in my scripts

VMware recently converted their PowerCLI product from PSSnapin format to modules.  So I am trying to update my scripts to use these new modules instead of the old snapins.  If I open a plain old PowerShell console, I can run the PowerCLI cmdlets no problem with having to "do anything" - I don't have to import-module, it just magically works.  However if I run those same cmdlets in a script I wrote - it tells me unknown command.

In order to get my script to work, I have to import the module first: "Import-Module VMware.VimAutomation.Core"

Now this is not a big deal - just one extra line.  But I am confused why I have to do this.  Why does a PS console work fine without have to use import-module, but a script does not?  I am pretty new to modules - everything I have used in the past has always been snapins (PowerCLI, Exchange, and Quest ActiveRoles Management Shell).  Everything I have read suggests modules are plug-n-play.  Do scripts need to be handled in a different way?  One thing I did notice is that the VMware module shows nothing listed for "ExportedCommands" when I run a "Get-Module -listavailable" - does this make it required to import first?  If so, why don't I have to do this in the PS console and only in scripts?

Thanks

NK

May 19th, 2015 3:52pm

In V3 and later the import is automatic.  In V2 and earlier you have to manually import the modules.

For Quest and others you get a pre-loaded console host.  The snapin has just been loaded for you.  If you open a generic coonsoole you have too manually add the snap-in.

Free Windows Admin Tool Kit Click here and download it now
May 19th, 2015 4:19pm

Thanks for the info.  But I am using V4 and it only fails in scripts.

NK

May 19th, 2015 4:44pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics