Essential PowerShell: Name your custom object types

PowerShell is a very flexible scripting language that allows users to dynamically create and/or extend objects with additional methods and properties.  This is very useful when you’re trying to build up a rich data set with all of the properties or methods you need.  One important thing that is often overlooked when people are writing scripts that do this is that they can also give those objects a type name.  Why is this type name important?  Three reasons:

  1. If you want to further extend those types automatically via a type data file, you’ll want a unique type name so that only the appropriate objects are extended.
  2. If you want to apply specific default custom formatting to those types via a format data file, you’ll want a unique name so that only the appropriate objects are formatted this way.
  3. If you want to associate specific links and actions with your custom object type in PowerGUI, you’ll want a unique name so that you don’t get links and actions associated with other types.

In practice there are only two use cases where I need to create a custom object type name, and I apply different names depending on the scenario I’m working with at the time.

If I have created a brand new generic PSObject, then I apply a name appropriate to the object.  In this case, after I created my custom object and added the properties, I would do the following:

$myObject.PSObject.TypeNames.Insert(0,’MyObjectTypeName’)

Alternatively, if I am extending an object of a particular type, then I apply an extended type name for that object to the modified version.  In this case, after I created my custom object and added the properties, I would do the following:

$derivedTypeName = $myObject.PSObject.TypeNames[0]
$myObject.PSObject.TypeNames.Insert(0,”$derivedTypeName#MyExtensionName”)

Then if you create type or format data files, you simply need to use your new type name in appropriate XML attribute to set up the association.  Or if you’re adding functionality to PowerGUI, any links and actions you create will automatically be associated with the lowest derived object type, which will be the type name you applied to the object before outputting it in the PowerGUI data grid.

You can add as many type names as you want to your objects, so if you want to create a virtual object hierarchy with your custom object types, and then associate format or type data specifications with derived or base object types, you can do that as well through multiple calls to the Insert method on the TypeNames collection.  This can be useful if you want to share a certain set of functionality between two types of objects you are creating but in addition you want some functionality to be specific to each type and you don’t want to duplicate code.

Hopefully this will encourage you to name your custom object types and define their default properties in scripts that you share with others.

If you’re working with custom objects you should also check out the follow-up post to this that resulted from Hal’s comments on this post, titled “Essential PowerShell: Define default properties for custom objects“.

Enjoy!

Kirk out.

Share this post:

PowerGUI hits 100,000 downloads!

Last week marked a major milestone in the history of PowerGUI.  Since the first beta was released to the web last year, PowerGUI has been downloaded 100,000 times!  Thanks to everyone for their support and participation in the forums!

Still not using PowerGUI as part of your PowerShell toolbelt?  Download the latest version from www.powergui.org and see what it’s all about!

Kirk out.

Share this post:

PowerPack Challenge now open!

I mentioned earlier that there is a PowerGUI contest going on this summer called the PowerPack Challenge.  Well as of July 1st (Happy Canada Day everyone!), this contest is officially open and participants can start submitting their entries!

Interested in the details?  Read all about it here!

I look forward to seeing your contest entries!

Kirk out.

Share this post:

PowerGUI wins Best of Tech Ed 2008 IT Professional Breakthrough Product award!

Well color me a happy camper!  Windows IT Pro just announced that PowerGUI has won the Best of Tech Ed 2008 IT Professional Breakthrough Product award!  Breakthrough Product is described by Windows IT Pro as the best single product of the Tech Ed 2008 IT Professionals conference, and could be from any IT Pro award sub-category.  Do yourself a favor and download PowerGUI today!  Even better, use PowerGUI to enter the PowerPack Challenge contest!

Kirk out.

Share this post:

Why isn’t my PowerPack showing up in PowerGUI?

Here’s an interesting question that came up recently:

“I just installed PowerGUI with the Exchange PowerPack and then I installed the Exchange 2007 Management Console.  The Exchange folder isn’t showing up in the PowerGUI Console.  Can you help?”

Yes, I can help.  Actually I came across this very same scenario myself not too long ago when working with a new VM.

If you install PowerGUI today, you can let it pick which of the core PowerPacks to install.  By default, PowerGUI will pick the PowerPacks for which you have the required PowerShell snapin(s) already installed.  For example, this means that the Exchange 2007 PowerPack will be installed automatically if you have the Exchange Management Console installed on your local machine, and the Active Directory PowerPack will be installed automatically if you have the Quest AD Cmdlets installed on your local machine.

Alternatively, you can also pick which PowerPacks you want to install yourself whether you have the required snapin(s) or not by using the custom install option.  I usually take this approach when I install PowerGUI, picking all PowerPacks so that I get all of the functionality that it comes with.  Most of the time this works fine when I have already installed all of the snapins I need, but what happens if you are missing one or more of the required snapins?  When you open PowerGUI for the first time your PowerGUI tree will automatically load all of the PowerPacks you installed for which you have the required snapins.  If you installed PowerPacks for which you don’t have the required snapins, they won’t show up in the PowerGUI tree.

Sounds like an easy enough problem to solve, right?  You can just install the missing snapin and then come back into PowerGUI and…um…notice that the PowerPack isn’t there.  This is because the automatic loading of the PowerGUI tree is only done once (at the moment; this will likely change in the future).  If you don’t have the required snapin installed when that happens, the PowerPack doesn’t get loaded into the PowerGUI tree.  Here lies the problem for the individual who asked the question stated earlier.

Fortunately the solution is straightforward once you know where the missing PowerPack is stored.  When PowerGUI is installed, all PowerPacks that are installed with it are placed in the PowerPacks subfolder of the PowerGUI installation folder.  To load a PowerPack that is missing because the prerequisite snapin wasn’t installed earlier, all you need to do is select File | PowerPack Management, click Import, browse to that folder and select the PowerPack that you want to import.  Once PowerGUI has verified that you have the required snapin(s) for that PowerPack installed it will import the PowerPack and your missing folder will be available in the PowerGUI tree.  And in case you weren’t aware, this is also the same way you would extend PowerGUI with other PowerPacks that you download from the library…download the PowerPack file, right-click the root node, select Import, and then select the PowerPack file you downloaded.

Hopefully this will help as you start using more snapins to manage and automate more with PowerShell and PowerGUI.

Kirk out.

Share this post:

Interview with Dmitry Sotnikov about PowerShell and PowerGUI on IT TV

Windows IT Pro magazine recently launched their new IT TV website, a community site that hosts videos relevant to today’s IT professional.  One video that caught my interest today is an interview with Dmitry Sotnikov discussing PowerShell and PowerGUI.  Curious about what PowerGUI is and how it might help you?  Check out the interview to learn more about it!

Kirk out.

Share this post:

Enter the PowerPack Challenge!

Ok, I’m really excited about this one.  Quest Software (my employer and sponsor of all PowerGUI development) has created a PowerGUI contest!  Whether you are a newcomer to PowerShell, a seasoned PowerShell scripter or somewhere in between, why not experiment with all of the creative things you can do with PowerGUI and PowerShell and enter the PowerPack Challenge!  All you need to do to enter is create and/or update one or more PowerPacks during the contest period (between July 1, 2008 and September 30, 2008).  There are some great prizes to be won, as described on the PowerPack Challenge contest page!

So now that you know about the contest, what should you do next?  Since the contest period doesn’t start until July 1, 2008, you have some time to prepare yourself for the contest.  if you aren’t totally familiar with PowerPacks or how to create them, you can learn how to do that by watching the flash demo I created here.  If you want to browse existing PowerPacks to get an idea what is already available, you can look in the PowerPack library here.  Or if you are attending the IT Pro week of TechEd 2008, there are a lot of sessions, hands on labs, instructor led labs, and more with PowerShell content and you can learn more about PowerShell and PowerGUI by visiting the demo station in booth 1101.

And please, just like anything else I post on this blog, if you have questions don’t hesitate to ask.

Thanks and good luck with your contest entries!

Kirk out.

Share this post:

Learn about PowerShell and PowerGUI at TechEd 2008

Are you interested in PowerShell?  Would you like to see a demo of some cool free tools that can help you use PowerShell more easily today?  Visit the PowerShell demo station in booth 1101 at TechEd 2008 and learn how PowerShell and PowerGUI can help you manage your IT environment more easily!

Kirk out.

Share this post:

EnergizeIT Certification Bootcamp slide decks now available

Whether you attended the EnergizeIT Certification Bootcamp 2008 National User Group Tour or not, you might want to check out the slide decks that were presented.  Kai Axford and I presented a ton of content over a 2½ to 3 hour period.  There were three sessions presented, as follows:

Session 1: Security in a Virtual World

Session 2: Windows Server 2008 & Virtualization

Session 3: Making Security a Career

I should note that the session order changed slightly and the content was tweaked after the first few stops on the tour.  Also as Kai and I mentioned during the tour there are additional slides included in these slide decks that were hidden during the tour, so now you can get access to those slides in addition to those we presented as well.

There is a lot of great material in these slide decks, so you should take a look.  I’ve linked the session names to the slide decks, but you can also find them on the official bootcamp website in the sidebar on the right-hand side near the bottom.

Do you use virtualization in your environment?  Are you sure you’re thinking about everything you need to with respect to virtualization security?  Check out the slide deck for session 1.  There may be some things you haven’t considered that you should definitely look at.  Or maybe you just want to learn more about Hyper-V and System Center Virtual Machine Manager — that’s in there too.

Windows Server 2008 brings a lot of new features and usage scenarios to the table.  Server Manager, Server Core and my personal favorite, PowerShell were demoed, and many other features were discussed.  Check out the session 2 slide deck for more information (including the PowerShell resources I talked about during the demos).

Are you doing the job today that you want to be doing 10 years from now?  Do you want to be telling your manager in 2018 that you just finished the automated rollout of Windows Server 2018 using PowerShell? That might sound pretty cool today (it sounds pretty cool to me today), but you might want to be the manager being told that the rollout is complete instead of the one doing the rollout by then, or maybe you have something completely different in mind for your future career.  The session 3 slide deck has some great content about career management.  The emphasis is on making a security career, but the content is very generic and can be applied to making many changes in your career.  Personally, I love the slides that talk about the three rules for recession proofing and have been following those for a while now.  I used these rules last year to redirected my career towards PowerShell and that lead to me joining the PowerGUI team where I continue to work happily today.  The rules are great for recession proofing, general career management and redirection as well.  If you are considering a career change in the future, internal to your current company or external, you should seriously check the session 3 slide deck out.

If you have problems accessing any of these slide decks, let me know and I’ll try to help work it out for you.

Kirk out.

Share this post:

Provisioning users in Active Directory with PowerShell and the QAD cmdlets

As part of the EnergizeIT tour that I was on for the past three weeks, I did a quick introduction to PowerShell and then showed how the ability to automate tasks using PowerShell can make the job of an IT administrator much, much easier.  The scenario for the automation example I used was this: your HR department contacts you on Friday afternoon and tells you there are a bunch of new users coming in next week and you need to make the accounts right away.  The actual number is arbitrary because PowerShell scripts are the same for 1 user as they are for 1000000 users (exaggerated as that might be), but for my example I used a dozen users.

The point with this example was to show you how you can accomplish a task like this quickly regardless of the number of users being created and go home on time for date night, your son’s baseball game, etc.  This demonstration was very well received, and at the end of the demonstration I promised that I would post the script I wrote during the demo here in my blog.  The script that I post here may look slightly different from the script you saw me run at your tour event because I wanted to make sure shared the answer to a question someone asked me during the tour: How do I set the password for the new users when I create them?

For those of you who didn’t see the live demonstration, note that the csv file I used (C:\Users\Poshoholic\Documents\NewHires.csv) contained the following text:

FirstName,LastName,Title
Dmitry,Prosser,Software Developer 4
Oisin,Hill,Quality Analyst 1
Jeffrey,Shell,Senior Support Representative
Don,Munro,Software Developer 1
Charlie,Shaw,Project Manager
Marco,van Oursow,HR Assistant
Brandon,Russel,Product Manager
Keith,Hicks,Software Developer 3
Marc,Grehan,Product Marketing Manager
Karl,Jones,Quality Analyst 3
Kirk,Lee,Quality Analyst 1
Thomas,Sotnikov,IT Analyst 2

(FYI: if you think those names look familiar, I took the first names and last names of 12 PowerShell MVPs and mixed them up)

During the demonstration, the script below was built up iteratively within the PowerShell console so that you could see the thought process involved in creating a script that way.  In this blog post however, I’m simply going to post the finished script.  If you have questions about the demonstration or want to refresh your memory on how you could build a script like this iteratively, let me know.  The finished script to create the users from the csv file simply looks like this:

Import-Csv C:\Users\Poshoholic\Documents\NewHires.csv `
    |
Add-Member -Name Name -MemberType ScriptProperty -Value {$this.FirstName + + $this.LastName} -PassThru `
    |
Add-Member -Name SamAccountName -MemberType ScriptProperty -Value {($this.FirstName[0] + $this.LastName.Replace( ,)).SubString(0,20)} -PassThru `
    |
New-QADUser -City Ottawa -UserPassword P4$$w0rd -ParentContainer poshoholicstudios.com/users

Please note the following if you want to use this as a basis for your own script:

  1. You can copy this script from my blog and paste it directly into the PowerGUI Script Editor where you can customize it to meet your needs (be careful of the word wrap – there should be four lines of script once you paste it in PowerGUI).  Alternatively if you want the script already in a ps1 file, you can download it here.
  2. This example requires the Quest AD cmdlets to create the new users.  Once you have those installed, don’t forget to load them in PowerShell using Add-PSSnapin or in PowerGUI using Libraries in the File menu.
  3. This shows the finished script I built during the demonstration, with the addition of the assignment of the default user password value as P4$$w0rd.  Whatever you use for the default password value, it must meet your password requirements in your lab.
  4. If you want to run this without making changes, don’t forget to append -WhatIf at the end of the last line in the script.

Don’t be afraid to ask me questions about any of this, whether you need help customizing this script to make it work in your environment or just want an explanation of how the script works.  I’m always happy to help!

Kirk out.

Share this: