Archive for August, 2008

Select-Object vs Add-Member

Let me be clear on this one: Select is bad, Add-Member is good. 😉

OK, OK, this is not a one-size-fits-all answer, and there are plenty of scenarios in which Select-Object is useful, but in a lot of cases it is being misused. The most frequent one, when someone tries to use Select-Object or Format-Table to add a new attribute to the displayed objects. In that particular scenario these two cmdlets basically create one-time-use disposable objects for a quick output, while Add-Member adds an additional property and preserves the original objects for subsequent use.

A quick example, let’s say you want to see the Primary Group for your AD users. Primary group can be calculated by merging account domain SID and primary group ID so you probably end up with something like:

$PrimaryGroup = Get-QADGroup $($user.Sid.AccountDomainSid)-$($user.PrimaryGroupId)

Now, to add a column to the user output you could use Select-Object or Format-Table and supply the additional value using a hash-table parameter:

Get-QADUser | Select Name, @{Name=PrimaryGroup;Expression={(Get-QADGroup $($_.Sid.AccountDomainSid)-$($_.PrimaryGroupId)).Name}}

Get-QADUser | Format-Table Name, @{Label=PrimaryGroup;Expression={(Get-QADGroup $($_.Sid.AccountDomainSid)-$($_.PrimaryGroupId)).Name}}

(Note a small gotcha by the way: the column name is Name for Select-Object but Label for Format-Table ;))

However, both of these operations have a very important side effect – they kill the original objects and create new disposable ones instead. The collection you get is basically just a formatting thing which you can no longer use in other operations (e.g. pipe to yet another cmdlet).

Select-Object cmdlet changing the object type and losing all other properties

Select-Object cmdlet changing the object type and losing all other properties

For example if I use the Select code above in a PowerGUI script node I will indeed get the output in the grid, however, if you pay close attention (click the image to see the detail) you will see that I cannot add any other columns to the grid (and see for example users’ phone numbers), and the Actions pane to your right is empty – the usual actions for user objects are not there. 😦

Enter Add-Member. This cmdlet is a complete opposite to Select and Format-* – it adds a new member to the objects while preserving all other properties and the object type.

So if instead of the code above we use something like:

Get-QADUser | Add-Member -Name PrimaryGroup -Value {(Get-QADGroup $($this.Sid.AccountDomainSid)-$($this.PrimaryGroupId)).Name} -MemberType ScriptProperty -Force -PassThru

Add-Member adds a new property to PowerShell objects while preserving everything else

Add-Member adds a new property to PowerShell objects while preserving everything else

We get a nice set of objects with all user properties there and the new one added. As a result, inside PowerGUI admin console you see this new column, but can still add any other columns and perform any usual actions – check out the right-hand pane!

Tags: , , , , , , , ,

OCS blog by Curtis

Curtis Johnstone – the author of the best OCS Management Console (which coincidentally is a PowerPack ;)) has just started blogging!

His blog already has posts on the DNS records Office Communicator uses for automated sign-in, the ports OCS requires you to open on your firewalls, and on the latest updates to his OCS PowerPack.

If you are working with Office Communications Server, this is one of those blogs you need to watch! I obviously hope that it will also have as much of the related PowerShell content as Curtis can fit in. 🙂 You can find the blog here.

Tags: , , , ,

See you at VMworld

If you are attending VMworld this year make sure you attend the session I will be co-presenting with Carter Shanklin (Product Manager for VI Toolkit) and Andrey Anastasov who is VMware’s PowerShell Architect.

The session is:

Managing VMware With PowerShell
Tuesday September 16 from 1-2 p.m. MST

See you there!

Tags: , , ,

SQL Reporting Services PowerPack

In a pretty dramatic move Adam Murray submitted his SQL Reporting Services PowerPack just a few hours before the end of Sprint 1 of the PowerPack Challenge. And it was definitely worth the wait!

Adam's SQL 2005 Reporting Services PowerPack has 138 (!) powershell scripts

Adam's SQL 2005 Reporting Services PowerPack has 138 (!) powershell scripts

His PowerPack seems to have any SRS management feature you can think of. You can:

  • Work with Reports, Datasources and Folders
  • Create, move and delete items
  • List and modify security for items
  • List dependencies
  • Create linked reports
  • Delete history snapshots
  • Enable and disable datasources

All in all 138 nodes, links, and actions available with their PowerShell source code, and all of that working remotely via web services and compatible with SQL Server 2005. Go get it here!

Tags: , , ,

Out-Wiki

How do you publish your cmdlets help online without duplicating the effort? I created this relatively straight-forward Out-Wiki function which I used to generate all the pages in the QAD cmdlets online reference. Today I finally found time to upload the script to the PowerShell code repository so it is now available for anyone to use.

