PowerShell v3 Simplified Where-Object Syntax

A great improvement to the new version 3 of PowerShell is the simplification of the Where-Object syntax. Here is an example of the way a Where-Object cmdlet works in v2.

    Set-Location "C:\Windows"  # Just to give us an interesting place
  
    # Old Syntax
    Get-ChildItem | Where-Object {$_.Length -ge 100kb}

Will produce this output:

Mode                LastWriteTime     Length Name                                                                                                          
----                -------------     ------ ----                                                                                                          
-a---         2/25/2011  12:19 AM    2871808 explorer.exe                                                                                                  
-a---         7/13/2009   8:39 PM     733696 HelpPane.exe                                                                                                  
-a---          6/3/2012   1:10 PM  649849097 MEMORY.DMP                                                                                                    
-a---         7/13/2009   8:39 PM     193536 notepad.exe                                                                                                   
-----         3/15/2012   6:07 AM    2693696 PWMBTHLV.EXE                                                                                                  
-a---         7/13/2009   8:39 PM     427008 regedit.exe                                                                                                   
-a---        12/20/2010   5:11 PM     307712 UltraMon.scr                                                                                                  
-a---         3/21/1996   2:00 PM     284160 uninst.exe                                                                                                    
-a---          6/4/2012   2:04 PM    1134024 WindowsUpdate.log                                                                                             
-a---          3/8/2012   5:37 PM     302448 WLXPGSS.SCR                                                                                                   
-a---         6/10/2009   3:52 PM     316640 WMSysPr9.prx    

Version 3 now allows a simplified version of the cmdlet:

    Get-ChildItem | Where-Object Length -ge 100kb

With v3 you can now eliminate the script block notation (the {} curly braces), the $_ current object placeholder, as well as the . property notation. Now you can simply enter the name of the property and the condition. It produces the same output as what you see above. You can also use it with variables, or with the question mark as an alias for Where-Object.

    $size = 100kb
    Get-ChildItem | Where-Object Length -ge $size
    Get-ChildItem | ? Length -ge 100kb

Please note that this new syntax is only valid for simple where clauses. Anytime you have something more complex, such as multiple conditions, you will be required to revert to the v2 $_ syntax.

Warning: This post was created using the PowerShell v3 BETA. Changes between now and the final release may affect the validity of this post.

PowerShell v3 Ordered HashTables

In version 2 of PowerShell, you could create hashtables, but the order in which the data is stored in the hashtable is not necessarily the same order you input it in. Take this example:

    $hashTableV2 = @{a=1;b=2;c=3;d=4}  
    $hashTableV2 

Produces this output:

Name Value

---- -----

  c     3

  d     4

  a     1

  b     2

Version 3 of PowerShell now offers the ability to force the hashtable to hold the data in the same order it was created, using the new [ordered] tag.

    $hashTableV3 = [ordered]@{a=1;b=2;c=3;d=4}  
    $hashTableV3 

The new syntax produces this output:

Name Value

---- -----

  a     1

  b     2

  c     3

  d     4

Now if it’s important that your hashtable is in a specific order, you have the [ordered] to make that happen.

Warning: This post was created using PowerShell v3 BETA. Changes to the final version may alter the validity of this post.

PowerShell, Atlanta Techstravaganza, and reading the PowerScripting.net RSS Feed with PowerShell

Today I had the pleasure of co-presenting “What’s new in Windows 8 PowerShell” (aka PowerShell v3) with my friend, Microsoft Regional Developer Evangelist Glen Gordon (blog | twitter).

You can find the slide deck for our presentation here.

We had a lot of code demos in the presentation, and some due to the short time period we didn’t get to. I’ll be posting the demos over the next few weeks, doing it slowly so as not to overwhelm anyone.

Below is the code to pull the RSS feed for the PowerScripting podcast. The code is pretty self explanatory, see the comments for more info.

#region Reading RSS Feeds

  #----------------------------------------------------------------------------
  # Reading RSS Feeds
  #----------------------------------------------------------------------------
  
    # Reading RSS Feeds has become even easier in v3
    $url = "http://feeds.feedburner.com/Powerscripting?format=xml"
  
    # v2 Syntax
    $webClient = New-Object net.webclient
    [xml]$rss = $webClient.DownloadString($url)
    $rss.rss.channel.item |
       Select-Object Title, pubDate, duration  |
       Format-List 
  
    # v3 Syntax
    Invoke-RestMethod $url |
       Select-Object Title, pubDate, duration  |
       Format-List 

    # To know the available object names, open the RSS
    # feed and look at the XML tags inside. The ones
    # shown in this example are pretty commong to most feeds.
  
