Skip to content

Instantly share code, notes, and snippets.

@djonasdev
Created December 12, 2022 10:19
Show Gist options
  • Save djonasdev/c38c4585ec338740e0da8bec319b72c9 to your computer and use it in GitHub Desktop.
Save djonasdev/c38c4585ec338740e0da8bec319b72c9 to your computer and use it in GitHub Desktop.
Easily replace all GUID in a WIX-Toolset-Installer file | It is also usable on any other file
## GuidSwap.ps1
##
## Reads a file, finds any GUIDs in the file, and swaps them for a NewGUID
##
$filename = "HMI.wxs"
$outputFilename = "HMI_newGuids.wxs"
$text = [string]::join([environment]::newline, (get-content -path $filename))
$sbNew = new-object system.text.stringBuilder
$pattern = "[a-fA-F0-9]{8}-([a-fA-F0-9]{4}-){3}[a-fA-F0-9]{12}"
$lastStart = 0
$null = ([regex]::matches($text, $pattern) | %{
$sbNew.Append($text.Substring($lastStart, $_.Index - $lastStart))
$guid = [system.guid]::newguid().ToString().ToUpper()
$sbNew.Append($guid)
$lastStart = $_.Index + $_.Length
})
$null = $sbNew.Append($text.Substring($lastStart))
$sbNew.ToString() | out-file -encoding ASCII $outputFilename
Write-Output "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment