Git and the Powershell ISE

This is one of those seemingly random posts. However, if you use the Git command line tools in the Windows Powershell ISE then this post is for you!

The Windows Git client expects the standard windows console host (conhost.exe) to be used and outputs “stuff” to the stderr stream – even when there is no error. Here’s an example of Git output to the error stream in cmd.exe:

Git in the Windows command prompt

And again in powershell.exe:

Git.exe in the Windows Powershell host

Notice that neither complains of any error. Now – what happens in the Powershell ISE console?

Git.exe in the Windows Powershell ISE host

That’s right folks, git.exe sends that we have successfully switched to the ‘test’ branch to the error stream. Luckily for us the Powershell ISE renders this nicely for us. Not.

PhatGit

This is where the PhatGit module helps out. PhatGit redirects output from the error stream to standard console output. This is rendered by the Powershell ISE console host and can be captured/manipulated etc. If an actual error is encountered then this is still sent to the error stream.

Clean Git.exe output in the Powershell ISE host

In the above screenshot the PhatGit module has been loaded and you can see the first Git command looks like we’d expect and is consistent with the output in the conhost.exe processes from cmd.exe and powershell.exe.

The second command demonstrates that errors are still bubbled up as you would normally expect.

Note: Just in case you’re wondering, “fat Git” is a British English non-complimentary phrase :).

Text Editors

It gets even more complicated – you knew that was coming right?!

Git also relies on external text editors when interactive actions such as merges and commits are performed. Unfortunately, console text editors such as VIM (the default) also rely on the conhost.exe process. If these editors get invoked, then the Powershell ISE console process may become unresponsive – if you don’t believe me try running the following command without PhatGit:

PhatGit without Git commit message

As you can see PhatGit traps known problematic Git commands and stops them hanging the console. The list of known commands easily can be extended but that will have to be another blog post.

Open Source

PhatGit is an open source project and you can find the project over on GitHub. If you would like contribute then get involved! Pull requests are gratefully received and if you find a bug or have any requests, log an issue.

Let’s Get Chocolatey

It’s taken a little while, but we are pleased to announce that the Virtual Engine Toolkit (VET) and the App-V Configuration Editor (ACE) are now available on Chocolatey! If you have Chocolatey installed on your systems then you can get going by running the commands listed below.

image

Microsoft Package Management

It gets even better if you’re running the Windows 10 Insider preview, the latest WMF 5.0 April 2015 preview or have installed the latest experimental OneGet build on Windows 7 and up, you can download these directly – today!

To do this, run the Install-Package ace or Install-Package vet commands, like so:

image
Other Packages

It doesn’t end here though. We have also published the following packages that you might find useful to the public Chocolatey feed:

Now – go get Chocolatey 😀

Testing Private Functions with Pester

We’ve been busy beavering away on a new Powershell module that is comprised of many .ps1 files, that are loaded by a master/control .psm1 file when the module is imported. And, like all good Powershell citizens, we have many Pester unit tests for this code in accompanying .Tests.ps1 files.

As Powershell script modules permit us to control which functions should be exported with Export-ModuleMember and Pester allows us to test non-exported functions with the InModuleScope option, you might be wondering why we would ever need to be able to test private/internal functions?

Script Bundles

Whilst coding the new Powershell module, we have always had the desire to ensure that it could also be used as a ‘bundled’ .ps1 file. By bundle, we mean a single combined .ps1 file that can be included verbatim at the beginning of an existing script or by dot-sourcing it as required. Unfortunately – in this particular scenario – the internal module functions would be exposed and could potentially cause unnecessary confusion.

Here’s an example where both the ‘PublicFunction’ and ‘InnerPrivate’ functions would be exposed when bundled or dot-sourced into an existing .ps1 file.

If we only want the ‘PublicFunction’ visible then the simple solution to this is to nest the private function(s) inside the public function(s) like so:

Now only the ‘PublicFunction’ will be available. End of the story?

Internal Functions

Not quite; by hiding the functions we now cannot test them with Pester. I had a very brief conversation with Dave Wyatt on GitHub about this and it was agreed that this functionality should not be a part of the official Pester release.

To solve this particular issue, we have a simple function that will locate and return a function’s definition from within a .ps1 file as a script block. The function definition can then be dot-sourced into the current Pester scope to enable testing. Here’s an example Pester test file that will test our ‘InnerPrivate’ function:

If this code is of interest you can simply save the following code as a .ps1 file in Pester’s \Function directory, for example \Functions\FunctionDefinition.ps1, and Pester will automatically load it.

Notes:

  1. As this code utilises the Abstract Syntax Tree (AST) it does require Powershell 3.0;
  2. If there are any dependencies on variables in the function’s parent scope these will need to be mocked/accounted for;
  3. Depending on how you install/update the Pester module, this might get overwritten when Pester is updated.

When we have more time we’ll put this up on Github so people can collaborate on changes. In the meantime, here’s a copy of the function.

App-V 5 Configuration Editor (ACE) v1.3 Released!

App-V 5 Configuration Editor (ACE) v1.3 Released!

Virtual Engine are pleased to announce the version 1.3 release of the App-V 5 Configuration Editor (ACE). This (free) utility provides a simple user interface for editing App-V 5 machine or user dynamic configuration files, without manually hacking the underlying XML files.

This release contains several reported bug fixes along with:

  • Many more Global Options added;
  • Enhancements to Terminate Child Processes;
  • Ability to open dynamic configuration files from the Command Line to allow integration into 3rd party products;
  • Source XML and Generated XML tabs now displayed with collapsible/expandable formatting.

