Tags


Powershell

First off, I didn’t figure this out.  Somebody told me.  But this is priceless.

Do you know how EASY it is to make a module in Powershell?

In your “DocumentsWindows Powershell” Folder there is most likely a folder called “Modules”

image

Go into that folder, make another folder and call it something useful like “FruityJello”.

At this point, I fully recognize that NO ITPRO worth his SALT would seriously make an important module for his office called “FruityJello” but I’m just having a bit of a blank spot for some good “Names for Folders” today.

So within our “FruityJello” folder were going to place a text file in it and call it (Wait….. pause…. ACTION!) FruityJello.PSM1

So now you have the BASE of a module.  It won’t actually do a thing.  It’s empty.  But it is the Base of “Script Module”.   So what do you put in there?

Well pretty much everything from your Profile in the way of Functions can sit in here for a start.

Let’s “ASSume” you have a blank $Profile and just want to Try something in a module and all you have are scripts.   There’s really VERY little difference between a script and a Function in Powershell 2.0.   I’ll show you a simple script.  Really simple.   Just so you can see the pieces that change.

——————– DUMBSCRIPT.PS1 ——————-

$ARGS[0]
WRITE-HOST $ARGS [0]
$ARGS[0] | Get-Member
WRITE-HOST "Wasn’t That IMPRESSIVE?!"

——————– DUMBSCRIPT.PS1 ——————-

So as you can guess, you call up this script and tack on a bunch of arguments and all this does is echo them off and do something completely unimpressive

Now this script as a function?  WATCH

—————- DUMBER Function ——————

Function Global:Write-Silly( $AnArgument ){
     $AnArgument
     WRITE-HOST $ArArgument
     $AnArgument | Get-Member
     WRITE-HOST "Wasn’t That IMPRESSIVE?!"

}

—————- DUMBER Function ——————

So now we have a function identical to the script called “WRITE-SILLY”.  All we had to do was prepend the original Script Block with a

Function Global:NameOfFunction( $parametera $parameterb $parameterc ) {

Swap out the variables for wherever there are (Or are not) needed in the original script block and top it off with a Parentheses on the end

— > } < —

So now that you have a function in your hands, paste that code into the “FruityJello.PSM1” file (Use Notepad or whatever you’re most comfortable with) and save the file

Now fire up Powershell and execute

IMPORT-MODULE FruityJello

You’ll get very little (actually no fanfare) but when you key in WRITE- and start tapping away on tab you’ll soon see your new function (Badly written too!) called WRITE-SILLY filling out with the AutoComplete.  

image

It even works like other Commandlets including when you execute “HELP” against it

image

If you check earlier an Earlier Blog post about “Comment Based Help” you can actually fill out the Function so it has full help and examples if you’re distributing it to staff or clients.

But there you have it, a basic (if not very silly) module for you to learn from.

Remember.  The Power of Shell is in YOU

Sean
The Energized Tech

Advertisements