Petri Newsletter Sign-up
Tech Tuesday

Subscribe to Tech Tuesday, the latest insights from Petri.com for IT Pros.

    See All Petri Newsletters

    Extending Objects in Windows PowerShell, Part 3

    Posted on by Jeff Hicks in PowerShell

    In the last part of this series, I demonstrated how to extend results that you might get back from a cmdlet. As you hopefully know, you can create an object out of thin air in PowerShell by using the New-Object cmdlet.

    Extending Objects in PowerShell Article Series:

    Revisiting How to Make an Object in PowerShell

    To create an object in PowerShell, all you need to do is specify a hashtable of property values.


    You can also use the [PSCustomObject] type accelerator.


    In either event, you get an object.

    Our new object in Windows PowerShell. (Image Credit: Jeff Hicks)
    Our new object in Windows PowerShell. (Image Credit: Jeff Hicks)

    This type of object can also be extended by using the same techniques I showed earlier.


    I went ahead and added a script property and a few methods.

    Our new script properties and methods. (Image Credit: Jeff Hicks)
    Our new script properties and methods. (Image Credit: Jeff Hicks)

    I can use this object to get some useful management information from the local computer.

    Using the uptime property to gather meaningful information. (Image Credit: Jeff Hicks)
    Using the uptime property to gather meaningful information. (Image Credit: Jeff Hicks)

    Creating a Custom Server Management Framework

    With this in mind, it seemed to me that I could create a custom server management framework. Here’s how this might work.

    First, I need a computer name.


    Next, I’ll outline a few standard properties.


    This is probably enough information to create an object.


     

     

     

     

    Now to add some custom methods I can use as management tools.


    There’s one item that I haven’t show you, and that’s a property set. With a property set, you can reference a collection of properties by using a single name.


    I hope that you realize that there’s no limit to the type of information and methods you could include here. Although one step I think I should have is an option to refresh the object. Should something change, I need to be able to update any static properties.


    I’ll test by rebooting the server.


    Using our refresh method in Windows PowerShell. (Image Credit: Jeff Hicks)
    Using our refresh method in Windows PowerShell. (Image Credit: Jeff Hicks)

    What have we ended up with in terms of a management object?

    By default I get all of the properties, including my alias. It might be more useful to define a default display. We can do this by defining a custom type for this object.


    These commands insert a new type name for my object.

    Now that I have a unique type name, I can use Update-TypeData and define a default property set.


    Notice the change?

    The other properties and methods are still available should I need them.

    In the final article, we’ll take one final step and explore how to use this techniques to manage at scale.

    BECOME A PETRI MEMBER:

    Don't have a login but want to join the conversation? Sign up for a Petri Account

    Register