Skip to content

Instantly share code, notes, and snippets.

@liilac
Created November 3, 2020 03:45
Show Gist options
  • Save liilac/01acf65cf7b3788ff49065f8da216c6c to your computer and use it in GitHub Desktop.
Save liilac/01acf65cf7b3788ff49065f8da216c6c to your computer and use it in GitHub Desktop.
PowerShell script to update file hashes in a .mf OVF manifest file, for all .ovf files in the current directory
# Updates the file file hashes in the OVF manifest for all .ovf files in the current directory
#
# Author: Lilac Kapul <[email protected]>
# Last updated: 2020-11-03
# License: MIT
#
# Copyright (c) 2020 Lilac Kapul
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# locate .mf manifest file
$manifestFile = (Get-ChildItem -Filter '*.mf').ToString()
# locate all .ovf files
$ovfFiles = Get-ChildItem -Filter '*.ovf'
# update file hash for each .ovf file
ForEach ($ovfFile in $ovfFiles) {
Write-Output "Updating hash for file $($ovfFile.Name)"
# match corresponding line in $manifestFile
$ovfFileHashRegex = '^SHA[d]+ \(' + $ovfFile.Name + '\) = (\w{40})'
# compute file hash for .ovf file
# SHA1 is used due to limited support for SHA256 in OVF tooling, at present
$ovfFileSHA1 = Get-FileHash -Algorithm SHA1 -LiteralPath $ovfFile.FullName
$ovfFileHash = $ovfFileSHA1.Hash.ToLower()
# compute new .mf line for the .ovf file
$newOvfFileHashLine = "SHA1 ($($ovfFile.Name)) = $($ovfFileHash)"
Write-Output $newOvfFileHashLine
# replace the existing line with the new one
(Get-Content $manifestFile) -replace $ovfFileHashRegex, $newOvfFileHashLine | Set-Content $manifestFile
}
@liilac
Copy link
Author

liilac commented Nov 3, 2020

Searching for and iteratively updating all .ovf files is intended mostly for illustration, as there would usually only be one. The filter can be expanded to include .vmdk files and others if desired.

For rationale for always writing SHA-1 output, see for example VMWare KB 2151537.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment