Tools

I’m being interviewed about PowerShell

December 3rd, 2008 / Development in a Blink

Hal Rottenberg invited me to chat on his PowerScripting Podcast tomorrow night. Data visualization with PowerShell is on the list. And the rest. We’ll have to wait and see.

Live Stream Here.

PowerShell Provider for Coral8

December 3rd, 2008 / Development in a Blink

Here on Google Code. Scott Weinstein made is navigation provider available here’s his post.

Using PowerShell’s PsParser::Tokenize, Discover Functions in PowerShell Scripts

November 14th, 2008 / Development in a Blink

Another CTP drop for PowerShell is expected in December. One of the new features, Modules, allows PowerShell code to be organized in self-contained, reusable units.

In anticipation of this feature, I am experimenting with building PowerShell solutions by organizing several related functions into a single script that is sourced. Think; Clear-Item, Copy-Item, Get-Item, Invoke-Item,  but tailored to a custom system.

The Challenge

As the PowerShell code grows, remembering the functions is harder. Here is script that discovers them.

Get-Functions

Get-Functions takes a single file, a list of files or the –Recurse switch. The recurse switch finds all the functions in all the files in the subdirectories.

param(
 $files,
 [switch] $recurse
)
 
$parser = [System.Management.Automation.PsParser]
 
if($recurse) {
 $files=(dir . -Recurse *.ps1)
} else {
 if(!$files) {throw "Please enter a file for scanning."}
 if($files -is [String]) {$files=(dir $files)}
}
 
