Skip to content

Instantly share code, notes, and snippets.

@peaeater
Created May 27, 2022 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peaeater/95477bbad96b3a0868bfc518ea6efe2a to your computer and use it in GitHub Desktop.
Save peaeater/95477bbad96b3a0868bfc518ea6efe2a to your computer and use it in GitHub Desktop.
<#
Peter Tyrrell, Andornot
Replaces old sql server name with new in textbase binary *.cbs file.
Note: Uses PowerShell Core syntax. See comments for Windows PowerShell.
Note: Server names of different lengths corrupt the file.
#>
param (
[string]$file = "D:\temp\ampas\backups\AAtrans.cbs",
[string]$find = "SERVER80",
[string]$replace = "P3SQL264"
)
$hexFind = ($find | Format-Hex).HexBytes
$hexReplace = ($replace | Format-Hex).HexBytes
# In *Windows PowerShell*, replace `-AsByteStream` with `-Encoding Byte`
$byteArray = get-content $file -Raw -AsByteStream
$byteString = $byteArray.ForEach('ToString', 'X') -join ' '
echo "Replacing `'$find`' with `'$replace`' in $file"
$byteString = $byteString -replace $('\b' + $hexFind + '\b(.*)'), $($hexReplace + '$1')
# In *Windows PowerShell* use `-Encoding Byte` instead of `-AsByteStream`.
[byte[]] $newByteArray = -split $byteString -replace '^', '0x'
set-content $file -AsByteStream -Value $newByteArray
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment