Rob Sanders

IT Professional with over 16 years experience, 13 in commercial software development and 8 years in IT consulting. Check out the "About Rob" page for more information.

May 162013
 

PowerGUI_Badge_SeeYouAtFollowing on from the last instalment where I looked at supporting PowerShell script development in Visual Studio 2010, I’m going to take a closer look at PowerShell integration with the newer Visual Studio 2012.

Again, I’ll be leveraging the excellent Quest PowerGUI product and the associated Visual Studio extension.

Installation

You’ll also need PowerGUI 3.2, but this time there’s an updated Visual Studio extension which you’ll need, version 1.6.1

Install PowerGUI 3.2 first, and once complete then install the Visual Studio extension.  PowerGUI has a dependency on the .NET Framework 3.5, on newer OSes (like Windows 8) you might not have this pre-installed, don’t worry – the installer will warn you if anything’s missing.

Getting Started

Once you’ve installed, you will see a new option in Visual Studio’s File –> New Project dialog for PowerShell.  This creates a fairly sparse new project with a .ps1 file in it ready to go.

Troubleshooting

The first two times I tried to get everything installed and running, I kept getting issues with Visual Studio 2012 warning it couldn’t load PowerGUI.  I tried everything until I realised what the problem was.  I’d downloaded the .vsix but one thing finally dawned on me:

image
Unblock

Therefore, unblocking the file before installing seemed to do the trick.  If you’re having problems loading the extension this might be a problem for you as well.

Looking at the Project

As with Visual Studio 2010, you get the PowerShell project type available in the New Project dialog:

image
New Project

A closer look

Some of the advantages of using Visual Studio to write your PowerShell scripts are easy to highlight.  The debugging support is obviously a huge advantage, but so to is the Intellisense support. 

It’s really evident when there’s an issue with the script, if you aren’t seeing the right info in Intellisense.  Rather than go into detail, they say a picture is worth a thousand words, so let’s see some examples of the value in writing your PowerShell scripts in Visual Studio:

image
Debugging Support

image
IntelliSense Support

image
PowerGUI integrated Console

image
Best of all – Tooltips which provide CmdLet syntax

What’s Next?

Now that we’ve explored both Visual Studio 2010 and Visual Studio 2012 options for PowerShell, the next step is to take a look at some of the advantages of using either environment when building your PowerShell scripts.  We’ll also look at trace/output options and how to best handle errors.

Check back for the next article..soon.

May 152013
 

PowerGUI_Badge_MyPSEditor-Pro1Recently I needed to write up a new Powershell script to automate some actions independently of our major release cycle.

This took me down a road with two possible options – write the script in Visual Studio 2012 or in Visual Studio 2010.  I’ve decided to pursue both avenues, just for kicks.  There are other options available, but I’m more comfortable with Visual Studio, and it integrates into our standard SDLC tools (Team Foundation Server), so I can associate the work to work items, e.t.c.

This article will cover writing PowerShell scripts in Visual Studio 2010 using PowerGUI and the PowerGUI VSX (Visual Studio Extension).  I’ll attempt a follow up showing how to accomplish the same outcome with Visual Studio 2012 later.

Be kind.. This is my first foray into the world of PowerShell scripting..  Just posting a few observations.

Installing Software For Visual Studio 2010

The first thing you need is Quest PowerGUI (my chosen PowerShell add-in) version 3.2 – note that this is not the latest version, 3.6!.  Unfortunately, the Visual Studio extension only supports version 3.2.  This required a little bit of Google-Fu, but I found a copy here on the PowerGUI forum.

You’ll also need PowerShell 2.0 installed (if not already installed), you can install from the Microsoft site.

Once installed, use Visual Studio’s Extension Manager (under Tools) to search for (in the online templates) the PowerGUI VSX extension and install.  Once done, you’ll need to restart your instance of Visual Studio.

vs-2010-extension

Once restarted, when you look under the File->New Project, you’ll see a new option for Powershell (note I have two VS extensions installed):

image

Once installed you can create a new project and we can start our scripting.

PowerShell

Well, I won’t go into too much detail here.  Once everything’s installed, you can create a new PowerShell project, which really just creates a project and allows you to add new PowerShell script files.