ForEach($file in $files) {
 $parser::Tokenize((Get-Content $file.FullName), [ref] $null) |
 ForEach {
  $PSToken = $_
  if($PSToken.Type -eq  ‘Keyword’ -and
     $PSToken.Content -eq ‘Function’ ) {     
     $functionKeyWordFound = $true
  }
  
  if($functionKeyWordFound -and
     $PSToken.Type -eq  ‘CommandArgument’) {
     
     ‘’ | Select `
      @{
       Name="FunctionName"
       Expression={$PSToken.Content}
      },
      @{
       Name="Line"
       Expression={$PSToken.StartLine}
      },
      @{
       Name="File"
       Expression={$file.FullName}
      }
      
      $functionKeyWordFound = $false
  }
 }
}

Example

I downloaded PowerShell code for two books, Windows PowerShell in Action and Windows PowerShell Cookbook. Then ran the command:

Get-Functions -r | Out-GridView

image

The Out-GridView lets you sort columns and filter. Typing in the search box scans each column, showing matches.

Get-Functions provides the file name and line number where to find function.

Thanks to James Brundage. He blogged the approach in WPF & PowerShell – Part 2 (Exploring WPF (and the rest of .NET) with Scripts). Also, he pointed out timings comparing PSObject creation using Add-Member v. the above Select @{name;expression} approach. Super helpful.

Download Get-Functions

Discover Intellipad commands using PowerShell

November 4th, 2008 / Development in a Blink

Get-IntellipadCommands | Out-GridView

This PowerShell snippet searches all the IronPython files deliver with Intellipad, looks for the key word MetaData.CommandExecuted, then extracts the Command Name, Short Cut Key, File Name and  the Line Number the command was found on.

Out-GridView, delivered with PowerShell V2, displays it.

Now you can sort and filter commands and find their short cut key. Then, when you want to extend Intellipad and need an IronPython snippet to follow, you have the File Name and Line Number the command is on.

image

Filtering

image

PowerShell Code

dir ‘C:Program FilesMicrosoft Oslo SDK 1.0′ -r *py |
 Select-String Metadata.CommandExecuted |
 ForEach {
  $line = $_.Line
  $lparen = $line.IndexOf(‘(’)
  $line=($line.Substring($lparen+1,$line.Length-$lparen-2) -Replace "’","").Trim()
  $null, $cmd, $key = $line.Split(‘,’)
  
  $pos = $cmd.IndexOf(‘}’)
  $cmd = $cmd.Substring($pos+1)
  New-Object PSObject |
   Add-Member -Passthru NoteProperty Command $cmd |
   Add-Member -Passthru NoteProperty ShortCutKey $key |
   Add-Member -Passthru NoteProperty LineNumber $_.LineNumber |
   Add-Member -Passthru NoteProperty FileName $_.Path |
 }

Download Get-IntellipadCommands Code

Social Networks and Pork Barrel Spending?

October 27th, 2008 / Development in a Blink

I blogged about data mining US Earmarks here, here, here and here. I started wondering, is there a relationship between Senators and and the sections of the bills they voted for.

Building on the initial PowerShell code, you can download it at the bottom, I used a simple similarity scoring and produced the graph below. This is a circular layout of the data, the placement of the target nodes are in the order they are passed to the AddEdge method, no weighting is done based on the calculated similarity.

I compared the votes of each the following Senators to the rest of the Senators looking for similarities. Clinton, Stevens, Kyl, Obama and Schumer. Choosing the Top 10 most similar and graphed the connections.

PowerShell-Earmarks

Raw Data

Source  Target     Tanimoto
Clinton Levin           0.7
Clinton Isakson         0.7
Clinton Pryor           0.7
Clinton Leahy          0.71
Clinton Chambliss      0.71
Clinton Nelson         0.72
Clinton Reid           0.73
Clinton Vitter         0.75
Clinton Stabenow       0.81
Clinton Schumer        0.96
Stevens Boxer          0.67
Stevens Akaka          0.67
Stevens Menendez       0.67
Stevens Coleman        0.67
Stevens Klobuchar      0.68
Stevens Leahy          0.68
Stevens Chambliss      0.68
Stevens Schumer        0.69
Stevens Inouye         0.76
Stevens Reid           0.81
Kyl     Dorgan         0.41
Kyl     Klobuchar      0.41
Kyl     Bingaman       0.42
Kyl     Wyden          0.43
Kyl     Smith          0.45
Kyl     Coleman        0.45
Kyl     Murkowski      0.47
Kyl     Ensign         0.47
Kyl     Roberts         0.5
Kyl     Thune          0.53
Obama   Wyden          0.68
Obama   Reed            0.7
Obama   Brown           0.7
Obama   Lugar          0.71
Obama   Snowe          0.71
Obama   Collins        0.71
Obama   Roberts        0.72
Obama   Bayh           0.75
Obama   Martinez       0.75
Obama   Whitehouse     0.75
Schumer Shelby         0.71
Schumer Vitter         0.72
Schumer Durbin         0.73
Schumer Levin          0.73
Schumer Leahy          0.74
Schumer Chambliss      0.75
Schumer Reid           0.76
Schumer Nelson         0.76
Schumer Stabenow       0.79
Schumer Clinton        0.96

Six Degrees of Separation

The network graph is based on the Tanimoto Coefficient.

Cosine similarity is a measure of similarity between two vectors of n dimensions by finding the angle between them, often used to compare documents in text mining.

My interpretation, take two lists, find the intersection. Add the count of the first list to the second, subtract the count of the intersection. Take this number and divide it into the intersection count.

The earmark data lists the bill section each Senator voted for, therefore, a Tanimoto coefficient can be calculated for say, what Clinton and Schumer voted on.

PowerShell Code

Lines 1 sources/loads the code containing several functions. Line 2 transforms the data from nested hash tables to a hash and array of strings the key being the Senators last name, this is used to calculate the coefficient.

   1: . .Do-Analysis.ps1
   2: $set = Do-Transform
   3: list Clinton Stevens Kyl Obama Schumer | 
   4:  % { Do-Compare $set $_ | select -last 10 } | 
   5:   Show-NetMap C

Next Steps

This is a spike test to see if it makes sense to continue. Using the PowerShell command line enables quick data analysis. Running the above code without the Show-Map displays the dataset including the similarity rating.

Drill down from the graph into the actual data is next. This should be straight forward hooking up the double click events of the NetMap control to PowerShell code.

Also of interest is the Tanimoto Coefficient, included in the Do-Analysis.ps1, it can be used on any list of strings in any application. Here is a version I posted using C#.

Downloads

SAT words and PowerShell Productivity

October 27th, 2008 / Development in a Blink

My daughter is getting ready for her SAT tests. She handed me a stack of 3×5 index cards. On one side are the synonyms and antonyms, on the other is the SAT word. Each word has an indication if it is a noun, verb, adjective etc.

She asked me to check each word on each card to see if the were correctly identified. So I created a PowerShell file which reads a file of words, queries The Free Dictionary for that word, pulls down the html, parses it for the word type and displays the results.

PowerShell

param($fileName=".words.txt")

$wc = New-Object Net.Webclient

gc $fileName |
 ForEach {
  $s = $wc.DownloadString("http://www.thefreedictionary.com/$_")
  $target=‘<div class="runseg"><i>’
  $m=[regex]::matches($s, $target)
  $result=$s.SubString($m[0].Index, $target.length+9) `
  -replace $target, "" `
  -replace "[<>/i]", ""

  New-Object PSObject |
   Add-Member -Pass Noteproperty Word $_ |
   Add-Member -Pass Noteproperty WordType $result
 } | Format-Table -AutoSize

Result

Word          WordType
----          --------
shameful      Adjective
despicable    Adjective
respectable   Adjective
flattering    adjective
recapitulate  Verb
vernacular    Noun
palatial      Adjective
sanctimonious Adjective
yearn         Verb
paraphrase    Noun

New-RandomWord from Ruby to PowerShell

October 17th, 2008 / Development in a Blink

Ruby Cookbook has interesting scripts. Here is one ported to PowerShell. Useful for generating test data.

You can add | Set-Content test.txt after New-RandomWord to save it to a file. Or create a script like Export-ToSql.

image

Begin {
 $letters = @{
  v=‘aeiou’
  c=‘bcdfghjklmnprstvwyz’
  ‘ ‘=‘ ‘
 }

 Function New-RandomWord ($s) {
  [char[]]$s |
   ForEach {
    $source = $letters.([string]$_)
    $word += $source[(Get-Random $source.Length)]
   }
   $word
  }
}

Process { if($_) {New-RandomWord $_} }

PowerShell and the Cloud

October 15th, 2008 / Development in a Blink

Jeffery Snover drops a secret on PowerShell. BITS, Background Intelligent Transfer Service. Cmdlets are coming in Version 2.

He blends this with Mitch Denny’s announcement of a PowerShell GoGrid Snap-in and offers up how to work with cmdlets requiring credentials.

Thanks Mr. Snover, super helpful.

Today the NY Times announced a RESTful API to Campaign Finance data(others to follow). In a few lines of PowerShell I am gliding through presidential contribution data by zip code. There are several ways to get at the data, each requiring credentials.

I’ll be employing Mr. Sonver’s suggestion.

PowerShell SaaS

October 13th, 2008 / Development in a Blink

Mitch Denny has released his PowerShell Snap-in for GoGrid up on CodePlex.

GoGrid is very similar to the Amazon EC2 offering except that right now it is the only one to offer Windows 2003/2008 hosting using this on-demand model.

Create a GoGrid Windows 2008 Server

Mitch shows this example for creating a basic GoGrid Windows 2008 server from PowerShell

Add-GoGridServer -Credential $credential `
 -Name DemoServer `
 -Image w2k8_32_base `
 -Memory 1GB `
 -IPAddress 173.1.23.194

Out of the box, the Snap-in supports 13 Cmdlets.

  • Add-GoGridLoadBalancer
  • Add-GoGridServer
  • Delete-GoGridLoadBalancer
  • Delete-GoGridServer
  • Get-GoGridImage
  • Get-GoGridIPAddress
  • Get-GoGridLoadBalancer
  • Get-GoGridOption
  • Get-GoGridServer
  • Get-GoGridServerPassword
  • Restart-GoGridServer
  • Start-GoGridServer
  • Stop-GoGridServer

Interestingly GigaSpaces teamed up with GoGrid in a

joint offering that enables enterprises to migrate existing and new Java, J2EE, .Net and C++ applications to a cloud computing infrastructure with an hourly pay-per-use pricing model.

Mitch mentions Amazon’s upcoming Windows offering. He is planning a snap-in when it is announced. I looked at integrating PowerShell and Amazon SimpleDB. I will kick the tires on Amazon’s EC2 Windows in the Cloud API once it is ready.

Displaying SVN Info On Your PowerShell Prompt

October 12th, 2008 / Development in a Blink

Came across this post Displaying GIT Branch on your PowerShell prompt and built it for subversion. Add this to your $PROFILE.

function prompt {
 $host.ui.rawui.WindowTitle = $(get-location)
 if(Test-Path .svn) {
  switch -regex (svn st) {
   "^?" {$other+=1}
   "^A" {$added+=1}
   "^M" {$modified+=1}
   default {}
  }
   $prompt_string = "SVN o:$other a:$added m:$modified >"
  } else {
   $prompt_string = "PS >"
  }

  Write-Host ($prompt_string) -nonewline -foregroundcolor yellow

  return " "
}

For a directory that is an svn working copy.

  • o:4 – 4 files not under version control
  • a:1 – 1 item scheduled for addition
  • m:1 – 1 item has been modified

Issuing svn add and check in causes the prompt to refreshed.

image 

More PowerShell and Congressional Earmarks

October 11th, 2008 / Development in a Blink

I blogged about visualizing US Congressional Earmarks from the Taxpayers for Common Sense data here. It is an itemized list of Senator and House of Representatives requesting funds. Included in the data are the categories Bill, Bill Section and Bill Subsection the request was made against.

The PowerShell script Do-Analysis does two things. It preps the data, creating nested dictionaries, and provides four functions for discovery Get-Names,  Get-Requested, Get-NotRequested and Transform-Data.

Note, the Build-Dataset function processes the data with dimensions present in the original file.  Typing Build-Dataset House Section will set up the dataset for answering question “How did the House of Representative vote?”.

Senators first

After dot sourcing the file . .\Do-Analysis. You can look at what Hillary Clinton requested. Get-Requested Clinton here is a sample

Name                                    Value
—-                                    —–
Agriculture                             1
Aircraft Procurement                    1
Conservation Programs                   1
Corps of Engineers                      1
Defense Health Program                  1

For Senator Ted Stevens Get-Requested Stevens

Name                      Value
—-                      —–
Agriculture               1
Bureau of Land Management 1
Conservation Programs     1
Corps of Engineers        1
Defense Health Program    1

And finally Senator Jon Kyl of Arizona

Name                                    Value
—-                                    —–
Bureau of Reclamation                   1
Corps of Engineers                      1
Department of Health and Human Services 1
Department of Justice                   1
Environmental Protection Agency         1

We can also look at what Senator Clinton didn’t request Get-NotRequested Clinton

Name                      Value
—-                      —–
Administrative Provisions 0
BRAC Projects 2005        0
Bureau of Indian Affairs  0
Bureau of Land Management 0
Bureau of Reclamation     0

What if we wanted to answer the question who requested funds in the bill section Rural Development Programs? No problem. Transform-Data pivots the nested dictionaries. Now we can use the same Get-Requested function and pass Rural Development Programs as the query. Get-Requested ‘Rural Development Programs’. The results are the Senators who requested monies for this section of the bill.

Name      Value
—-      —–
Baucus    1
Harkin    1
Johnson   1
Kohl      1
Leahy     1
Lincoln   1
McConnell 1
none      1
Pryor     1
Specter   1
Tester    1

Use Transform-Data again and the dictionaries are back in their original shape.

Deeper Analysis

Shaping the dataset Senator->Bill Section -> 0|1, where 0 indicates no vote and 1 voted for, answers what Senators requested. Transform-Data re-shapes the dictionaries into Bill Section -> Senator -> 0|1 so we can answer who voted for what bill sections.

In follow up posts we’ll look at relationships between the dimensions. Iterating over the data and calculating Pearson correlation coefficients so we can show neighbors and links between bill sections and senators voting patterns.

References

These techniques are from the book Programming Collective Intelligence in Chapter 2 using PowerShell instead of Python.

Jon Udell posted The Congressional content management system. He highlights:

The absurdity of expecting people to make sense of complex texts

And notes that in one of the Congressional PDFs an Xml file name is embedded. The good news, Congress may be using automation to do revisions. The bad news, it is not publicly available.

Download

Here are the two files for download if you want to play with PowerShell and the data.

More PowerShell and Congressional Earmarks

October 11th, 2008 / Development in a Blink

I blogged about visualizing US Congressional Earmarks from the Taxpayers for Common Sense data here. It is an itemized list of Senator and House of Representatives requesting funds. Included in the data are the categories Bill, Bill Section and Bill Subsection the request was made against.

The PowerShell script Do-Analysis does two things. It preps the data, creating nested dictionaries, and provides four functions for discovery Get-Names,  Get-Requested, Get-NotRequested and Transform-Data.

Note, the Build-Dataset function processes the data with dimensions present in the original file.  Typing Build-Dataset House Section will set up the dataset for answering question “How did the House of Representative vote?”.

Senators first

After dot sourcing the file . .\Do-Analysis. You can look at what Hillary Clinton requested. Get-Requested Clinton here is a sample

Name                                    Value
—-                                    —–
Agriculture                             1
Aircraft Procurement                    1
Conservation Programs                   1
Corps of Engineers                      1
Defense Health Program                  1

For Senator Ted Stevens Get-Requested Stevens

Name                      Value
—-                      —–
Agriculture               1
Bureau of Land Management 1
Conservation Programs     1
Corps of Engineers        1
Defense Health Program    1

And finally Senator Jon Kyl of Arizona

Name                                    Value
—-                                    —–
Bureau of Reclamation                   1
Corps of Engineers                      1
Department of Health and Human Services 1
Department of Justice                   1
Environmental Protection Agency         1

We can also look at what Senator Clinton didn’t request Get-NotRequested Clinton

Name                      Value
—-                      —–
Administrative Provisions 0
BRAC Projects 2005        0
Bureau of Indian Affairs  0
Bureau of Land Management 0
Bureau of Reclamation     0

What if we wanted to answer the question who requested funds in the bill section Rural Development Programs? No problem. Transform-Data pivots the nested dictionaries. Now we can use the same Get-Requested function and pass Rural Development Programs as the query. Get-Requested ‘Rural Development Programs’. The results are the Senators who requested monies for this section of the bill.

Name      Value
—-      —–
Baucus    1
Harkin    1
Johnson   1
Kohl      1
Leahy     1
Lincoln   1
McConnell 1
none      1
Pryor     1
Specter   1
Tester    1

Use Transform-Data again and the dictionaries are back in their original shape.

Deeper Analysis

Shaping the dataset Senator->Bill Section -> 0|1, where 0 indicates no vote and 1 voted for, answers what Senators requested. Transform-Data re-shapes the dictionaries into Bill Section -> Senator -> 0|1 so we can answer who voted for what bill sections.

In follow up posts we’ll look at relationships between the dimensions. Iterating over the data and calculating Pearson correlation coefficients so we ca show neighbors and links between bill sections and senators voting patterns.

References

These techniques are from the book Programming Collective Intelligence in Chapter 2 using PowerShell instead of Python.

Jon Udell posted The Congressional content management system. He highlights:

The absurdity of expecting people to make sense of complex texts

And notes that in one of the Congressional PDFs an Xml file name is embedded. The good news, Congress may be using automation to do revisions. The bad news, it is not publicly available.

Download

Here are the two files for download if you want to play with PowerShell and the data.

More PowerShell and Congressional Earmarks

October 11th, 2008 / Development in a Blink

I blogged about visualizing US Congressional Earmarks from the Taxpayers for Common Sense data here. It is an itemized list of Senator and House of Representatives requesting funds. Included in the data are the categories Bill, Bill Section and Bill Subsection the request was made against.

The PowerShell script Do-Analysis does two things. It preps the data, creating nested dictionaries, and provides four functions for discovery Get-Names,  Get-Requested, Get-NotRequested and Transform-Data.

Note, the Build-Dataset function processes the data with dimensions present in the original file.  Typing Build-Dataset House Section will set up the dataset for answering question “How did the House of Representative vote?”.

Senators first

After dot sourcing the file . .\Do-Analysis. You can look at what Hillary Clinton requested. Get-Requested Clinton here is a sample

Name                                    Value
—-                                    —–
Agriculture                             1
Aircraft Procurement                    1
Conservation Programs                   1
Corps of Engineers                      1
Defense Health Program                  1

For Senator Ted Stevens Get-Requested Stevens

Name                      Value
—-                      —–
Agriculture               1
Bureau of Land Management 1
Conservation Programs     1
Corps of Engineers        1
Defense Health Program    1

And finally Senator Jon Kyl of Arizona

Name                                    Value
—-                                    —–
Bureau of Reclamation                   1
Corps of Engineers                      1
Department of Health and Human Services 1
Department of Justice                   1
Environmental Protection Agency         1

We can also look at what Senator Clinton didn’t request Get-NotRequested Clinton

Name                      Value
—-                      —–
Administrative Provisions 0
BRAC Projects 2005        0
Bureau of Indian Affairs  0
Bureau of Land Management 0
Bureau of Reclamation     0

What if we wanted to answer the question who requested funds in the bill section Rural Development Programs? No problem. Transform-Data pivots the nested dictionaries. Now we can use the same Get-Requested function and pass Rural Development Programs as the query. Get-Requested ‘Rural Development Programs’. The results are the Senators who requested monies for this section of the bill.

Name      Value
—-      —–
Baucus    1
Harkin    1
Johnson   1
Kohl      1
Leahy     1
Lincoln   1
McConnell 1
none      1
Pryor     1
Specter   1
Tester    1

Use Transform-Data again and the dictionaries are back in their original shape.

Deeper Analysis

Shaping the dataset Senator->Bill Section -> 0|1, where 0 indicates no vote and 1 voted answers what Senators requested. Transform-Data re-shapes the dictionaries into Bill Section -> Senator -> 0|1 so we can answer who voted for what bill sections.

In follow up posts we’ll look at relationships between the dimensions. Iterating over the data and calculating Pearson correlation coefficients so we ca show neighbors and links between bill sections and senators voting patterns.

References

These techniques are from the book Programming Collective Intelligence in Chapter 2 using PowerShell instead of Python.

Jon Udell posted The Congressional content management system. He highlights:

The absurdity of expecting people to make sense of complex texts

And notes that in one of the Congressional PDFs an Xml file name is embedded. The good news, Congress may be using automation to do revisions. The bad news, it is not publicly available.

Download

Here are the two files for download if you want to play with PowerShell and the data.

PowerShell Scripts and NetMap for US Earmarks

September 29th, 2008 / Development in a Blink

Below is the download of the tools I put together to produce this

FY’ 08 Earmarks with PowerShell, PowerGadgets and NetMap

The zip contains

Get PowerGadgets here

Happy mining

More FY’ 08 Earmarks with PowerShell, PowerGadgets and NetMap

September 28th, 2008 / Development in a Blink

After posting Mapping FY’ 2008 Earmarks for the United States with PowerShell and NetMap. I poked around the Taxpayers for Common Sense data and charted the State by State Earmarks Per Capita information with PowerGadgets.

Turns out Alaska received the largest earmarks by more than double its nearest neighbor.

image

Then I ordered the data by final amount received. Alaska was #10 with $346 million. New York is #6 with $437 million.

image

The network below graph is from drilling down on Alaska’s earmarks.

Three attributes (data dimensions) Bill, Bill Section and Bill Sub Section are categorized. The Bill Section and Bill Sub Section relationships are then graphed using MSR NetMap.

image

Download the graphic

Here is the download for better viewing.

PowerShell’s .Net integration and explorative scripting makes for interesting business intelligent data mining.

Mapping FY’ 2008 Earmarks for the United States with PowerShell and NetMap

September 27th, 2008 / Development in a Blink

I read Senate sends big spending bill to Bush to sign. At the end it said

Taxpayers for Common Sense, a watchdog group, discovered 2,322 pet projects totaling $6.6 billion. That included 2,025 in the defense portion alone that cost a total of $4.9 billion.

On http://www.taxpayer.net there is TCS Database of FY’ 08 Earmarks that can be downloaded. I did and proceeded to do some data mining. 

States Receiving Defense Earmarks

This is a network graph from data in the TCS Earmarks Excel document. Each line representing a state receiving an earmark from a bill.

  • Ag-Rural Development-FDA
  • Commerce, Justice & Science
  • Defense
  • Energy & Water
  • Financial Services
  • Homeland Security
  • Interior
  • Labor-HHS-Education
  • Legislative Branch
  • Military Construction
  • State-Foreign Ops
  • Transportation and Housing & Urban Development

The red lines are states receiving money from the Defense bill.

image

Approach

I edited the spreadsheet, removing the Title, summary information and modified some headings to remove spaces. Then I exported the spreadsheet to a csv format. Using PowerShell, I imported the data and filtered out data records where state and bill were empty. Earmarks allocated to more than one state in a single record were also filtered out. The data was transformed into records of the form Source and Target and then mapped as a directed graph with NetMap. I used the PowerShell script I posted about in Using PowerShell and World Bank Data  with Microsoft’s NetMap graphing libraries.

Note

It took longer to write this post than to mine and graph the data. There are other interesting columns in the spreadsheet like the House and Senate requestors and party. This data may need some work but I graph it and look for patterns.

List-Partition in PowerShell

September 25th, 2008 / Development in a Blink

Partitions a list based on a predicate. The first list contain the elements that match and the second the ones that don’t.

Function List-Partition {
 param($fun, $list)

 $list1=@(); $list2=@()

 $list |
   ForEach {
     if(& $fun $_) {
       $list1+=@($_) } else {$list2+=@($_)
     }
 }

 $list1, $list2
}

Works with a simple list

$numList = 1,2,3,5,7,11,13,16,17,19,23
$fun = {param($x) $x % 2 -eq 0}

$list1,$list2 = List-Partition $fun $numList
"[$list1] [$list2]"
Results

[2 16] [1 3 5 7 11 13 17 19 23]

Works with a list of Objects

$list = @()
$list += New-Person John Doe
$list += New-Person Jane Doe
$list += New-Person Tom Doe
$list += New-Person Harry Doe
$list += New-Person George Carlin
$list += New-Person Lenny Bruce

$list1,$list2 = List-Partition {param($x) $x.Last -eq ‘Doe’} $list
"List1"
$list1

"`nList2"
$list2
Results

List1

First  Last

—–  —-

John   Doe

Jane   Doe

Tom    Doe

Harry  Doe

List2

George Carlin

Lenny  Bruce

PowerShell is motivated by functional programming

September 24th, 2008 / Development in a Blink

A line out of Tomas Petricek upcoming book, Real World Functional Programming.

Luke Hoban came and presented some great F# ideas at Lab49. One idea is recognizing patterns in code where the plumbing can be lifted so a function can be passed to get the job done. Here is some simple plumbing. You want to process a list of numbers and return the largest.

Get-Maximum

This approach let’s you stash the plumbing for processing the list and the setting and storing of the limit.

param($numbers, [scriptblock]$sb)

$numbers |
 ForEach {$max = [int]::MinValue} {$max = & $sb $_ $max} {$max}

Functions

Now write some functions, scriptblocks in PowerShell, that can be passed to Get-Maximum to determine the largest value.

$std compares the current number to $max and returns the correct value.

$std = {param($n, $max) if($n -gt $max) {$n} else {$max}}

$pct takes the current number, adds 10%, compares and returns the largest.

$pct = {param($n, $max) if($n*(1+.1) -gt $max) {$n} else {$max}}

Ways the functions can be used

This call returns 5.

.Get-Maximum (1,1,3,0,2,-1,5) $std

Here you pass an array of functions to the a function to get a maximum number in different ways. This approach let’s you concentrate on the business value. Calculating a result.

$pct, $std | ForEach {.Get-Maximum (.87,.80) $_}
Produces:
0.8 
0.87

Writing and testing of the algorithms can be done and saved. Then parameterize them in different ways.

One of the most important properties of LINQ: its flexibility

September 14th, 2008 / Development in a Blink

I am looking at extending LINQ for a current project. I Googled for custom linq and found resources like Charlie Calvert’s Links to LINQ then Introducing Linq to Amazon and then LINQ in Action. Other resources, Bart De Smet’s many detailed posts on LINQ that included LINQ through PowerShell.

Reading LINQ in Action, the authors start with Extension Methods. Which got me thinking about extension methods in PowerShell. Experimenting a while back I created a static class, added a method, used the this syntactical sugar but loading it up PowerShell and trying it did not work.

Continuing research on customization of LINQ led me to Bart’s post Extension Methods in Windows PowerShell. He used PowerShell’s feature called the Extended Type System. With this approach you to create Xml and use the cmdlet Update-TypeData.

You can view the extend types shipped with PowerShell like this

notepad %windir%\system32\WindowsPowerShell\v1.0\types.ps1xml

Here is Bart’s illustration of what you can find in this file and its use.

<Type>
    <Name>System.Array</Name>
    <Members>
        <AliasProperty>
            <Name>Count</Name>
            <ReferencedMemberName>Length</ReferencedMemberName>
        </AliasProperty>
    </Members>
</Type>

PS > $a = "Jimmy", "John"
PS > $a.Count
2

Take a look at Jeffery Snover’s post Hate Add-Member? (PowerShell’s Adaptive Type System to the Rescue). He calls this feature the Adaptive Type System and shows how to extend every object so it can dynamically add Methods and Properties to itself.

Back to LINQ

Bart has a new post outlining one of the most important properties of LINQ: its flexibility.

The potential of LINQ is infinity², the reason for it being the infinite fan-in (LINQ though C#, VB, F#, PowerShell, various transport mechanisms, etc) multiplied by the infinite fan-out (LINQ to SQL, Entities, XML, Objects, DataSets, SharePoint, Active Directory, Amazon, etc).

Bart’s full post Who ever said LINQ predicates need to be Boolean-valued?

PowerShell VI Toolkit Community Extension

September 10th, 2008 / Development in a Blink

A community open source project is online for VMWare PowerShell Toolkit. There is some source scripts posted, looks like they are using the new Module feature of PowerShell Version 2.

Good stuff.