How to module and function with Powershell

For first timer, like me, in Powershell this could be a starting point for quite serious developing. Just to know: Powershell previously was made for Windows automation tasks and was used with VMware modules to automate vSphere, vRA, NSX (and many other) environments. Now this shell is available for Mac and Linux; check this for further info and installation (https://github.com/PowerShell/PowerShell).

It’s easy to automate using a single main flow:the execution starts from the first line and ends to end of the file. In this way all module should be invoked at the top of the file and a kind of result must be showed in the end or in the middle depending on conditional flows and/or what you’re reaching during execution.

But what happens when your are executing the same set of actions with a change of some variables? In every beginner programming guide there’s written: << you should use a function >>

The function construct

This is a standard template that could be used to build a function in many implementations:

 

There are 3 main parts:

  1. params: 0…more variables that could be used in the code execution. All of these are not available for other function or in the main stream
  2. process: the main code
  3. the return-s

 

You could call this function in 2 ways:

  • Using param position: Myfunction “one” “two”
  • Specifying every param, in this case the order in declaration doesn’t matter: Myfunction -sTwo “two” -sOne “one”

In some cases it could happens to pass a type of variable that is different from the type required in the function… don’t worry! variable casting is performing at param level.

 

The function callback… nightmare

Ok now let’s see what happens in this function:

Output:

image

Oh my gosh! all of echoed string in the output? Ok let’s go further:

image

Ok all of the echoed will join the return in array form:

image

If you wish to write a debug string during function execution you should use Write-Host:

image

 

Writing a module

A module is the good way to distribute your code across systems, user, … The abstraction and the documentation must be at the highest possible level because, the nature of the module is to distribute an implementation for general purpose. Well! let’s se a simple module construct:

The file extension of a Powershell module must be “.psm1” and could be invoked in a ps1 file or in the shell with the command ImportModule:

image

NOTE: if your developing and testing in the module, be sure you unload and re-import the module. Otherwise, new changes are not available until you correctly remove module from memory (the Import-Module skip additional import with the same module to avoid the duplication in memory).

   Send article as PDF