To use PSObject Add-Member or not…

I was playing around creating a PowerShell script with the intention of add items to a custom PSObject using the Add-Member. Well, I couldn’t get it to work or maybe I was still doing something wrong. But then, I came up with a (kind-of) unorthodox method that I will share with you. Please, don’t tell anyone:

So, I going to duplicate the same functionality in a different way without creating a new PSObject and not using Add-Member. I’m going to let PowerShell to create the PSObject for me and work a little with the Select-Object. Here’s my version:

In this script, I’m using the SMO SQL enumerator to collect the SQL Server names so we can use the “foreach” to collect the information from each server into the output array “$SQLinfo”. After building the output variable then you can save the results in different output format such as: CSV, TXT, XML, or event back to a SQL table.

This is another way to collect information and let PowerShell do the rest for you! PowerShell gives Power to the scripters.