Simon de Kraa
Simon de Kraa

Reputation: 435

Auto update PowerShell script from Windows Azure Blob Storage

I would like to automatically update a PowerShell script from Windows Azure Blob Storage if a newer version is available.

Pseudo code:

if <update available> {
  <download update>
  <restart Powershell script>
}

I did find the ContentMD5 property where the value is for example "r3zP0ehIwNxGVIMcB8AcCg==".

$StorageAccountName = "myAccount"
$StorageAccountKey = "myKey"

$context = New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
$list = Get-AzureStorageBlob -Container deployment -Blob myScript.ps1 -Context $context
$list.ICloudBlob.Properties.ContentMD5

Two questions.

1. The ContentMD5 is calculated for a ZIP file but is empty for a PS1 file. Any idea why this is happening?

2. How do I calculate the MD5 value myself?

I did find some code to calculate the MD5 from a local file. But the value is in a completely different format like for example "FA-0D-F6-22-7A-BF-AE-87-B9-F6-88-0C-AE-5B-75-E9".

$md5 = new-object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
[System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($myScript)))

Upvotes: 2

Views: 1186

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136256

Instead of making use of ContentMD5 property, may I suggest you use ETag property of the blob for checking if the blob has changed. ContentMD5 property is a user-defined property and a user can update it anytime even if the blob was not updated. ETag property on the other hand is updated by the system every time a blob is updated.

Update

Regarding why the MD5 is not matching, basically you're converting the hash byte array to string. You would need to convert it into Base64 encoded string:

$md5 = new-object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
[System.Convert]::ToBase64String($md5.ComputeHash([System.IO.File]::ReadAllBytes($myScript)))

That should do the trick.

Upvotes: 2

Related Questions