Updates from May, 2009 Toggle Comment Threads | Keyboard Shortcuts

  • Chris 6:22 pm on May 27, 2009 Permalink | Reply
    Tags: modules, Powershell Advanced Functions, Powershell Modules   

    Introduction to Powershell Modules w/ Advanced Functions 

              If you been using powershell you heard these terms Modules and Advanced functions . What are they ? Why are they important ? Why would I want to use them ? Do I need to use them ? As you can see allot of questions.

              Modules have replaced Pssnapins in V1 which was compiled code usually C#  that gave powershell more functionality .This was a way to add more cmdlets . An example of this is System Center Operations Manager (SCOM) pssnapin . When you install the console along with powershell you get the pssnapin for SCOM which gives you a bunch of cmdlets.

              Now with powershell V2 we have what are called modules . These modules can be any function that you have written in powershell. That is the beauty of modules they are basically scripted cmdlets that you create . Now anyone can create them. You don’t need to fire up a compiler and bang out C# code to get the extra functionality you need .So if you take any function your written in Powershell and changed the extension to .psm1 then you have a module .

               They are important because they can save you time by re-using specific scripts to do repetitive tasks . When you import your module its loaded into your powershell session sort of like dot sourcing your script. First you need to know where are the modules located . To find out where your modules folder is located enter this command in your powershell command prompt .

    dir env:psmodulepath

    psmodulepath is an environment variable. Please see this blog for more information http://tfl09.blogspot.com/2009/01/modules-in-powershell-v2.html

    You should see something like this …

    Name                           Value
    —-                           —–
    PSMODULEPATH                   C:\Documents and Settings\username\My Documents\WindowsPowerShell\Modules;

    There is also a location in your %windir%\system32\Windowspowershell\modules directory.

     

    You probably be using the user specific directory.

    So now that you know where the modules are here is an example of a module I created :

    I will call it Get-OSInFo.psm1

    Here is what it looks like using Advanced Functions:

     

    Function Get-OsInfo {

    <#
    .Synopsis
     Gets basic Os information Using WMI OperatingSystem Class.
    .Description
     Gets basic Os information Using WMI OperatingSystem Class.
    .Parameter ComputerName
     Name of the of the Computer you want info on.

    .Example
     PS> get-OsInfo -computer serverA
    .Link
        about_functions
        about_functions_advanced
        about_functions_advanced_methods
        about_functions_advanced_parameters
    .Notes
    NAME:      Get-OsInfo
    AUTHOR:    Name
    LASTEDIT:  05/26/2009

    #>

    param(
    [Parameter(Mandatory=$true)]
    [string]$computerName = “localhost”
    )

    gwmi -class Win32_OperatingSystem -Computer $computername

    }

                Advanced Functions is new for V2 . What makes them so nice to use is the built in help . Notice the syntax. Also notice the parameter section . This is a new way to specify parameters. Note that I have a parameter called Computername default to local host and it is mandatory. You can go crazy with parameters but this is just an intro so more to come in the future . The last portion is the standard WMI call to OperatingSystem .

     

    Now lets put this all together 🙂

    Place this file in your modules folder but create a folder called get-osinfo and put the psm1 file in there. I use the same name as the script for the folder . (I am not sure if that is necessary but it is best practices ).

     

    Now that we have the module file in the correct place open up your powershell command prompt.

    We are going to import the module …

    import-module get-osinfo.psm1

    Like I said before this is like dot sourcing a script (. .\scriptname.ps1). Now that we imported it lets check it out ..

    Before we do I wanted to point out that you can have an umlimited number of functions in this psm1 file . So what if you got a bunch of functions for a third party how would you know what functions are in this file ?

    A somewhat easy way would be to use this command (This is after you imported the module )

    get-command -commandtype Function

    CommandType     Name                                                     Definition
    ———–     —-                                                     ———-
    Function        A:                                                       Set-Location A:
    Function        B:                                                       Set-Location B:
    Function        C:                                                       Set-Location C:
    Function        cd..                                                     Set-Location ..
    Function        cd\                                                      Set-Location \
    Function        Clear-Host                                               $space = New-Object System.Management.Au
    Function        D:                                                       Set-Location D:
    Function        Disable-PSRemoting                                       …
    Function        E:                                                       Set-Location E:
    Function        Enable-PSRemoting                                        …
    Function        F:                                                       Set-Location F:
    Function        G:                                                       Set-Location G:
    Function        Get-OsInfo                                               …
    Function        H:                                                       Set-Location H:

    You can see we see our get-Osinfo function listed .

     

    Now that we know that is the function we need …we need to know how to run it (Remember the built in help for the function we created ?? )

    Well lets get help the same way you would get help on any of other function or cmdlet.

    help get-osinfo -det

    NAME
        Get-OsInfo

    SYNOPSIS
        Gets basic Os information Using WMI OperatingSystem Class.
    SYNTAX
        Get-OsInfo [-computerName] [<String>] [-Verbose] [-Debug] [-ErrorAction [<ActionPreference>]] [-WarningAction [
        nce>]] [-ErrorVariable [<String>]] [-WarningVariable [<String>]] [-OutVariable [<String>]] [-OutBuffer [<Int32>
        ameters>]
    DETAILED DESCRIPTION
        Gets basic Os information Using WMI OperatingSystem Class.
    PARAMETERS
        -computerName
            Name of the of the Computer you want info on.

        <CommonParameters>
            This cmdlet supports the common parameters: -Verbose, -Debug,
            -ErrorAction, -ErrorVariable, -WarningAction, -WarningVariable,
            -OutBuffer and -OutVariable. For more information, type,
            “get-help about_commonparameters”.

        ————————– EXAMPLE 1 ————————–

     

        PS> get-OsInfo -computer serverA
    REMARKS
        To see the examples, type: “get-help Get-OsInfo -examples”.
        For more information, type: “get-help Get-OsInfo -detailed”.
        For technical information, type: “get-help Get-OsInfo -full”.

     

    Isn’t that great !!!

    Lets run it …

    Get-Osinfo -computername ServerA

    (Notice how the parameter variable computername is used …)

    You can even pipe this to the select cmdlet  like so

    Get-Osinfo -computername ServerA | select Name,Organization,BuildNumber

     

    To sum up … Modules helps you create specific functions to help you with different tasks . Microsoft made these scriptable so that any one can create them . By using advanced functions you get the built in help and special parameter settings for your functions .

    It doesn’t stop here … there are also module manifests . Also you can enable different functions in your modules for lets say security reasons . This was just an introduction.

     

    I hope you enjoyed reading this blog 🙂

     

    Chris

    Advertisements
     
    • Jeff 10:29 pm on April 26, 2010 Permalink

      Can you show me how to build cmdlet ?
      I am trying to build a cmdlet which is to when system boots up number lock turns on by default.
      Can you give me some hits?
      Thank you so much!
      Cheers
      Jeff

    • Thomas Lee 5:58 am on October 3, 2011 Permalink

      Thanks for the link to my site. Modules are a rich and complex feature of PowerShell 2. As you note, module manifests provide significant additional functoinality, such as teh ability to include type an format XML, specify binary files for compiled cmdlets, etc.

  • Chris 8:03 pm on May 5, 2009 Permalink | Reply
    Tags: $ofs,   

    Powershell Using $OFS 

    Hello ,

    $OFS which is a special variable in powershell . OFS stands for Output field separator . You would use this to separate objects in your array for example :

     

    PS H:\> $numbers = 1,2,3,4,5
    PS H:\> $numbers

    1
    2
    3
    4
    5

     

    What if you wanted to separate these let say with a semi colon . You can use the $OFS variable..

    PS H:\> $ofs = “;” ;[string]$numbers
    1;2;3;4;5 

     

    This could be useful lets say if your getting users out of AD to setup an e-mail in outlook .

    Not to bad … 🙂

    For More info :

    http://blogs.msdn.com/powershell/archive/2006/07/15/What-is-OFS.aspx 

     

    Have Fun !

    Chris

     
    • Is dis 10:02 am on June 20, 2014 Permalink

      exists for lines ?
      what I mean:
      what separates lines by default is “\r\n”
      what I want:
      change globally (in a script) this default to “\n”

    • Chris 10:43 am on June 20, 2014 Permalink

      Hi … I do not really understand what your asking … Can you explain or give an example ?

c
Compose new post
j
Next post/Next comment
k
Previous post/Previous comment
r
Reply
e
Edit
o
Show/Hide comments
t
Go to top
l
Go to login
h
Show/Hide help
shift + esc
Cancel