Thursday, October 10, 2013

Powershell to read the assembly comments field.

After migrating our applications from a Windows Server 2003 environment to a Windows Server 2008 environment we have found that the comments field of our assemblies is no longer visible in Windows Explorer.

After spending some time searching the internet for a simple PowerShell command I gave up and started experimenting in the PowerShell GUI.

This is the end result of my experiments:
((get-itemproperty .\serviceContract.dll ).VersionInfo).Comments

To list all of the custom attributes I used the following:
(get-itemproperty .\serviceContract.dll).VersionInfo | format-list

When things are busy it can can feel awkward to have to open up a command or powershell window, copy the name of the dll I want to check from another server, and then run a command. To make it simpler to check the version info I wrapped a batch file around my script and put it on my desktop so I could just drag and drop a .dll onto the batch file. My batch file looks like this:
@echo off
powershell e:\scripts\getVersionInfo.ps1 -dll '%1'
pause
Dropping a file on the batch file causes the comand window to open and run the batch with the file name as the first parameter and close again. I added the 'pause' to prevent the window from closing until I had the time to see the result.

Dragging a file onto the batch file causes the OS to try to start the batch file up with the working directory of the file being dropped. When the file comes from another server this will cause an error message. The version info appears too but it didn't feel like a tool I'd want to share.

To avoid the error message I created a shortcut to my batch file and configured the short cut's working directory property to start up in my script folder. Then I put the batch file and the powershell file into my script directory so I just have the shortcut on my desktop.

This dialog shows how to configure the shortcut.



It is possible to change the size of the font and window that pop up using the font and layout tabs.

To make it easier to spot my shortcut on my desktop, I clicked on the Change Icon button and chose an icon from the selection that appeared.



Now I just drag a file from windows explorer onto the info icon on my desktop and presto I have the comments data from my assembly.

I slightly modified my powershell script to accept the assembly name as a command line parameter when I added the batch script abstraction. My final powershell script looks like this:
param([string]$dll)
$dll + " version:"
((get-itemproperty "$dll").VersionInfo).Comments


The first line defines the command line parameter. The second line will just print out the name of the dll and the last line prints out the value in the comments field.
The final result is a lot simpler than all of the C# code I surfed past while looking for this solution, codewise and maintainablity wise.

No comments:

Post a Comment