For years Windows had inferior scripting capabilities compared to Linux and other OSes. I don’t mean to belittle the efforts of the teams that brought us VBScript and CScript, but the languages and the library support for them were simply not good enough for most of the people.
What every Windows administrator and power user needed was a more powerful language and even more importantly, better library support for common administration operation and, if possible, general library support for any task at hand. We finally have it – great language and even better library support – in the shape of Powershell.
Powershell is a “console on steroids”. Where before you had weak and strange constructs and only a text input/output model, today you have .NET-like scripting language, object input/output model (the pipe symbol still works but you can send .NET objects across) and the full .NET Framework at your disposal!
This is great, great news. To celebrate the occasion, I bring you a third version of the signature verifier tool. I first made a C# version (compiled to EXE), then a Ruby one (interpreted) and finally here is a Powershell version. Just a reminder – this tool takes a SHA1 hash you got from the MSDN subscriber site (for a given download), compares it with the downloaded bits and verifies if the hashes match. If they do, your download is OK, if not, some of the bits got corrupted during download so you should download it again. Here’s the script:
param ([string]$path = $(read-host "Please specify path to the file to verify"),
[string]$hash = $(read-host "Please specify the expected hash")
)
$f = New-Object -typeName System.IO.FileStream $path, Open, Read
$s = New-Object -typeName System.Security.Cryptography.SHA1Managed
$bh = $s.ComputeHash($f)
$f.Close()
$sh = ""
foreach ($b in $bh) { $sh += ("{0:x02}" -f $b) }
if($sh -eq $hash.ToLower())
{ Write-Host -foregroundcolor Green "The file is valid" }
else
{ Write-Host -foregroundcolor Red "The file is NOT valid" }
Note how it’s really easy to create any .NET object with a New-Object – I am creating FileStream and SHA1 this way. Also note how easy it is to change a console foreground color to emphasize output (red for no match, green for match). The only line that might be confusing is the part where string formatting takes place. Expression ("{0:x02}" -f $b) is more or less equivalent to string.Format($b, ("{0:x02}"), the only difference is that Powershell has -f operator for string formatting.
In order to test the script, copy the source above into a plain text file, save it as Verify-Signature.ps1 and put it somewhere on the path. Don’t forget to set the execution policy to RemoteSigned (or alternatively, sign the script). On the screenshot to the right you can see the script in action, validating Office 2007 ISO image. Note how if you don’t name the parameters, everything works, just as if you don’t provide them at all – the Powershell will prompt you for each of them.
Be the first to rate this post
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5