.Net 3.0, PowerShell and Scott Hanselman   -   July 2nd, 2008

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

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?

| show comments »

IBM WebSphere MQ – PowerShell Library   -   June 30th, 2008

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

via All About Interop

Microsoft Cloud Computing   -   June 26th, 2008

Interesting data points Seeding the Cloud.

  • Microsoft Quincy-class San Antonio location
    • 475,000 square feet
    • Covers 11 acres
    • 1.3 miles to circumnavigate
  • Adding more than 10,000 servers a month to their worldwide infrastructure
  • R&D team completely focused on data center design and have 40 projects under way
  • Almost every Microsoft product will have a services component
  • Possibility of utility computing services
    • That’s a lot of Windows Servers, SQL Servers …

image

Silverlight - Parts & States Model with VisualStateManager   -   June 23rd, 2008

Karen Corby is doing a multi-part write up on managing states and transitions inside of controls.

Open Xml meet PowerShell   -   June 18th, 2008

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.

| show comment »

Roadrunner faster than a petaflop   -   June 15th, 2008

A complex physics calculation that will take Roadrunner one week to complete, would have taken the 1998 machine 20 years to finish – it would be half done today!

via Breaking the Petaflops Barrier

IBM Fact Sheet

Silverlight 2 Beta 2 TabControl   -   June 10th, 2008

After asking the oracle of infinite knowledge (Google) for examples on Silverlight Beta TabControl, I came up with nothing.

Attempting to Close the Discoverability Gap I changed my search to WPF TabControl. Starting with this example I came up with this working Silverlight TabControl.

image 

Xaml

  <c:TabControl TabStripPlacement="Left">
   <c:TabItem >
    <c:TabItem.Header>
     <StackPanel Orientation="Horizontal">
      <Ellipse Width="10" Height="10" Fill="Green"/>
      <TextBlock Text="Toolbox" Margin="5"/>
     </StackPanel>
    </c:TabItem.Header>
   </c:TabItem>
   <c:TabItem >
    <c:TabItem.Header>
     <StackPanel Orientation="Horizontal">
      <Ellipse Width="10" Height="10" Fill="DarkGray"/>
      <TextBlock Text="Server" Margin="5"/>
     </StackPanel>
    </c:TabItem.Header>
   </c:TabItem>
   <c:TabItem>
    <c:TabItem.Header>
     <StackPanel Orientation="Horizontal">
      <Ellipse Width="10" Height="10" Fill="DarkGray"/>
      <TextBlock Text="General" Margin="5"/>
     </StackPanel>
    </c:TabItem.Header>
   </c:TabItem>
  </c:TabControl>
 </Grid>
| show comments »

More Silverlight 2 VSM Tutorials   -   June 9th, 2008

image

Over on Nibbles Tutorials.

18 minutes with an Agile mind   -   June 9th, 2008

image

 

 

Enjoy the ride.

Developed by DataDesignIT