Microsoft – PowerShell – Extending via Script Modules

Script Module

In PowerShell v2.0, one can promote code re-use by packaging PowerShell scripts into source code modules.

The steps are iterated below:

  1. Write your application in a modularized fashion
  2. Identify the folders referenced in the PSModulePath environment variable
  3. Choose a base folder underneath one of referenced PSModulePath
  4. Based on your package name, create a corresponding folder underneath your chosen PSModulePath folder
  5. Copy the module underneath the folder

More detailed info:

1) Write your application in a modular fashion

  • Have well defined functions / methods
2) Save the file based on a general module name
  • In our case, we called our package mybigdata.psm1

3) Identify the folders referenced in the PSModulePath environment variable

From OS Command line issue:
set PSModulePath

Response:
set PSModulePath=C:\Windows\system32\WindowsPowerShell\v1.0\Modules\

4) Choose a base folder underneath one of your referenced PSModulePath

  • In our case, the only folder referenced in PSModulePath is the system folder (C:\Windows\system32\WindowsPowerShell\v1.0\Modules\)
  • To reduce possible corruption and yet accommodate PowerShell’s standard we chose to use the <documents>\WindowsPowerShell\Modules standard.  Based on the OS, the folder’s name varies a bit:

      Windows XP: %userprofile%\my documents\WindowsPowerShell\Modules

       Windows Vista +: %userprofile%\documents\WindowsPowerShell\Modules

4) Based on your organization and package name, create a corresponding folder underneath your chosen PSModulePath folder

  • For organization name, Using the old Java nomenclature we called it myBITech.com
  • On our Windows 7 machine & based on our convention,  we created the folder as %userprofile%\documents\WindowsPowerShell\Modules \myBITech.com\mybigdata

5) Copy the module underneath the folder – we thus end up as follow:

C:\Users\dadeniji\Documents\windowspowershell\modules\myBITech.com\mybigData

6) Update the environment variable

set PSModulePath=C:\Windows\system32\WindowsPowerShell\v1.0\Modules\;%USERPROFILE%\documents\WindowsPowerShell\Modules\myBITech.com;

We have copied a couple of sample modules below:

  • importData.psm1 (the module file)
  • mainRead.ps1 (the invoking file)
# this is a sample module file
#file name is importData.psm1
function import-Data( $filename)
{

   #import-csv
   #http://technet.microsoft.com/en-us/library/dd347665.aspx

   $header = "ContactID", "FirstName", "MiddleName", "LastName", "EmailAddress", "Phone";

   $fileRead = Import-CSV -Delimiter "," -Path $filename  -Header $header;

 # prep computed column
 $fullname = @{expression={ [string]($_.Firstname + " " + $_.Lastname) };     label='fullname';width=40;alignment='left'}

 # display contact ID & full name
 $fileRead | Format-Table ContactID, $fullName, emailAddress

}

# this is a sample main file
# mainRead.ps1

   Import-Module importBigData

   $file = "contact.csv";

   "file name is " + $file

   import-Data $file

References:

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s