PowerShell is a great tool for collecting information. Here’s some direct tips to try to minimize the pain of coding, or at least make it (kind of) readable. I’ve seen some blogs out there that turn some people way just because their script is working. But, that’s the beauty of PowerShell. No matter how you build the code, if the end results is what you’re looking for, then good for you.
I hope later on you realized that your script can be improved, and you will feel the POWER of POWERSHELL!!!
Briefly, I will be covering PSObject, Hash table, and a bonus Array objects
Check this out. Here’s an example of some script lines building a PowerShell variable object from two Cmdlets executing two different WMI classes: Win32_OperatingSystem and Win32_BIOS.
Now, the above code will work. In my opinion using Add-Member is already old school.
At the end, I checked for the PS variable ($obj) type, using the “.gettype()” method. This confirmed this variable a PSCustomObject.
I will show you other ways to get a similar results, and hopefully with less typing. Maybe, it will look nicer too. The goal is, as you progress using PowerShell for you day-to-day activity, you will continue to improve your existing code.
As you can see, here I’m first collecting all the information by building my PowerShell variables in $wmi1 and $wmi2 which has different results. Then, I’m combining the values I want from my $wmi1 and $wmi2 using the “Select” command.
So we made some improvements from originally 9 lines of code cut down to 5 lines. And, notice that the new PS variable type still is “PSCustomObject”.
Now, let me show you another very nice variation that is intended to replace the use of “Add-Member” command. We are going to use the Hash table concept to create our “PSCustomObject” variable.
Here’s another way which is better looking with less typing than our first example.
As you can see, this PowerShell code have 10 lines but, for me, it looks cleaner and easy to read. Basically, we are breaking the code in three sections:
1. Create the variable objects holding our results.
2. Build the hash table where we combine all our selected objects.
3. Convert the hash table to a “PSCustomObject”, so we can display the results in a proper format. (see next pic.)
Check in both improvement samples (1 & 2), I’m showing how you can create/change the label of your variable property in the “Select” command, and when you are creating a Hash table.
Bonus Script: Using Arrays objects to Collect information
Here’s a snippet you could use the gather information about your SQL Servers using .NET SMO SQL Enumeration to get all SQL names on the network. To run this script you need to have SQLPS loaded in you PowerShell session and have proper server Windows Authentication.
I hope you’ll like it:
Hidden Tip: Notice, I have created an Array object ($SQLInfo) in line 5, in which the “ForEach()” command will be use to add the results from each element read from the server object ($SQLSvr).
BIG REMINDER!!! Remember that your PS variables will probably have more values you can display in your results using the “.. |Select val1, val2, val5…”. Use the Get-member command to exposed them. (see next pic.)
That’s it for now!!