#endregion Reading RSS Feeds

SQL Server Learning Resources for Beginners

Over the last couple of months I’ve been putting up some of my favorite learning resources. Recently my friend Robin Hunt (twitter | linkedin) at ThinkData Solutions asked me for some recommendations for some SQL Server learning resources for beginners. I thought I’d share that list here as well.

Books

Below are some of my favorite books on the subject. The links are to Kindle format, mostly because I’m a Kindle junkie, but you can also get the paperback version from a link on each page.

Microsoft SQL Server 2008 Step by Step – Any of the step by step books are good quality. There’s no 2012 version of this that I know of, but I’m sure one will be coming.

Microsoft SQL Server 2012 A Beginners Guide – This is a good resource if you are totally new to SQL Server. It’s very complete, covering all aspects of SQL Server Administration.

Introducing Microsoft SQL Server 2012 – This book is an easy read, so even beginners shouldn’t have any issues understanding it. It focuses mostly on new features in 2012, so it’s not quite a comprehensive book. But the Kindle version is FREE, so it makes a good resource for this list.

Professional Microsoft SQL Server 2012 Administration – If you already have some good technical background you might want something one step above a beginner book, but is still easy for people new to SQL Server to understand, then this is a really good book to get. (Disclaimer, I’m a co-author of this book.)

Videos

SQL Share – This is a really unique site. It’s designed for the busy professional. All the videos here are very short, generally in the 10 minute range, and focus on one very specific topic. As I write this, the featured video is on working with foreign keys. It doesn’t have anything in the way of an overall course though, so I’d suggest it as a great resource to learn more about a topic you are interested in, or perhaps read about but don’t quite understand. (Disclaimer,  I have some videos on this site.)

 

#sqlhelp

Another good resource is Twitter. Yes, Twitter. Do a search on the #sqlhelp hash tag and you’ll see all sorts of great questions being asked and answered. The SQL community does a great job of monitoring this hash tag and providing assistance.

Using PowerShell To Find A Value In A Set Of Files

I often present PowerShell at SQL Saturdays. One question I’m often asked is “Why learn PowerShell? I can do everything in T-SQL.” Well here’s a great example.

I was developing some SSIS packages that took a set of text files and loaded them into multiple dimension and fact tables. In the process I had one particular value that was creating duplicate values in one of the target tables. Obviously, I needed to find that value in the various files and see what’s different about it versus the other data.

The problem was there are close to 200 files I’m loading. Each file has hundreds of lines, and each line is over 200 characters long. Yikes! That’s a lot of data to try to look through. It would have been very time consuming to look in all of those files manually.

PowerShell to the rescue! In just a few minutes I created a PowerShell script to loop over the files, find the ones that had the value I was looking for, and list those file names. Made it easy then to open just the files I needed and look at the rows. I could have gone further, actually printed out the data, or perhaps load it into some array where it would count it, or more.

I decided to share the script, which you will see below. It actually took me longer to write up all the comments than it did to write the initial code. With the comments removed there’s only 9 lines of real code in the script.

Using PowerShell I was able to quickly find the rows I needed and find the issue. This is just one simple example of how PowerShell can be used to quickly solve a problem that might take a long time to accomplish in other languages.

 

#-------------------------------------------------------------------------
# Script: Search for a string in a group of files
# Author: Robert C. Cain
#
# Revision History
#   V01 - 05/28/2012 - Initial version
#-------------------------------------------------------------------------
# This PowerShell script will loop over a collection of text files 
# which are delimited somehow (comma, tab, etc) and look for a 
# specific value. It will then print out the name of the file. 
#-------------------------------------------------------------------------

# Here I've hardcoded a value to look for, you could also
# pass this in as a paramter if you convert this to a function
$ValueToLookFor = '123456789'

# Our data files have a header row, this should match the 
# names of those columns. This will allow us to address 
# the columns by name later in the routine
$header = "Column1", "Column2", "ColumnToSearch", "Column3", "Column4"

# Move to the location with all the files
Set-Location 'C:\MyData'

# Retrive a list of files that we need to look thru
# Note the use of the filter to only select some files
# If you are doing all files you can omit the filter
$files = Get-ChildItem -Filter "Data*.txt" | Select-Object Name

foreach($file in $files)
{
  # Load the data in the file into $data
  #
  # In this example the files are tabbed delimted, hence the need
  # to specify the tab character as a delimter. If your file is
  # comma delimted you can omit the -Delimter, otherwise specify
  # whatever yours is. 
  #
  # We also need to pass in the $header variable so PowerShell 
  # will be able to associate each column of data with a name
  $data = Import-Csv $file.Name -Delimiter "`t" -Header $header

    foreach($line in $data)
    {
      # Here's where the header comes in handy, I can now use the 
      # column name as the .Property to look in
      if ($line.ColumnToSearch -eq $ValueToLookFor)
      { 
        # Print out the name of the file. You could do other things
        # like print out the actual line, or load it into a variable,
        # just to name a few. 
        $file.Name
        
        # Use a break if you only want the file name listed once
        # I actually let it print out multiple times to indicate how
        # many times it's in the file

        # break  
      }
    } # foreach($line in $data)
} # foreach($file in $files)

Column Cut Copy Paste in VS SSMS and PowerShell

Did you know it’s possible to do Column based cut, copy and paste in Visual Studio, SQL Server Management Studio, and PowerShell v3? Not many people do. Even less people know that with VS 2010 and SSMS 2012 you get a little “extra” functionality. Watch the video to find out all the juicy details.

 

Atlanta Code Camp 2012

It’s time again for the Atlanta Code Camp! Still a few hours left to register, you can do so at http://www.atlantacodecamp.com/. Why would you want to come? Well for one to see me, I’ll be giving two presentations.

The first is right after lunch, The Decoder Ring to DW/BI (Data Warehousing / Business Intelligence). In this talk I’ll walk through all the concepts behind designing a data warehouse, including some real world examples to help you understand the differences between Facts, Dimensions, Surrogate Keys, and more.

You may think a BI talk is an odd one for a developer oriented day. More and more though developers are being directed toward the data warehouse to get information for their applications, to combine with the system they are designing. Understanding how data warehouses work will give you a leg up when your company establishes it’s own data warehouse.

The slides for my presentations can be downloaded HERE.

My second session is “Become a PowerShell Pop Star”. In this very fast session we’ll start at ground zero with PowerShell, Microsoft’s scripting language. You’ll see all about cmdlets, variables, functions, programming and more.

SQL Saturday #111 Atlanta

The excitement is brewing! This Saturday is going to be huge if you are a SQL person living in the southeast US. SQL Saturday #111 is this Saturday, April 14 with a whopping TEN tracks! With five sessions per track that’s over FIFTY sessions of SQLY goodness for you to pick from. There’s even some PowerShell thrown in there for us PowerShell fans.

I was lucky enough to be asked to present two sessions this time. The first is at the 10:15 am time slot.

Configuring SQL Server 2012 Reporting Services

Reporting Services can be one of the easier tools to configure, once you understand how to use the configuration manager. In this session you’ll explore the configuration settings available through the exploration of the Reporting Services Configuration Manager. You’ll learn the different accounts Reporting Services will need in order to run, how to access Reporting Services via URLs, and the importance of encryption keys.

This year they are doing something new, and having a beginner track. For anyone brand new to SQL Server this is the place for you to be. I was asked to do my second session in the beginner track in the 1 PM time slot.

The Decoder Ring for Data Warehousing / BI

Business Intelligence is one of the hottest job skills in the IT field. DBAs and Database Developers are being asked to implement databases with non-traditional design, and are having a lot of new vocabulary thrown their way. In this talk you will learn the concepts behind building and designing data warehouses, and cut through the buzz words so you will walk away with a clear understanding of what words like dimensions, facts, and measures mean.

Last I checked there were still some seats left for the event, but hurry as they are running out quick. I also have a little extra swag to give out during my sessions, courtesy of Pragmatic Works and Pluralsight.

PowerShell Help Community Edition