The main advantage here is the debugging and IntelliSense support, which really helps let you know if things are being written properly.  I did the majority of scripting within Visual Studio, although PowerGUI has a separate script application which isn’t bad.

PowerShell Challenge No.1

OK, so here’s what I was attempting to do.  From a given directory, iterate through XML files and read out the contents of each file.  For each file, the aim was to hit a WCF (SOAP) web service and store the XML data from the files.

Pretty basic stuff?  Agreed.  So I decided to tackle the trickiest part first.  Using Visual Studio was a bit of a help, because the IntelliSense gave me a good hint when the script was written properly.

Invoking a WCF Web Service from PowerShell

There’s actually an extremely straightforward way of doing this provided that the service is using a HTTP endpoint.  There’s a cmdlet in PowerShell 2.0 called New-WebServiceProxy which simplifies the calls to child’s play.

Let’s say I had a Web Service called FormManagement which had an operation called Save.  The Save operation takes two parameters, a string param called “AppNumber” and a string for the form data.  Assuming I had this service configured with basicHttpBinding and no authentication, to invoke the Save() operation from PowerShell I’d just do this:

$uri   = “http://tempuri.org/Forms/FormManagement.svc
$proxy = New-WebServiceProxy -uri $uri

Write-Output $appNumber
$proxy.Save($appNumber, $formData)

For all other cases – particularly when using a net.Tcp endpoint – (and potentially if you have dramas with wsHttpBindings) you’ll have to deal with programmatically creating at runtime a client proxy assembly to get the type goodness.  Luckily, someone else has a very elegant solution which is worth a look.

The Rest

Is Child’s play.  In fact, I wrote the logic into functions just because I can.  Essentially, I’m using a global variable ($directory) which is referenced by the Process-Folder function.  The rest is really too trivial to go into detail.

function global:Process-Folder()
{
    $files = Get-ChildItem $directory -Filter *.xml -Name
    foreach ($file in $files)
    {
        $processing = “Processing file: ” + $file
        Write-Output $processing
        Process-File $file
    }
}

The Process-File function takes a filename parameter and takes care of prepping the input data for the Web Service call in the previous section.

function global:Process-File
(
        [CmdletBinding()]
        [Parameter(Position=1, Mandatory=$true)][string] $fileName)
{
    $appNumber = $fileName.ToUpper().Replace(“.XML”, “”)
    $fqn = $directory + $fileName
    $formData = Get-Content $fqn #TODO: flatten the string array into a single string
   
    #Invoke the WCF service
}

It’s almost elegant.  Not bad for a first script?  I’ll be doing a second article on how to work with PowerShell in Visual Studio 2012.

May 092013
 

If you’ve ever had any involvement with an Agile project (whether it was “pure” Agile or not), you’ll likely have encountered the beast which is effort forecasting and analysis.  This drives the initial estimate of the amount of work which your team thinks it can deliver within a given period.

Agile sprint
Example of a scrum style sprint
[source]

It doesn’t really matter how big your project is, sizing up the amount of work which can be produced is a time honoured tradition, but how do you know if you’re even in the ballpark of getting your estimates right?

Over at ThoughtWorks Studios, Martin Fowler (and others) have spent significant time and effort in trying to document some conclusions about this very topic and a PDF white paper can be found on the ThoughtWords Studios website.

It’s hardly a light read – at 32 pages – but can you really afford to take estimation lightly?  In a world of commercial agreements, balancing customer or client expectations and attempting to meet tight delivery timelines, getting your estimations accurate is a key step in delivery.

However, in my mind going into this document, it really helps to have a decent view of what it is you are trying to build.  The more uncertainty going into any kind of sizing or storyboarding exercise, the rougher the estimation or analysis is going to be. 

There’s no silver bullet, one-size-fits-all methodology at play here, however this document is a really good read if you are looking to canvass different views and opinions about how to set expectations around Agile delivery.  Be prepared to have your designs challenged, and to field changes as they can (and do) present themselves!

If you aren’t quite ready to dip into the minefield which is Agile planning and forecasting, perhaps you’d find value in another e-book from ThoughtWorks – “How do you develop a shared understanding on an Agile project?”.  Remember, for an Agile project to succeed, everyone needs to play their part – the methodology isn’t just for programmers!

For those who haven’t already gone to visit the ThoughtWorks website, here’s a direct link to the PDF.

Further reading: