Turn On PowerShell Help Window

talkbubble-v3Here’s a little suggestion for today that might make it easier for you to use PowerShell. In PowerShell 3.0, the Get-Help cmdlet includes a terrific new parameter called -ShowWindow. When you ask for help with this parameter, you get complete help in a new window. The window is re-sizable, searchable and customizable. I love this thing because it means I can look at full cmdlet (or function) help without having to scroll back and forth in a console window.

help-window

Now for the fun part. PowerShell 3 also includes a feature where you can define default parameter values. These values are stored in a hash table where the key takes for the form “cmdletname:parametername” and you can use wildcards. In your PowerShell profile script add this line.

Now, whenever you run a help command using either Get-Help or the Help function, you will automatically get the popup help window. This won’t affect help commands like these:

You can always remove the entry from the $PSDefaultParameterValues hash table, or temporarily not use it by explicitly setting ShowWindow to $False.

Now you have no excuse for ignoring PowerShell’s help documentation.

Browse TrainSignal Courses with PowerShell

talkbubble-v3It took longer than I expected, but my latest course for TrainSignal is now available. PowerShell v3 Essentials is targeted for IT Pros with little to no PowerShell experience. This is the course that will get you up and running in short order. I developed the course so that an IT Pro could be effective with the PowerShell console, using many of the new features found in PowerShell 3.0. One of those features is the Invoke-WebRequest cmdlet. I thought I’d share a version of a demonstration I did for the course on using Invoke-WebRequest to browse the TrainSignal course catalog.

In case you didn’t know, all of TrainSignal’s courses are now delivered online on a monthly subscription basis starting at $49/month. Their site has all the pricing information you need. But you can also start with a 3 day free trial. Oh, and lessons can be viewed offline as well. Anyway…using Invoke-WebRequest I can “scrape” the TrainSignal courseware page using PowerShell. Here’s my sample script.

The script saves the results from Invoke-WebRequest to a variable. In looking through the raw html I learned how the links were formatted and discovered that I only wanted links that started with /Course. I also figured out that the link objects had instructor and course information that could be parsed out of the OuterText property so I reformat the data into something more object-like.

I did this so that I could push the results to Out-Gridview displaying the courses.

get-trainsignalIn PowerShell 3, Out-Gridview can pass objects back to the pipeline, so I can select a few courses that look interesting, click OK, and the links will open up in my web browser.

I had a lot of fun creating this course and hope you find it worth your investment. Let me know what you think. And if there is a course you’d like to see me create, especially PowerShell related, let me know that too.

 

Friday Fun Get Beer List

beerWell, another Friday and what goes better with it than beer. Of course I should mix in a little PowerShell as well. I live in the Syracuse, NY area and we have a terrific local brewery, Middle Ages Brewing Company. Not only is there a tasting room, but I can also get growler refills. Middle Ages brews a number of beers throughout the year and beers available for growlers vary. Fortunately they post online what is currently available.

But I can’t be bothered to open a web browser and visit their site everytime I want to see what they have. That’s what PowerShell is for. Specifically PowerShell 3.0 and Invoke-Webrequest. With only a few lines of PowerShell, actually it could be done as a one-liner, I can get the list of beers available for growler refills. Let me break it down.

First, I’ll grab the web page with Invoke-WebRequest

Invoke-Webrequest “packages” it as a nice object.

invoke-webrequest-middleages

On the page, the growler refills have links pages for the respective beers. This is handy because I can get all the links from the object I just pulled down.

Naturally there are other links as well, but after looking through them I realized all the ones I was interested in had /beers/ in the href property. Knowing that, I can filter.

Excellent. All I need is to grab one of the text properties and I have my list.

Perfect (and I’m getting a little thirsty). My script, such as it is comes down to two lines.

This could even be consolidated down to a one-liner:

Although there’s no practical reason to do so. If you are new to PowerShell that is a little more difficult to understand. Normally I prefer sending objects to the pipeline but all I really need to see are beer names so this works just fine for me.

I see they are filling growlers with the Duke of Winship porter, one of my faves, so I’d better wrap this up. Cheers!

Query Local Administrators with CIM

Yesterday I posted an article on listing members of the local administrators group with PowerShell and Get-WmiObject. PowerShell 3.0 offers an additional way using the CIM cmdlets. The CIM cmdlets query the same WMI information, except instead of using the traditional RPC/DCOM connection, these cmdlets utilize PowerShell’s remoting endpoint so they are much more firewall friendly and a little faster in my testing. I’ll be covering these cmdlets over time as PowerShell v3 is released.

The code I wrote yesterday can be re-used with only a few modifications. We can still easily get the group object.


PS C:\> Get-CimInstance Win32_Group -filter "Name='Administrators'" -computername 'Quark'

SID Name Caption Domain
--- ---- ------- ------
S-1-5-32-544 Administrators QUARK\Administrators QUARK

We can connect either by a computername or a CIMSession. We still need to get associated objects. There is a cmdlet called Get-CIMAssociatedInstance which you could use like this:


Get-CimInstance Win32_Group -filter "Name='Administrators'" -computername 'Quark' | Get-CimAssociatedInstance -ComputerName 'Quark'

However, this will return all associations and I have not been able to find a way with this cmdlet to limit results to user and group objects as I did with WMI. However, we can still use the Associators Of query.


PS C:\> $computer='Quark'
PS C:\> $query="Associators of {Win32_Group.Domain='$computer',Name='Administrators'} where Role=GroupComponent"
PS C:\> Get-CimInstance -Query $query -ComputerName $computer

Name Caption AccountType SID Domain
---- ------- ----------- --- ------
Administrator QUARK\Admini... 512 S-1-5-21-139... QUARK
Jeff QUARK\Jeff 512 S-1-5-21-139... QUARK

These objects have some slightly different property names so to tweak the output I had to make a few changes. Here’s my complete demo script.


#requires -version 3.0

#use CIM to list members of the local admin group

[cmdletbinding()]
Param([string]$computer=$env:computername)

$query="Associators of {Win32_Group.Domain='$computer',Name='Administrators'} where Role=GroupComponent"

write-verbose "Querying $computer"
write-verbose $query

Get-CIMInstance -query $query -computer $computer |
Select @{Name="Member";Expression={$_.Caption}},Disabled,LocalAccount,
@{Name="Type";Expression={([regex]"User|Group").matches($_.Class)[0].Value}},
@{Name="Computername";Expression={$_.ComputerName.ToUpper()}}

Here’s the code in action.


PS C:\> C:\scripts\Get-CIMLocalAdmin.ps1 -Verbose
VERBOSE: Querying QUARK
VERBOSE: Associators of {Win32_Group.Domain='QUARK',Name='Administrators'}
where Role=GroupComponent
VERBOSE: Perform operation 'Query CimInstances' with following parameters,
''namespaceName' = root\cimv2,'queryExpression' = Associators of
{Win32_Group.Domain='QUARK',Name='Administrators'} where
Role=GroupComponent,'queryDialect' = WQL'.

Member : QUARK\Administrator
Disabled : False
LocalAccount : True
Type : User
Computername : QUARK

Member : QUARK\Jeff
Disabled : False
LocalAccount : True
Type : User
Computername : QUARK

It wouldn’t be much to turn this into a function, although all computers will need to be running Powershell 3.0. Download Get-CIMLocalAdmin and try it out for yourself.

Disclaimer: All of this is based on pre-released software. No guarantees that it will work when PowerShell 3.0 officially ships.