Last week when I blogged about iPowerShell from Sapien, I mentioned they had a few free community tools. One of those is PowerShell Help Community Edition.

As you can see, it provides a simple, easy to you way to navigate help in PowerShell. I find this especially nice as I often have my editor open in one window and want help files or other reference materials open in a second. Down the left is a tree which allows you to navigate to the type of object you are seeking help for. It also supports search, you can see the search results pane in the lower right.

SNAGHTML18a594e

 

Like iPowerShell, it uses the standard PowerShell help format, so you can add in any PowerShell help file, just as you can with iPowerShell. Best part is the price, it’s free! Just go to Sapien’s website, to the download center, and navigate to the Community Tools section. Select your flavor (32 or 64 bit).

One little quirk I was having that someone in the support forums helped me with, and this was a Windows issue not a PowerShell Help issue. I wanted to create a docked shortcut for this right in the Windows 7 task bar, but it wasn’t letting me. Turns out if you have the word Help in your file name Windows “protects” you by not letting you add it to the task bar. I created a shortcut, renamed the shortcut to remove the e in Help from the file name, and all was well with the universe.

iPowerShell

As a consultant I travel a lot, and am usually on my small laptop with it’s 13 inch screen. A great size for travel, but a bit small on occasion. Doing PowerShell I often want to look things up, but it’s a pain having to jump back and forth between my editor and the help.

I have e-books on PowerShell which I can read on my various e-book devices, but as you might expect with a book they are not highly interactive. So the other day I ran across a solution for, of all things, my iPad.

It’s called iPowerShell, from the folks at Sapien. It was less than five dollars (US), and they have both iPad and iPhone / iPod Touch versions. It provides interactive access to the PowerShell help system, allowing you to quickly drill down into Cmdlets, Aliases, Providers, and more.

image

 

What I love most about iPowerShell is it’s extensibility. It uses the standard PowerShell help file format. That means you can write your own custom help files and use them not just in regular PowerShell but iPowerShell and other products that incorporate standard help.

This brings up some great possibilities. Many companies have generated their own set of custom modules and cmdlets. Often they have already created help files to go with them. Now those same help files can be used by simply importing them into iPowerShell.

If I had any wishes for iPowerShell, there’s be two. First, it’d be nice to have the basic syntax included. For example, there’s two ways to do the foreach command, it’d be nice to have those documented for easy lookup in iPowerShell. Second, right now the cmdlets are sorted alphabetically by the verbs. It’d be nice if there was a way to have them sorted by the nouns. There is a search feature though, which greatly alleviates that need.

Incidentally I found out about the tool because of my post on PowerShell Training. Over on the BIDN version of the post, a reader had posted a link to Windows PowerShell 2.0 TFM by Don Jones and Jeffery Hicks. This is an extremely comprehensive book, one that I immediately bought and have been reading ever since (hence explaining why I was up until 2 am this morning). You can get it in PDF format directly from Sapien at the link above, or in print format from Amazon. Highly recommended.

Sapien has some other cool, free community tools I plan to blog about in the near future, so stay tuned. In the meantime if you are both a PowerShell coder and iPod / iPhone / iPod Touch owner I think you’ll enjoy this inexpensive app.

PowerShell Training Resources

OK, I admit it. I love PowerShell! It’s the way to go when you want to do automation. Not only that, it has the ability to hook into all aspects of the Microsoft tool stack. Not just SQL Server, but SharePoint, Exchange, Windows Server, and more. So here are some of my favorite resources for learning PowerShell.

A quick disclaimer, some of the links below are by co-workers or other people I have an affiliation with, financial or otherwise. That’s because I’m lucky enough to work with some of the best people in the field. Also, in the case of the books I’ve linked to the Kindle version where possible, mostly because I’m a Kindle junkie. There are paper versions of the books, and you are free to buy from your favorite retailer.

Books

Windows PowerShell in Action, Second Edition – If you are only going to buy one PowerShell book, this is “The” book as folks say. It’s one of the two that gets referenced quite often. Note the prior link is to Amazon, where you can only get the paper (aka “dead tree”) version. You can get electronic versions (Kindle, PDF, and ePub) directly from the publishers website.

PowerShell In Practice -  This is the other book in my collection that gets a good workout. Lots of great examples and easy to understand. Like the previous book, the link is to the Amazon dead tree version, you can also get the electronic version from the publishers website.