App-V 5 Sequencer Template – Full VFS Write Mode

With the recent release of Hotfix 4 for App-V 5.0, Microsoft has now provided the ability to “Allow virtual applications full write permissions to the virtual file system”. This setting can be found in the sequencer under the “Advanced” tab as demonstrated in the screen shot below:

 Sequncer Full VFS

Should you wish to enable this setting as a default whenever you create a new package, then simply go ahead and add <FullVFSWriteMode>true<FullVFSWriteMode> into your sequencer template (.appvt), as you can see below. This setting is only valid where you have the App-V 5.0 SP2 Hotfix 4 sequencer installed.

And if you’re not currently using a template then I’d highly recommend you do. You’ll find a great article over at Rory Monaghan’s blog that explains the use of the sequencer template in more detail.

Nathan

Creating .CAB files with Powershell

During our on-going development of online updateable Powershell help (more on that later) you quickly come to realise that each culture-specific set of help files is stored within its own 1980’s style cabinet (or .cab) file. When packing the .cab files for release, I like to automate (if you hadn’t already guessed!) as it makes things quick, easy and certainly less error prone.

Working with .cab files in Powershell requires use of MAKECAB.EXE which, fortunately, is distributed with each edition of Windows. In the good ol’ days we could use the MakeCab.MakeCab.1 COM object, but this has been deprecated since Windows Vista. I had a quick Google and couldn’t find anything easily reusable (except Ed Wilson’s post here) and thus this blog post.

I set to work, creating a Powershell advanced function that would easily allow me to package each culture-specific help file (or one or more files) into its own cabinet file. It would probably be more prudent to utilise .NET’s StringBuilder class but performance is not (currently) an issue or priority!

You never know I might come back and show you how we use this with Psake in the future… Here are some examples of how you can use it:

Here’s the full advanced function:

 

Searching for String Properties with Powershell

I had a requirement recently to parse a configuration file (let’s just say for documentation purposes) and I needed to retrieve a property/value pair which may or may not be present in a text line. Now, depending on the product we wish to document, we might have a line in the configuration file constructed as follows:

Now, if wanted the “-intfType” value we could split the string using the Powershell .Split() method like so:

However, there are two issues here:

  1. What happens if there are additional or missing properties (the index/order will change)?
  2. We were hoping/expecting to see “Xen Virtual” output and not just “Xen.

Here’s a quick function that will solve issue #1 (complete with case insensitivity):

Solving issue #2 is a little more involved as we need find the quoted text, escape it and then restore it when needed. The following Get-StringProperty advanced function will first replace all quoted spaces with “^” and then replace the quotes themselves with “^^”. This permits the split function to work as expected. Finally, everything is put back together just before we need it!

That’s much better Smile. Just for good luck I have also created a complimentary Test-StringProperty advanced function that tests whether a property name is present or not. This removes a lot of if ((Get-StringPropertyValue $SearchString “PropertyName”) -ne $null) { Do-Something } calls.

Some final words of warning:

  • If there are escaped (double) quotes then this function won’t work without some modification.
  • If the $SearchString just so happens to natively contain either “^” and/or “^^” it won’t (currently) work either.
  • If you have any improvements or feedback then please let me know.
  • Please test in your environment before putting any 3rd party or external code into production!

RES IT Store Web Portal Setup

We’ve had some confusion both internally and externally with some customers with the installation of the RES IT Store Web Portal. During the installation you will be prompted as to how you would like the IIS website to be configured:

image

This dialog is a little bit unclear as to what it is actually asking you. If you follow these simple guidelines, I’m sure you’ll be a whole lot clearer as to what is going on:

  • The RES IT Store Web Portal setup will create a new website – no ifs, buts or maybes.
    • It will not install to the default IIS website.
  • If you select the “Yes” option, the new website will be bound to port TCP port 80:
    • The existing default IIS website will also be bound to port 80 and therefore only one will start.
    • You will need to manually change the IIS default website port bindings.
  • If you select the “No” option, the new website can be bound to a specific TCP port:
    • Be careful as it will default to TCP port 80 but you can manually alter it.
    • The wizard will bind the Hostname specified to the new website as an IIS Host Header Name.

Our recommendation is to always select “No”, leave the default port binding and configure the host header value with your load-balanced fully qualified DNS alias, i.e. itstore.virtualengine.co.uk.

Easy!

Windows 8.1 Update KB 2919355 Error 0x8007003

It’s been long, too long since the last post. However, never fear as we have plenty of new updates in the pipeline (pun intended!). This post is a little off topic but one that might help one or two people with the latest Windows 8.1 Update 1.

Whilst installing the latest Windows 8.1 Update 1 (and also KB2894853) on a couple of machines I saw the following error:

Installation Failure: Windows failed to install the following update with error 0x8007003: Update for Windows (KB2919355)

A quick Google landed me here which mentions that you will receive this error if the profiles directory is redirected with a registry key. As we have small SSDs in the office machines, the profile directory is indeed redirected to a bigger, spinning disk. This article is a little confusing (hence the post) as it states:

After putting a copy of UserProfiles on the C: drive, installation finished normally.

To fix the installation error we need to ensure that the Default directory is present in the redirected folder location, not on the C: drive. A quick copy of C:\Users\Default to D:\Users\Default fixed the installation issues with both updates.

12312

Archives

Categories