Logo-PowerShell

Ok let’s take a crack at this feature in Powershell V2 called Modules.

Modules really aren’t all that difficult when you get down to it.   It’s very much like taking a script and loading it into memory. 

Well maybe not exactly like that but the cool part is when you turn that script into a module (as long as it’s loaded) you don’t have to keep specifying the path to the script, and you could even pop it into your Profile file (since it’s just text) and add functions that make YOUR admin life better without trying.

So how hard is it?

Would you like the fact that the Powershell team GIVES you a simple sample module in the Help file?

In Example 3 (I like this one the best) under the “New-Module” help they give you this example

 

—————— Sample from Microsoft Powershell ISE Help System – Example 3 – New-Module ——————————-

C:PS>New-Module -scriptblock {$SayHelloHelp="Type ‘SayHello’, a space, and a name."; function SayHello ($name) { "Hello, $name" }; Export-ModuleMember -function SayHello -Variable SayHelloHelp}

C:PS> $SayHelloHelp Type ‘SayHello’, a space, and a name.

C:PS> SayHello Jeffrey Hello, Jeffrey

Description
———–

This command uses the Export-ModuleMember cmdlet to export a variable into the current session. Without the Export-ModuleMember command, only the function is exported. The output shows that both the variable and the function were exported into the session.

—————— Sample from Microsoft Powershell ISE Help System – Example 3 – New-Module ——————————-

 

The nice thing about this example is I can EASILY break it down so it looks like a script and you can VERY easily see how to reverse the process.

 

The same module can also look like this and is the EXACT same code.  I just haven’t left it as one line. Remember, a Semicolon ‘;’ is used to TIE the lines together.  You DON’T have to have all the code on ONE line just to make the module work.  And Honestly, If I’m writing it, I like it broken up.  It makes more sense to my eyes and SOMEBODY ELSES eyes too!

 

——————————————————————————–

# BEFORE
#
# New-Module -scriptblock {$SayHelloHelp="Type ‘SayHello’, a space, and a name."; function SayHello ($name) { "Hello, $name" }; Export-ModuleMember -function SayHello -Variable SayHelloHelp}
#

# AFTER
#

New-Module -scriptblock {

$SayHelloHelp="Type ‘SayHello’, a space, and a name."

function SayHello ($name)

{
"Hello, $name"
}

Export-ModuleMember -function SayHello -Variable SayHelloHelp

}

———————————

 

And this module if it was just a script would look like THIS

 

———————————

# This is a function

function SayHello ($name)

{
"Hello, $name"
}

# this is the script part that will use the function

$somename=’EnergizedTech’

SayHello $somename

SayHello ‘EnergizedTech’

———————————–

 

So really all you’re doing is taking a script and where you want to send variables on a regular basis, instead of assigning them IN the script or typing in literals, you’re just typing them on the command line.

 

We’ll take a slightly deeper look at how you can take this and customize this to your own needs next time.

 

Happy Holidays.  Don’t Script and Drive!

Sean
The Energized Tech