Just change the name of the PowerShell snapin in the invocation line:

Out-Wiki (Get-Command -PSSnapin Quest.ActiveRoles.ADManagement) c:\Temp\QADHelp

And in a minute you have all the wiki pages in MediaWiki (the wikipedia engine) wikitext format generated for you!

The script was inspired by Antonio’s New-HtmlHelp.

You can search for out-wiki in your PowerGUI Script Editor or grab it online here.

Tags: , , , ,

Summer PowerGUI Wallpaper

PowerGUI seasonal summer wallpaper for your desktop.

PowerGUI seasonal summer wallpaper for your desktop.

Continuing the good old tradition of seasonal wallpapers Andrey Tsarkov has just sent me this really nice one for the Summer 2008.

Enjoy your vacation if you still have one or bring a piece of it to your desktop!

The wallpaper is available in all the standard desktop sizes at the PowerGUI downloads page.

Tags: , ,

PowerShellizing Facebook

Have you ever thought of automating your social life? Mark Kilfoil has just posted a great tool to do that! Check out his Facebook Organizer PowerPack.

Manage your friends with Facebook Organizer PowerPack

Manage your friends with Facebook Organizer PowerPack

You can:

  • retrieve all information about your friends, albums, groups, events,
  • open profiles
  • send messages
  • etc., etc.

All this done by a nice collection of PowerShell scripts snapped into a cool PowerGUI pack.

We all have been using PowerShell to automate our jobs, now you can also create a bunch of PowerShell scripts to generate rich social life! And with both your boss and your friends/family thinking they are getting all your attention, you can actually start spending your time on something useful. Like, egh… Playing with v2 CTPs?.. Ideas are welcome!

Download Mark’s pack here.

Tags: , , ,

3 Days to Claim Sprint 1 Prizes

If you have a PowerPack you wanted to share with the community – it’s about time. Sprint 1 of the PowerPack Challenge 2008 will end this Friday and a bunch of $250/$500 Amazon certificates will be given away to the winners (here are the current scores).

Next week Sprint 2 will start – but because it goes well into September and that means that everyone will be back from vacation and competition will be way up! 😉

So spend a few hours tonight and get into the race! Rules & details can be found here.

Tags: , , ,

Citrix PowerPack

Quintus has just published a great PowerPack for managing Citrix XenApp (a.k.a. Presentation) Server.

It has 26 (!) different PowerShell functions to manage Citrix farms, users, applications, printers, and much more. He even has actions for application backup and restore!

PowerShell library with a couple of dozen Citrix XenApp management functions

PowerShell library with a couple of dozen Citrix XenApp management functions

Full list of functionality can be found at the powerpack page here.

The scripts use the MFCOM APIs which Christian described here a couple of weeks ago. It’s nice to see Quintus now taking the concept and running with it to create the whole Citrix management library!

(By the way, this is his second entry for the PowerPack challenge contest. You still have a few of days if you want to submit your pack for its first sprint. ;))

Tags: , , , ,

List all Constructed Attributes

Constructed (or computed) Attributes are an important part of the way Active Directory is functioning. Basically, these are not real attributes, in the sense that they do not really exist, but are calculated by AD when being queried. They contain very useful info (for example well known primaryGroupToken and modifyTimeStamp) but they obviously have a few limitations such as not being “settable” or not available for filtering, so knowing which are which is quite useful!

Here’s how you get a list of all computed attributes in your AD:

Get-QADObject -SearchRoot CN=Schema,CN=Configuration,dc=MyDomain,dc=COM -Type attributeSchema -IncludedProperties systemFlags -SizeLimit 0 | where {$_.SystemFlags -band 4}

Basically, this one-liner retrieves all (-SizeLimit 0) attributes (-type attributeSchema) from the Schema partition (-SearchRoot "CN=Schema,CN=Configuration,dc=MyDomain,dc=COM"), together with their system flags (-IncludedProperties systemFlags), and leaves just the ones with FLAG_ATTR_IS_CONSTRUCTED (where {$_.SystemFlags -band 4}).

Thanks to Andrey Moiseev who shared this with me recently!

[UPDATE] Check out Aleksandar’s post on making this oneliner run 40 times faster. 😉

Tags: , , , , , , ,


My Recent Tweets

Legal

The posts on this blog are provided “as is” with no warranties and confer no rights. The opinions expressed on this site are mine and mine alone, and do not necessarily represent those of my employer - WSO2 or anyone else for that matter. All trademarks acknowledged.

© 2007-2014 Dmitry Sotnikov

August 2008
M T W T F S S
 123
45678910
11121314151617
18192021222324
25262728293031

%d bloggers like this: