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 032013
 

If you are a user of the new “Windows to Go” feature of Windows 8 Enterprise edition, which enables you to boot and run the Windows 8 operating system off an external USB device, you may have noticed something odd when trying to use the Windows App Store:

no-go

In a nutshell: “Windows Store isn’t available on Windows To Go workspaces”.

How to fix?

This seemed odd to me, I can’t think of any technical reason why this might be the case, especially given Windows to Go still has the full formerly-known-as-Metro user interface.  Then I stumbled across this blog article on the MSDN blogs site which pondered the same question.

It appears that the Windows Store is locked down by Group Policy for unknown reasons, I suspect due to paranoia (key term here is ‘Enterprise’, after all).

I don’t want to steal the thunder of the original article, but for brevity – the steps to unlocking require a change to the system’s group policy.  Here’s a paraphrasing of the steps outlined in the original MSDN blog article:

  1. Open the Group Policy Editor (gpedit.msc) – for local policy
  2. Go to Computer Configuration/Administrative templates\Windows Components\Store. Go to the “Allow Store to install apps on Windows To Go workspaces” policy.
  3. Enable & Apply

Sometimes you might want to make this apply right away, to do so open a Command Prompt and gpupdate /force

This worked for me Smile

Note that this applies to the local Group Policy and may be superseded by a domain-level Group Policy.

Still no joy?

If you still have issues, perhaps you may need to make a registry change, first noted here.  This may have unintended consequences but I can’t think of any off the top of my head, so why not give it a try (worst case, you can restore the registry value to the default):

  1. Open the Registry Editor (regedit), you should be prompted for permission elevation (under UAC)
  2. Navigate/expand to the following path: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control
  3. Find the following DWORD value: PortableOperatingSystem
  4. Change the value from 1 to 0 and reboot the system

 

If you’ve made these changes, of course you will be able to download the Sanders Technology Digest application – here’s a handy link..  Or why not try the Windows Store directly?

/R