Windows PowerShell 2.0 Best Practices – So you want to know if you are doing PowerShell right? As it’s title implies, this book lays out best practices to help your PowerShell solutions succeed.

Note there are a lot of other really good PowerShell books on the market that focus on using PowerShell with specific technologies such as SharePoint, SQL Server, Exchange, and Windows Server. The books I’ve listed above cover PowerShell in general.

Blogs

Richard Siddaway – This guy blogs more about PowerShell than anyone I know. It’s an invaluable resource for PowerShell. In addition he is also the author of PowerShell in Practice, listed above.

PowerShell.com – This website is a treasure trove of resources. Indeed, it could have been put into all of the sections in this post as it holds not just blogs but free e-books, forums, and webinars.

Hey Scripting Guy – Ed Wilson, author of the Best Practices book above, is “The Scripting Guy”. Ed works on the PowerShell team at Microsoft and is very active in the PowerShell community.

Podcasts

PowerScripting Podcast – A great show out of Atlanta, has a lot of information about PowerShell especially for beginners. The website also has a lot of useful links. I always learn something new listening to their show.

Get-Scripting – If you are out of the UK you’ll appreciate this PowerShell Podcast. Has a focus on PowerShell with VMWare’s PowerCLI.

Videos

Pluralsight – I know Pluralsight has a great series on PowerShell, as I’m the one who authored them! In addition Pluralsight has an extensive catalog of other courses you can pick from. It’s subscription bases so there is a modest fee (starts at $29 US per month last I checked) but well worth it for the training you can get. There’s also a free trial.

For a quick link direct to this post, you can use http://bit.ly/arcaneps

SharePoint BI (Business Intelligence) Training Resources

SharePoint is a huge topic unto itself, so I wanted to provide some links that focus on learning how to use SharePoint in the context of Business Intelligence. 

A quick disclaimer, some of the links below are by co-workers or other people I have an affiliation with, financial or otherwise. That’s because I’m lucky enough to work with some of the best people in the field. Also, in the case of the books I’ve linked to the Kindle version where possible, mostly because I’m a Kindle junkie. There are paper versions of the books, and you are free to buy from your favorite retailer.

Books

SharePoint 2010 Business Intelligence 24 hour Trainer – This is a really cool book, in that it’s not just a book. It comes with a DVD loaded with video lessons on how to use the various tools in SharePoint for doing BI. This is the first book to buy if you are new to doing BI in SharePoint.

Business Intelligence in Microsoft SharePoint 2010 – This is a great book which provides an introduction to all of the BI Services available within SharePoint 2010.

Microsoft SharePoint 2010 Business Intelligence Unleashed – Like most of the books in the “Unleashed” series, this takes a much deeper dive into the tools than the previous two books. A good choice once you are ready to move beyond the beginner stage.

Blogs

There aren’t many blogs that focus solely on SharePoint for BI, so I’ve picked out two sites that have a lot of SharePoint for BI content on them.

Data Inspirations -  This blog focuses on all aspects of BI, one of the lead bloggers is Stacia Misner, co-author of the BI in Microsoft SharePoint 2010 book listed above.

BIDN – Many experts in the BI field, myself included, contribute to the Business Intelligence Developer Network. Here you’ll find a wide variety of topics in the BI field, including many on SharePoint BI.

Videos

Pragmatic Works Webinars – On our website we have a big catalog of past webinars (all of which are free to watch), many of which focus on PowerPivot.

Pluralsight – Pluralsight has an extensive catalog of other courses you can pick from. It’s subscription bases so there is a modest fee (starts at $29 US per month last I checked) but well worth it for the training you can get. There’s also a free trial.

For a quick link direct to this post, you can use http://bit.ly/arcanespbi

PowerPivot Training Resources

I’ve been asked to provide links to some useful resources for learning about PowerPivot. Below are a list of my favorite blogs, books, and other sites to learn from.

A quick disclaimer, some of the links below are by co-workers or other people I have an affiliation with, financial or otherwise. That’s because I’m lucky enough to work with some of the best people in the field. Also, in the case of the books I’ve linked to the Kindle version where possible, mostly because I’m a Kindle junkie. There are paper versions of the books, and you are free to buy from your favorite retailer.

