Posts Tagged PowerShell

PowerShell logs even caught errors

An interesting PowerShell feature is that all errors are collected in an array called $error.

so for example if you throw an error you can echo $error and find it later:

image

However the error is also logged even if you catch it. Consider the following code snippet, which I’ve called errorcollectiontest.ps1

image

When running the script it outputs nothing, but when we check the errors array we find something interesting:

image

What does this matter?

Well if you have a system running PowerShell a convenient way to see if there were errors in the execution of a script is to check the $error array for any errors. However this array will also contain errors that were caught during the execution, which will introduce false-positives.

The PowerShell plugin for Hudson/Jenkins does exactly this, so if you want to use a PowerShell script that catches errors in Hudson/Jenkins the way to prevent false positive build results is to make the following change to the script:

image

This will remove the latest occurred error from the list, which just happens to be the error just caused since we entered the catch block.

I’d like to mention Johan Andersson, in this post, since without him it wouldn’t be written.

,

No Comments

Listing executed actions – A PowerShell surprise

I was hacking around in PowerShell the other day, when I managed to press a few buttons on my keyboard. To my surprise there were some GUI implemented into PowerShell. I did not know this, so here’s a post about this GUI awesomeness (warning for extremely beautiful interface designs).

Executing commands

Pressing F7 brings up this fantastic piece of UI: It allows you to scroll through the commands you’ve executed and selecting it visually from a list.

image

F9 brings up this:

image

This allows you to execute a command based on the number it has in the execution list.

using the two in combination by first pressing F7 followed by F9:

image

This allows you to see which command you want to execute before writing the actual number.

Modifying commands

F2 brings up this prompt:

image

It allows you to copy the currently typed command from the cursors current position up to a specific character.

F4 brings up this prompt:

image

This allows you to delete characters on the command line from the cursors position to a specific character.

Closing

All in all their not *that* amazing, but they were a pleasant surprise to see something new in the prompt.

,

No Comments

Creating ZIP files using Powershell

I’ve been working with a RemoteX version of the Continuous Delivery pipeline that is available at github. At RemoteX we have all our deployment done using PowerShell so to maintain as much familiarity as possible between builds and deploys were using the PowerShell plugin for Hudson (Jenkins).

The set up that we are working towards is this. The CI builds creates output. There is a packaging build that takes the CI output and creates a deployable package and that starts the rest of the pipeline. You can see the structure as it is outlined in the github project.

Now from the package I want a zip file as the delivery. It’s suiting to have an archive as an artifact that is tested and added on in the later steps of the pipeline.

The trouble is that PowerShell has no simple available command-line functions for creating and managing zip files. Not out of the box. In fact any archiving would be appreciated.

These are the solutions I looked at for working with zip-archives using PowerShell:

  • David Aiken did an article on how to compress zip files in PowerShell using COM interop.
    • This didn’t work out for. The size and amount of files in our archive was simply to much, a bunch of popup started to popup with read/write access issues to the zip-archive.
  • Another example I found was using .Net and ICSharpCode.SharpZipLib to create zip files.
    • This solution caused a lot of problems when I tried to dot-source the PowerShell files. The path to the dll screwed up and I didn’t want to introduce any hardcoded paths.
  • Another solution is to use the Community PowerShell Extensions. These extensions have a lot of options for common tasks, one of them being the handling of zip-files.
    • This wasn’t an option to us since it would require the entire extension suite to be checked in in source-control. I didn’t want to have our delivery pipeline reference an entire collection of functions for just working with zip files.
  • Last solution was to introduce 7zip’s command line program for managing zip-files. I tried GZip first but there were issues with file system access on Windows 7.
    • To us this was a perfect fit. It turned zip-handling into one line commands and there were not issues with dot-sourcing in PowerShell.
      • I checked it in under hudson/bin to keep a collection of tools that we use with our pipeline.

          

, ,

No Comments

Powershell principles

We have a document checked-in in the root of our source-control system for Applications. The document is the development principles we strive for. Most of them are basic, SOLID principles with highlights in how our code works.

Currently we’re focusing on automating the installation process for applications, the goal is to be able to handle a lot more installations without increasing the amount of people needed to handle our operations department. We’re removing a lot of manual error-prone steps, and were doing it using PowerShell.

Now my colleague is doing a lot of PowerShell work, and research, to automate different parts of the installation process. We identified that we needed to have a document to make sure that the PowerShell scripts follow guidelines just like with the rest of the code.

Now Johan has been doing a lot of work on this and we’re trying to get a lot of feedback on the guidelines. To increase the feedback we can get Johan has published the PowerShell guidelines on his blog. Please, have a read and any comment is appreciated.

,

No Comments

Updating ClickOnce deployment manifest with PowerShell

We are automating some of our deployment using powershell. As such we use the command line tool mage.exe for signing our deployment manifests, in effect were creating them using mage.exe as well.

We needed to add the trustUrlParamter setting to one of our manifests. Using mageui.exe you just check the box for allowing the application to see the query string, but with mage.exe this isn’t possible.

What you need to do is modify the XML of the application.deployment manifest, adding the parameter as an attribute to the deployment tag.

<deployment install="true" >

Should become

<deployment install="true" trustURLParameters="true">

As I said, we’ve already automated the process of creating and signing deployment manifests using powershell, now we just need to add the element attribute before signing the manifest. Powershell has some nifty features for updating XML, part 3 of  Windows PowerShell in Action: Working With Text and Files in Windows PowerShell (Part 3) demonstrates this.

So just before invoking mage.exe to sign the manifest we add this litle piece of powershell code:

    $xml = [xml]( Get-Content $deploymentManifestPath )
    $trustUrl = $xml.CreateAttribute("trustURLParameters")
    $trustUrl.psbase.Value = "true"
    $null = $xml.assembly.deployment.SetAttributeNode($trustUrl)

Voila, the manifest now has the trustURLParameters set.

, , , ,

1 Comment