PowerShell

Using PowerShell and World Data (now a public API) with Microsoft’s NetMap free graphing libraries

September 5th, 2008 / Development in a Blink

The World Bank’s first API offers 114 indicators from key data sources and 12,000 development photos. We are releasing this API because we believe this information can be mapped, visualized and mashed up in an unlimited number of ways that will help develop a better understanding of trends and patterns around key development issues.

HERE

Visualize the Data with PowerShell and NetMap

I posted on Microsoft Research .NetMap and PowerShell. This script visualizes countries by Income Level, Lending Types and Regions.

param($category="incomeLevel")

. .Show-NetMap

$wc = New-Object net.webclient
$url="http://open.worldbank.org/rest.php?
method=wb.countries.get&per_page=250&api_key=<your api key>"

([xml]$wc.DownloadString($url)).rsp.countries.country |
  % {
   if($_.iso2code -and $_.name) {
    new-object psobject |
     add-member -pass noteproperty source ($_.$category.‘#text’) |
     add-member -pass noteproperty target ($_.name)
    }
  } | Show-NetMap -w 250 -l F

Income Level

image

 

image

 

 

Lending Type

image

 

 

image

 

Region

image 

 

image

icomasoft startup integrates PowerShell into VMWare Virtual Infrastructure

September 1st, 2008 / Development in a Blink

Earlier this year VMWare, global leader in virtualization solutions, opened the beta for its Powershell-based VMware Infrastructure Toolkit (VI Toolkit).

icomasoft is providing, in beta, VI PowerScripter which provides users with an unprecedented ease-of-use and simplification of many administrative tasks around VirtualCenter ESX and i3.

  • Reduction of required resources for administrative tasks
  • Easy monitoring and analysis of the virtual infrastructure behavior
  • Mass configuration and individual configuration can be executed with one single script
  • Almost unlimited extension of the basic functionality of VMware infrastructures and free ESXi

We’ll continue to watch the PowerShell management Library for Hyper-V on CodePlex currently at 66 cmdlets and counting.

Microsoft Research .NetMap and PowerShell

August 31st, 2008 / Development in a Blink

Microsoft Research released .NetMap. A free Open Source tool for creating and viewing network graphs. Included is a set of .Net libraries to add network graphs to custom applications.

Out of the box the tool supports some great features. One feature is the support for automatic layout of the vertices like Circular, Grid and Sinusoid Horizontal to name a few.

Interactive features I like are the ability to drag the resulting nodes around on the drawing surface and clicking on a node to highlight the edges in and out of the node.

Excel Integration

A unique feature is the VSTO component for Excel. In a spreadsheet you can specify Vertex 1 and Vertex 2. The associated task pane lets you read the spreadsheet and layout the graph with different options.

image

PowerShell Integration

I worked up a few PowerShell scripts so I could produce graphs from different sources by:

  1. Importing a comma separated values file
  2. Importing an Excel Spreadsheet
  3. Running commands against the system

Here’s are the commands and their results. The csv file and excel file contain the same vertex information for the graph.

Contents of test.csv

source,target
a,b
a,c
c,a
d,a
e,h
f,i
g,j
a,f

Import-Csv test.csv | Show-NetMap

image

image Same graph as above. Clicking on node “a” highlights all in and out edges.

 

 

 

 

 

 

.\Import-ExcelSheet -File test -SheetName Test | Show-NetMap

image

 

.\Graph-Object (Get-Process powershell, moe*)

 image

 

.\Graph-Object (Get-Service net*p*)

image

Notes

Download Here

Debugging Silverlight with Windbg and SOS

August 25th, 2008 / Development in a Blink

If you have hangs, performance, memory issues, exceptions or crashes in Silverlight applications you can debug them using windbg and sos just like you would if the issues occurred in other .net applications.

http://blogs.msdn.com/tess/archive/2008/08/21/debugging-silverlight-applications-with-windbg-and-sos-dll.aspx

Maybe this works with Automated Debugging using WinDbg and PowerShell.

Automated Debugging using WinDbg and PowerShell

August 18th, 2008 / Development in a Blink

On CodePlex PowerDbg.

Enables you to easily create PowerShell scripts that automate the debugging session interacting with WinDbg.

Can be used for

  • Kernel Mode
  • User Mode
  • Post-Mortem debugging
  • Live Debugging
  • Native code
  • Managed code

In addition

  • Easier to create scripts
  • Leverage the .NET Framework
  • Debugging and tracing features
  • Code reuse through functions and cmdlets
  • Easier maintenance
  • Easier to build large scripts
  • Easier to format and display the important information

Coral8 PowerShell Provider

August 13th, 2008 / Development in a Blink

Scott Weinstein posted a PowerShell implementation to explore a remote Coral8 server stream.

Coral8 provides a Complex Event Processing platform.

PowerShell script New-PDF made possible by PDFSharp

August 13th, 2008 / Development in a Blink

PDFsharp is the Open Source library that easily creates PDF documents from any .NET language without .  The same drawing routines can be used to create PDF documents, draw on the screen, or send output to any printer. Neither Adobe’s PDF Library nor Acrobat are required.

Thanks to Marco Shaw for spotting this.

Hello World

The New-PDF PowerShell script is a port of the PDFSharp Hello World sample.

Download It

PSParser::Tokenize, PowerShell and GLEE

July 26th, 2008 / Development in a Blink

I tweaked the Get-ExternalScripts code from Use PSParser::Tokenize to discover other scripts a PowerShell script is calling to create PSObjects and hooked it up to PowerShell + (GLEE - Graph Layout Execution Engine) producing this call tree visualization starting at the Director script.

image

Use PSParser::Tokenize to discover other scripts a PowerShell script calling?

July 26th, 2008 / Development in a Blink

At a glance Get-ExternalScripts takes a script, shows the scripts it calls, and, in turn shows the scripts each of those scripts call.

Composability

Reduces complexity by having small discrete units of work that can be glued together in different combinations to achieve different behaviors. Depending on granularity, this enables the grouping and gluing of code into different logical packages which deliver different functionality.

Tasks

The same idea can be applied in building separate PowerShell files.

For example, you may want to

  1. Get the latest files from a network drive
  2. Copy them locally
  3. Decompress them
  4. Check their time stamps
  5. Check there content for correctness

Common functions are kept in Support.ps1, which is dot sourced in each of the others. Finally, there may be a directing script which manages the workflow, transitions and return codes.

Using a text editor to open, view and identify each called script can be a challenge.

Get-ExternalScripts

image

Get-ExternalScripts uses [System.Management.Automation.PsParser]::Tokenize from Version 2 of PowerShell.

It reads the script, tokenizes it, pulls out the commands/command arguments, calls Get-Command and does this recursively for each script.

#requires -version 2
param(
  $fileName=$(throw "please specify a PowerShell file")
)

function Get-ExternalScripts($p,$t=0) {
  $content = [IO.File]::ReadAllText( ( Resolve-Path $p ))
  $tokens  =
   [System.Management.Automation.PsParser]::Tokenize($content, [ref] $null)

$t+=3

$externalScripts = $tokens |
  Where {$_.type -match ‘command’} |
    ForEach { Get-Command `
     -ErrorAction silentlycontinue `
     $_.content `
     -CommandType ExternalScript }

  if($externalScripts) {
    $externalScripts |
      ForEach {
        write-host -no (" " * $t)
        write-host (Split-Path -Leaf $_.Definition)
        Get-ExternalScripts $_.Definition ($t)
      }
  }
}

write-host (Split-Path -Leaf $fileName)
Get-ExternalScripts $fileName

Download

This is another way to use PowerShell to discover about PowerShell. PSParser::Tokenize returns a lot of detail. I am sure combining that and other PowerShell Cmdlets will turn up some real gems.

Here are the script and demo:

To Ship is to Choose

July 19th, 2008 / Development in a Blink

Is the PowerShell team mantra.

I’d like it to work in the VS IDE with deep intellisense, the DLR, ASP and more.

Jeffery Snover sums it up:

We are trying to solve the problem of administrating and automating Windows […] it would be a breach of our responsibilities to pursue ASP programming.

Upcoming features Remoting, Jobs, Eventing, etc.

Meanwhile

The  PowerShell community delivers:

PowerShell Pages is a great how to reference, written by Andre de Cavaignac, colleague at Lab49.

PowerShell Quiz

July 17th, 2008 / Development in a Blink
Fellow Lab49er Daniel Simon asked why the different results?
PS > $a=1,(2,3)
PS > $a.Count
2
PS > ($a | % {$_}).count
3

What do you think?

Jacquard Loom, Punch Cards and PowerShell

July 5th, 2008 / Development in a Blink

In 1801, Joseph Marie Jacquard invented an improved textile loom. The Jacquard loom was the first machine to use punched card. These punched cards controlled the weaving producing beautiful patterns in a style previously accomplished only with patience, skill, and hard work.

Knitting

Has a programming language. Follow it and you produce elaborate patterns and designs. Including sections which are narrowed, elastic, angled, mitered, curved and more.

co 24
Row 1 (WS):
 *k1 p2* Repeat to * 2 times.
  [k2tog, yo, k1, yo, cdd, ssk, yo, k3]
    repeat 2 times end k4

These instructions increase or decrease the number of stitches being worked to produce the desired effect.

PowerShell Knitting Language

The knitting instructions don’t tell how many stitches you’ll end with when your done with a row.  If you missed a few, your piece will not look right and sometimes there are printed errors.

Here is PowerShell function which lets me express the pattern in terms of the knitting instructions.

function KnittingLanguage($pgm) {
  switch -regex ($pgm) {
    k2tog   { "[decrease] knit two together"       }
    yo      { "[increase] yarn over"               }
    ssk     { "[decrease] slip, slip, knit"        }
    cdd     { "[decrease] central double decrease" }
    "^kd$" { "knit " * $_.substring(1)            }
    "^pd$" { "purl " * $_.substring(1)            }
  }
}

function list {$args}
KnittingLanguage ( (list k1 p2) * 2)
KnittingLanguage ( (list k2tog yo k1 yo cdd ssk yo k3) * 2 )
KnittingLanguage "k4"
image

Create a variable with the initial number of stitches. In the correct script block,  decrement or increment it. This counts the final number of stitches.

Introducing PowerShell Pages - Script Style HTML Rendering

July 5th, 2008 / Andre de Cavaignac

Early in the web development world, scripting languages such as ASP or PHP were used to compose pages. Although this proved great for relatively static pages, the dynamic web, filled with rich applications called for a more powerful framework. Thus, frameworks like ASP.NET were born.

ASP.NET solved a good number of problem spaces, but has made creating simple pages (such as a resume or menu, or other primitive list of data) more cumbersome. With the world of COM development becoming less common and less preferable, the gap for a scripting language to replace VBScript/ASP is needed. PowerShell scripting has filled the gap left by the demise of VBScript, but nothing has come along to replace ASP.

PowerShell Pages is an ASP like language, based on the PowerShell runtime. Using a simple HTTP Handler, ASP.NET can render pages scripted using PowerShell script (including cmdlets, and CLR/.NET objects) to the web. Simple, fast and intuitive programming for simple pages that just need to display some content.

The PowerShell Pages project is an open source project that I am starting.  Its implementation will be based on ASP.NET using a simple handler capable of consuming PowerShell HTML (PSH) scripts and writing HTML.  Because the script is hosted in ASP.NET, the ASP.NET HttpContext and the other components of the object model are available.  PSH scripts can work side-by-side with ASPX pages.

Ready to see what PowerShell Pages look like?
Sample Page:
http://decav.com/psp/resume.psh
Sample Page Code: http://decav.com/psp/viewsource.psh?page=resume.psh
View Source Code: http://decav.com/psp/viewsource.psh?page=viewsource.psh

Join the Project - Visit the PowerShell Pages CodePlex project workspace

PowerShell Quiz Part II

July 5th, 2008 / Development in a Blink

Part 1 showed different ways to display information in the console. One had unexpected output.

Below are examples of writing an object to the error pipeline. One of these triggers a non-terminating error

Write-Error : A parameter cannot be found that matches parameter name ‘+’.

Write-Error "hello  World"
Write-Error "hello " + "World"
"World" | % {Write-Error "hello $_"}

Tracking this bug took a colleague a while. Complicating the resolution; the Write-Error nested in a Trap. He was expecting an error from an underlying component. This was throwing in the trap code.

.Net 3.0, PowerShell and Scott Hanselman

July 2nd, 2008 / Development in a Blink

Scott posted Back to Basics: var != Dim showing how to use Microsoft Office Interop to change a value in the CustomDocumentProperties of a Word document.

My Approach

Use PowerShell and System.IO.Packaging in Windowbase.dll from .Net 3.0.

Advantages
  • Microsoft Office does not need to be installed
  • Works with Word, Excel and PowerPoint
  • It is not limited to just custom properties 
  • Only a text editor is needed to write the glue

I wrote the PowerShell Package dll which facilitates the transition between PowerShell and the Packaging namespace.

Process

Office 2007 files are zip files. Take an Excel file, rename it to .zip and unzip it. Check out the file system directory structure and contents. It is all Xml. The System.IO.Packaging namespace let’s you work with compressed file contents, package parts, relationships and the data stream.

The DLL and PowerShell scripts abstract this further. The Support.ps1 script contains functions and filters which wrap the C# DLL so the methods play better in PowerShell’s pipeline.

The Update-Properties.ps1 script pipes through the relationships, relationship types, target uri’s and retrieves the document object model. The nested for each statements extract all the custom properties, sets them and finally saves and closes the document.

Summary

This code forms the basis for next steps in modifying the data in Office documents.  Recently I joined the open source project PowerTools for Open XML and found a script written by Dave Glover which sparked this approach.

Dave demonstrated how to generate Office Open Xml documents on the fly by merging a Word Template with an Xml file of Customers into customer invoices.

His script generates 80 documents/second. After making it more PowerShell like I generated 140 documents/second. Refactoring can improve this another 30%.

In subsequent posts I will improve this code and demonstrate other ways to interact with Office Open Xml.

The Code

param($file=".Template.docx")

. .Support

(Open-Package $file).Relationships |
  Where {$_.RelationshipType -match ‘custom-properties’} |
    ForEach {$_.TargetUri} |
      Get-Dom |
        ForEach { $_.Properties.property } |
          ForEach {$_.lpwstr="TestChange"}

Save-Package
Close-Package

Download

The Standalone.zip contains test documents, PowerShell scripts and the compiled DLL. The other zip contains the VS 2008 solution as well.

PowerShell Quiz

July 1st, 2008 / Development in a Blink
1..10 | % {write-host "hello  $_"}
1..10 | % {"hello " + $_}
1..10 | % {write-host "hello " + $_}
1..10 | % {write-host "hello $($_)"}

Three of these print the same result, which one doesn’t?

IBM WebSphere MQ – PowerShell Library

June 30th, 2008 / Development in a Blink

A library which allows for command-line and script-based administration of WebSphere MQ queue managers from the PowerShell.

via All About Interop

Open Xml meet PowerShell

June 18th, 2008 / Development in a Blink

Microsoft announced a fully supported release of the Open XML Format SDK 1.0, read about it here download it here.

Eric White, Technical Evangelist for Open XML, announced an open source project for processing Open Xml documents using PowerShell, PowerTools for Open XML.

I offered my help

I emailed Eric, offered my help and he added me as a developer.

PowerTools for Open XML

Currently there are 33 PowerShell cmdlets. They work with Excel and Word creating and modifying files directly without the Office object model and do not require the installation of Microsoft Office.

I’ve gone through some of the code and scripts, made some changes and reworked the BulkMailing.ps1 example into the ImprovedBulkMailing.ps1 script.

Improved Bulk Mailing

In a nutshell, the script reads an Xml file, dumped from the Northwind customers database, containing customer name, address etc.

The script uses the inline letter and  generates 88 separate Word documents customized to each customer.

Download the updated script here.

Excel

Creating a an Excel spreadsheet of the running processes on your system is as simple as

Get-Process |
  Export-OpenXmlSpreadsheet .ps.xlsx

Creating a spreadsheet and separate chart of the top 10 memory consumers

Get-Process |
 sort -Descending PM |
  select name, PM -First 10 |
   Export-OpenXmlSpreadsheet .ps.xlsx `
    -Chart -ChartType bar `
    -ColumnsToChart PM -HeaderColumn Name

Next Steps

Eric and I have been chatting about several ideas. Improving the underlying PowerShell cmdlets, adding PowerPoint cmdlets, and other ways PowerShell can make short work of leveraging Open Xml.

Check it out.

Marc Adler asks

June 8th, 2008 / Development in a Blink

Microsoft Velocity

Close the Discoverability Gap

June 1st, 2008 / Development in a Blink

James Brundage, from the Microsoft PowerShell Team,  posted a 7 part series on WPF & PowerShell. Aimed at PowerShell, WPF and version 2 capabilities; it delivers.

Along the way James creates other PoSH nuggets. Here are 3 from Part 2, that help close the discoverability gap.

The Discoverability Gap is the difficulty in determining what solutions exist for a problem

  • Get-Type
  • Get-MSDNInfo
  • Show-ClassInfo

Put these in Get-WPFControlInfo.ps1, add two switch parameters (gridView, msdnView) and you’re cruising WPF information locally and on the web.

View WPF control details with Out-GridView

gwci label

image

Searching is across Name and Definition

image

Grouping too.

image

View the MSDN Web Page

gwci tooltip -msdn

image

Download Get-WPFControlInfo