PowerShell 2.0 & Modules

I have a scenario where I want/need to

  • Modularize my code, so that bits and pieces can be updated easily over time.
  • Store those bits and pieces on a network and access them using UNC paths
  • Execute the main script a number of ways: SCCM with arguments, shortcut's with arguments and Windows Run from the registry. The former two as an Administrator, the latter as a user.
  • Have a hash table of global scope that is populated by a Function in a module, and used by a function in another module.
  • Use PowerShell 2.0, as my customer base is almost exclusively in Windows 7, and very resistant to installing newer versions of PowerShell.

As I started learning PS I verified that I could indeed Load a module from a UNC path, but the code I tested did not include Functions in that module. Now as I progress I am running into problems, specifically that my function call "not recognized as the name of a cmdlet, function, script file, or operable program."

So, I am wondering if I am barking up the wrong tree and I either need a newer version, or dot referencing is the only option, or...? My hope is all of this is possible and I am just shagging the code a bit.

Crossed fingers someone can verify the above is all possible, or offer an alternative solution.

Thanks!
Gordon

January 28th, 2014 11:25am

As long as the Import-Module command is successful, it shouldn't matter what is in the module itself. If it exports functions, you should have those functions available in your PowerShell session.

If you're getting the "not recognized as the name of a cmdlet, function", etc error, then I suspect one of two things is happening:  Either you haven't imported the module in the session where you tried to run the command, or the module doesn't export the function you're trying to call.  (Which functions are exported can be controlled either by a module manifest file, or by Export-ModuleMember calls in a script module.  By default, if there is no manifest information and if no calls to Export-ModuleMember are made, all functions in a module are exported, but no aliases or variables.)

Free Windows Admin Tool Kit Click here and download it now
January 28th, 2014 11:47am

David, thanks again! So

HelloWorld.psm1 is

Function Hello-World
{
   $a = new-object -comobject wscript.shell 
   $a.popup("Hello World!",)
}

Export-ModuleMember -Function "Hello-World"

And ImportModuleTest.ps1 is

Import-Module "\\PX_SERVER\Support\Scripts\Px 3.0\PS Dev\HelloWorld.psm1"
Hello-World

Seems simple enough even I could trouble shoot it, but it seems alas no. Any thoughts? One other bullet point, is there any way to both run the parent script the various required ways, and also from within the ISE? 

Thanks!
Gordon

January 28th, 2014 12:07pm

You've got a syntax error in your call to $a.Popup (an extra comma at the end of the parameter list), which would prevent that module from loading. Import-Module should be giving you an error, something like this:

Missing expression after ','.
    + CategoryInfo          : ParserError: (,:String) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingExpressionAfterToken
 
Import-Module : The specified module '\\PX_SERVER\Support\Scripts\Px 3.0\PS Dev\HelloWorld.psm1' was not loaded because no valid module file was found in any module directory.
At line:1 char:1
+ Import-Module \\PX_SERVER\Support\Scripts\Px 3.0\PS Dev\HelloWorld.psm1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (\\PX_SERVER\Support\Scripts\Px 3.0\PS Dev\HelloWorld.psm1:String) [Import-Module], FileNotFoundException
    + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand


Free Windows Admin Tool Kit Click here and download it now
January 28th, 2014 12:39pm

Well, I had that error, and two others in my more involved test example! And once you pointed out the one, the others where easier to find! Maybe I should not jump into new stuff when tired and not caffeinated. So, this is working and I am done for the night. Singleton/Constant hash tables can wait! ;)

Thanks again David!
Gordon

January 28th, 2014 3:48pm

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

Other recent topics Other recent topics