PowerShell

Expand-Alias for PowerShell Integrated Scripting Environment

January 3rd, 2009 / Development in a Blink

Get your PowerShell code ready for production by expanding your shorthand alias syntax.

Use the shorthand approach to knock out your scripts. Then expand them. Makes your code more readable and maintainable in the long run.

image  

 

 

 

 

 

 

Press Ctrl+Shift+E

image

 

 

 

 

 

 

 

How To

Add this code to your Microsoft.PowerShellISE_profile.ps1

Function Expand-Alias {
  $content=$psise.CurrentOpenedFile.Editor.text
  [System.Management.Automation.PsParser]::Tokenize($content, [ref] $null) |
    Where { $_.Type -eq ‘Command’} |
    Sort StartLine, StartColumn -Desc |
    ForEach {
      if($_.Content -eq ‘?’) {
        $result = Get-Command ‘`?’ -CommandType Alias
      } else {
        $result = Get-Command $_.Content -CommandType Alias -ErrorAction SilentlyContinue
      }

      if($result)
      {
        $psise.CurrentOpenedFile.Editor.Select($_.StartLine,$_.StartColumn,$_.EndLine,$_.EndColumn)
        $psise.CurrentOpenedFile.Editor.InsertText($result.Definition)
      }
    }
}

if( -Not ($psISE.CustomMenu.Submenus | ?{$_.DisplayName -eq ‘Expand Alias’}) ) {
    $null = $psISE.CustomMenu.Submenus.Add("Expand Alias", {Expand-Alias}, ‘Ctrl+Shift+E’)
}

PowerShell and F#

December 30th, 2008 / Development in a Blink

Two great tastes that taste great together. Andy Schneider posts how to use PowerShell’s Add-Type cmdlet to get at the FSharp Code Provider and inline F# text in a PowerShell script and then access it from PoSh.

Inline F# in PowerShell

PowerShell

December 30th, 2008 / Development in a Blink

James Brundage, from the PowerShell team, has several posts up since the release of CTP3.

One is Write-CommandBlogPost another is The CodeDownloader Module.

I modified his Write-CommandBlogPost. Now I can post PoSh code and it can be pulled off my blog programmatically.

Additionally, I created a Microsoft Live Writer Plug-In, embedding  the PowerShell Scripting engine so with a few clicks I can inline my PoSh code in my WordPress blog post.

image

 

Sample PoSh Code

Write-Hello.ps1

Synopsis:

Writes Hello to the console

Syntax:

C:\PoSHScripts\Write-Hello.ps1 [[-who] [<Object>]] [<CommonParameters>]

Detailed Description:

Sample script writes Hello to the console

Examples:

--------------------- EXAMPLE 1 ---------------------

Write-Hello "World"

Command Parameters:


Name Description
who Who to say hello to

Here’s Write-Hello.ps1:

<#
.Synopsis
  Writes Hello to the console
.Description
  Sample script writes Hello to the console
.Parameter Who
  Who to say hello to
.Example
  Write-Hello "World"
#>

param($who)
"Hello $who"

Automatically generated with a custom version of Write-CommandBlogPost

Things you need

Download Live Writer C# Project.

You can download the Live Writer SDK and build the DLL, below is the Help About showing the version of Live Writer I am using.

Or copy the BlogPoSh.dll and Write-CommandBlogPost.ps1 from the bin/debug to C:\Program Files\Windows Live Writer\Plugins.

These steps are setup in the VS IDE for the project.

image

Upcoming Post

I will post the script that is ‘added’ to James’ Code Downloader which enables programmatic extraction the code above.

In James’ examples, he provides plug-in scripts that can programmatically download scripts from Microsoft PowerShell Blog posts and PoshCode.org.

Jingle Shell, Jingle Shell, PowerShell V2 CTP3 is RTW

December 23rd, 2008 / Development in a Blink

via The PowerShell Guy

Download Windows PowerShell V2 CTP3

Download WinRM 2.0 CTP3 (required for PowerShell remoting)

Some New Things

  • Over 60 new cmdlets
  • Adding/Removing/Renaming computers
  • Cmdlets for event logs
  • Cmdlets for WS-Man functionality and even a WS-Man provider
Windows PowerShell ISE
  • Supports a graphical debugger
  • Context sensitive F1 help
  • A Programmable interface

via Hemant Mahawar Windows PowerShell Team

Advanced Functions

Cmdlets++ ?

Function Test-LeapYear {
  param( 
   [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)] 
   [Int]$Year 
  ) 
}
 

Attributing parameters this way gives you

  • Ensures that you send it the right number of parameters
  • Supports pipelining
  • Tab-Completion of parameter names
  • Supports Get-Command, (Get-Command Test-LeapYear).Parameters

You can also provide granular inline documentation that works with Get-Help. Like Synopsis, Example, Link and more.

via Jeffrey Snover Windows PowerShell Team

I am really looking forward to working with PowerShell in 2009.

PowerShell: Querying Windows Desktop Search

December 21st, 2008 / Development in a Blink

I saw this IronPython: Querying Windows Desktop Search.

Then took Lee Holmes’ Invoke-SqlCommand and hacked up Invoke-WDS 

param(
    $sqlCommand = "SELECT FileName FROM SYSTEMINDEX Where System.FileExtension =’.ps1′  Order By FileName"
)
 
$connectionString = "Provider=Search.CollatorDSO;Extended Properties=’Application=Windows’;"
 
## Connect to the data source and open it
$connection = New-Object System.Data.OleDb.OleDbConnection $connectionString
$command = New-Object System.Data.OleDb.OleDbCommand $sqlCommand,$connection
$connection.Open()
 
## Fetch the results, and close the connection
$adapter = New-Object System.Data.OleDb.OleDbDataAdapter $command
$dataset = New-Object System.Data.DataSet
[void] $adapter.Fill($dataSet)
$connection.Close()
 
## Return all of the rows from their query
$dataSet.Tables | Select-Object -Expand Rows

The Script Center has a deeper treatment here Seek and Ye Shall Find Scripting Windows Desktop Search 3.0

You can create queries like

@"
 SELECT 
  System.FileName 
 FROM 
  SYSTEMINDEX 
 WHERE 
  System.Music.Artist = ‘John Lennon’ 
 OR
  System.Music.Artist = ‘George Harrison’
"@

PowerShell is going to be the cluster scripting language for the future

December 20th, 2008 / Development in a Blink

PowerShell for Failover Clustering in Windows Server 2008 R2

The team is looking for feedback on their current set of 68 cmdlets. They are using the new Module feature of PowerShell version 2.

To get a complete list of the cmdlets, run the following command: Get-Command -Module FailoverClusters

Example of creating a highly available File Server

$Node1 = "symonp-n1"
$Node2 = "symonp-n2"
$FileServerGroupName = "symonp-fsBlog"
$FileServerDiskResourceName = "Cluster Disk 1"
 
# Create a highly available file server
Add-ClusterFileServerRole `
 -Storage $FileServerDiskResourceName `
 -Name $FileServerGroupName

List of Verbs and Nouns

Verbs Nouns
  • Add
  • Block
  • Clear
  • Fail
  • Get
  • Grant
  • Move
  • New
  • Remove
  • Resume
  • Set
  • Start
  • Stop
  • Suspend
  • Test
  • Update
  • Cluster
  • ClusterAccess
  • ClusterAvailableDisk
  • ClusterDisk
  • ClusterDiskReservation
  • ClusterFileServerRole
  • ClusterGenericApplicationRole
  • ClusterGenericScriptRole
  • ClusterGenericServiceRole
  • ClusterGroup
  • ClusterIPResource
  • ClusterLog
  • ClusterNetwork
  • ClusterNetworkInterface
  • ClusterNode
  • ClusterOwnerNode
  • ClusterParameter
  • ClusterPrintServerRole
  • ClusterQuorum
  • ClusterResource
  • ClusterResourceDependency
  • ClusterResourceDependencyReport
  • ClusterResourceType
  • ClusterServerRole
  • ClusterSharedVolume
  • ClusterVirtualMachineConfiguration
  • ClusterVirtualMachineRole

I demoed Microsoft Oslo MGrammar and walked the Graph with PowerShell

December 19th, 2008 / Development in a Blink

I presented on Microsoft Oslo to a number of developers at Lab49 where I work.

Download it here

Download the Oslo SDK October 2008 CTP

Oslo Developer Center

Demo Highlights

  • Show Intellipad and build a Contacts DSL (lifted from the PDC 2008)
  • Generate C# objects from the input through the grammar using MGraphXamlReader
  • MWindow sample from the Oslo team
  • Code generation using PowerShell. PowerShell scripts walk the graph and generate PSObjects from the input through the grammar.

Lots of discussion about Intellipad and it enabling quick turn around of grammars. Intellipad’s use of VS 2010 Editor control based on WPF, MEF and IronPython as the way to extend it. How to build interpreters in C# with LINQ Expressions.

PowerShell and MGrammar

Download the demo and the Oslo SDK. Start PowerShell and go to the CodeGen directory. Type the following and press enter.

.\Do-All.ps1 .\Atom.cfg

Do-All.ps1 kicks off the C# code generation based on the configuration in Atom.cfg. The DSL in Atom.cfg works based on the MGrammar in ConfigurationDSL.mg. It turns this configuration into C# code that can read a file with this layout creating C# structs.

What is generated is entirely up to you. The supporting PowerShell scripts return an array of objects with this information mapped to it. You can generate BPC Xml format files for use with Sql or anything else you can think of.

Atom.cfg
Atom {
  RecordName  1-6  : Text;
  Serial      7-11 : Text;
  Name       13-16 : Text;
  AltLoc     17-17 : Text;
  ResName    18-20 : Text;
  ChainId    22-22 : Text;
  ResSeq     23-26 : Text;
  iCode      27-27 : Text;
  X          31-38 : Real;
  Y          39-46 : Real;
  Z          47-54 : Real;
  Occupancy  55-60 : Real;
  TempFactor 61-66 : Real;
  Element    77-78 : Text;
  Charge     79-80 : Text;
}

Build-Tree.ps1 - Calls Enumerate.ps1 which compiles the grammar file, loads the dynamic parser and enumerates the graph while emitting nodes.

The nodes are processed  in the swtich statement that recognize identifiers and creates PSObjects with properties describing the start and end locations, the field name and data type.

In the end you get and array of objects describing the layout of an input text file.

Pipe this result to CodeGen-Struct then CodeGen-FileRead to create the data structure and FileRead operations.

Resources

Mark your Calendars, PowerShell Santa Claus is coming to town

December 15th, 2008 / Development in a Blink

This Thursday, December 18th, Jeffery Snover, creator of PowerShell, and Bruce Payette, principal author of the language implementation, are on the PowerScripting Live Show.

Bruce is also author of the excellent book Windows PowerShell in Action.

I need to start thinking of what I want to ask Santa for Christmas.

NetMap has resurfaced as NodeXL

December 15th, 2008 / Development in a Blink

I posted about how to use PowerShell and NetMap before.

It Had Disappeared

We talked about it on the PowerScripting Podcast. Steve Murawski was in the audience and decided to do some digging, He found it on CodePlex under NodeXL.

Steve leveraged PowerShell’s compositional nature using my Show-NetMap with his Get-ADMapObject and Show-ADObject code to visualize Active Directory.

Using PowerShell to get the mist into the bottle

December 9th, 2008 / Development in a Blink

James Brundage posted two PowerShell write-ups. One on his Get-Web script and the other on his Get-MarkupTag.

Here they are at work on the US Senate web page.

$url="http://www.senate.gov/general/contact_information/senators_cfm.cfm"
Get-MarkupTag option (Get-Web $url) | % {$_.Xml}

Turns This

image

 

Into This

Structured data.

image

PowerShell – program with hand grenades not sniper rifles

December 5th, 2008 / Development in a Blink

Jeffery Snover, inventor of PowerShell, makes point when Erik Meijer sits talks with him and talks about pipelines, monads and some things upcoming in PowerShell.

Here is the video:

Expert to Expert: Erik Meijer and Jeffrey Snover - Inside PowerShell

Highlights

  • In Window 7, Windows Diagnostics are all PowerShell based
  • PowerShell is the first shell to support transactions
  • PowerShell optimizes the person not the CPU
  • Integrated Script Editor is based on the VS 2010 Editor
  • Covers how the Object Flow Engine works, it’s push based which is opposite of IEnumerable
  • Begin/Process/End components of a script are stream oriented
  • Parallel? Not yet, at the top level multiple Runspaces (A runspace provides a mechanism for hosting applications to execute pipelines programmatically in a well-constructed manner)
  • Batch files are a disease you die with not of

The Interview went well

December 5th, 2008 / Development in a Blink

Thanks to Hal and Jon, hosts of the PowerScripting Podcast. It was great to chat with them and the folks who joined in.

I think it was the first time being in a group of techies where I didn’t have to field the question “Why did they use a $ sign for variable names”.

We jumped in feet first and swam to the deep end.

Thanks guys and great questions for the audience too.

Excel functions for .NET written in F#

December 4th, 2008 / Development in a Blink

Luca Bolognese of Microsoft posted the code and did a write up here. Plus he has 201,349 test cases for the library.

You can use them in PowerShell too!

You need to have the F# September CTP installed here.

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 $_} }