More PowerShell tricks

get-website | Add-Member-MemberTypeScriptProperty-NameOutageEnabled-value { test-path$this.physicalpath } -PassThru | select name, OutageEnabled

Here you’ll find 3 more PowerShell tricks I’ve learned over the weekend. See what properties are available First, here is a really simple one.  As you work with PowerShell, you’ll constantly run into new cmdlets that return some kind of object and you want to know what kind of properties are available to work with.  The first way I learned to do this was by simply piping the results to Format-List.  The problem though is if there are many objects to return, this will quickly overwhelm your screen with duplicated data as each row has the same columns.  I then started piping to Get-Member which shows the actual properties names only once.  This is much better but has the problem of not showing you the values of any of the properties which can make it harder to figure out what’s available.  I’ve now figured out a simple method that combines the ability to see all the properties available while at the same time giving you an indication of what those variables contain.

Get-Process | select * -First 1

Elegant I must say. Pipe your desired object to select, grab everything but only grab the first entry so you don’t end up duplicating anything. Add a column to an existing object Here, I wanted to add a new column to the output of Get-Website (IIS cmdlet for viewing websites configured in IIS) called OutageStatus that checked if a specific file was present on the server and make it true or false depending on the results. I didn’t want to make a new object though. I was able to accomplish these goals like this:

get-website | Add-Member -MemberType ScriptProperty -Name OutageEnabled -value { test-path $this.physicalpath } -PassThru | select name, OutageEnabled

What we’re doing is here is taking and object and “adding a member” to it with a type “ScriptProperty”. This allows us to assign custom code to this column effectively. Note that in order for this to work you also must use the –Passthru parameter as well as explicitly include your new column on your select at the end of the pipe. Expand property of object while still showing the other properties This one ended up taking me hours to come up with a solution for so I figured I’d share.  The IIS team includes a PowerShell cmdlet called Get-WebSite that provides the name of all web sites on the system along with their physical path and host headers.  Unfortunately, the host headers include the port information appended to the front it which I didn’t want.  The PowerShell below extracts out so that we just get the base URL.  This more generally applies to any situation where you have a property that must be expanded with –expandproperty but you also want to display the other data related to it.  Note that for whatever reason the IIS team decided to rely on collections for many of its properties which makes them annoying to deal with in PowerShell.  Annoying but no longer impossible.  This script works by using the name/expression syntax and drilling into the collection.  Once there, it uses the split function to take everything to the right of the last : which happens to the the URL.

Get-Website | 
Select name, physicalpath, @{n="HostHeader";e={`$_ | select -expandproperty bindings | select -expandproperty Collection | 
    select @{n="ChildHeader";e={($_ | select -expandproperty bindingInformation | %{$_.split(':')[2] } ) } } | 
    where { $_.ChildHeader -ne"" } | 
    select -expandproperty ChildHeader 
} } | 
Where {$_.HostHeader}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.