Books

Professional Microsoft PowerPivot for Excel and SharePoint – This book covers all aspects of PowerPivot, from using it to installing it to configuration. Everything you want to know in one volume. I wouldn’t particularly call this a beginners book however, it assumes you are competent in BI, Excel, and SharePoint.

Microsoft PowerPivot for Excel 2010: Give Your Data Meaning – This is a good first book to get, it goes deeply into the use of PowerPivot within Excel.

Practical PowerPivot and DAX Formulas for Excel 2010 – Once you are comfortable with PowerPivot, you’ll want to learn more about DAX, Data Analysis eXpressions, the set of functions used to do advanced calculations and aggregations in PowerPivot. I’m a big fan of the way the author, Art Tennick does his books. It’s the Problem – Solution approach, where he demonstrates a common problem then shows one or more ways to solve it. Art also has books on MDX and DMX you should check out.

Blogs

PowerPivot Pro – Probably one of the best blogs around, Rob and Kasper provide excellent content.

Denny Lee’s Blog – Denny works for Microsoft and is part of the PowerPivot team. He provides some really great insights, and is co-author of the Professional Microsoft PowerPivot for Excel and SharePoint book above.

PowerPivot Info – Not so much a blog but a blog aggregator, this site brings the best PowerPivot content on the web to the forefront.

Videos

Pragmatic Works Webinars – On our website we have a big catalog of past webinars (all of which are free to watch), many of which focus on PowerPivot.

Pluralsight – A little shameless self promotion here. I did a complete course on PowerPivot for Pluralsight. This includes both using PowerPivot from Excel and managing PowerPivot within SharePoint. In addition, Pluralsight has an extensive catalog of other courses you can pick from. It’s subscription bases so there is a modest fee (starts at $29 US per month last I checked) but well worth it for the training you can get. There’s also a free trial.

For a quick link direct to this post, you can use http://bit.ly/arcanepivot

SSRS Training Resources

I’ve been asked to provide links to some useful resources for learning about SQL Server Reporting Services. Below are a list of my favorite blogs, books, and other sites to learn from.

A quick disclaimer, some of the links below are by co-workers or other people I have an affiliation with, financial or otherwise. That’s because I’m lucky enough to work with some of the best people in the field. Also, in the case of the books I’ve linked to the Kindle version where possible, mostly because I’m a Kindle junkie. There are paper versions of the books, and you are free to buy from your favorite retailer.

Books

Microsoft SQL Server 2008 Reporting Services Step by Step – A great beginner book, loaded with good examples.

Pro SQL Server 2008 Reporting Services – This book goes much more in-depth with SSRS, delves into many advanced topics.

Microsoft SQL Server Reporting Services Recipes – 2008 or 2012 version of book. This is a great book, especially if you are doing Business Intelligence reporting. Note Amazon says the 2008 version is no longer available in the US, but I’m betting you can find it in your local bookstore or from other retailers. The 2012 version is available for pre-order.

Applied Microsoft SQL Server 2008 Reporting Services – Great book, like the book above covers many aspects of SSRS including BI reporting. Note Amazon only sells the paper version, you can also get it in PDF format direct from the publishers website.

Professional SQL Server 2012 Administration – I mention this book because I wrote the chapter on SQL Server Reporting Services. I don’t go deep into creating reports, although I briefly cover Report Builder. I do go into configuring SSRS and how to do scale out deployments, the total chapter is around 50 pages.

Blogs

Paul Turley – Paul is an active blogger and fellow Microsoft MVP. He is also co-author of the Reporting Services Recipes book I listed above.

Tep Lachev – An active blogger, Teo is not only a good resource for SSRS but for other BI tools such as PowerPivot. He is also author of the Applied Microsoft SQL Server 2008 Reporting Services book, listed above.

Videos

Pragmatic Works Webinars – On our website we have a big catalog of past webinars (all of which are free to watch), many of which focus on SSRS.

Pluralsight – Pluralsight has an extensive catalog of courses, including some great SSRS content. It’s subscription bases so there is a modest fee (starts at $29 US per month last I checked) but well worth it for the training you can get. There’s also a free trial.

For a quick link direct to this post, you can use http://bit.ly/arcanessrs