Skip to content

Instantly share code, notes, and snippets.

@janisBerz
Created January 13, 2022 15:16
Show Gist options
  • Save janisBerz/4e9e98d728e546b8d3dfa908abf1ca71 to your computer and use it in GitHub Desktop.
Save janisBerz/4e9e98d728e546b8d3dfa908abf1ca71 to your computer and use it in GitHub Desktop.
Terraform State Azure Resource ID Replace
$storageAccountName = "xxx"
$blobName = "xxx"
$containerName = "xxx"
$destination = "$env:TMP\blob.json"
$stringToReplace = "xxx"
$stringToReplaceWith = "xxx"
try {
$ctx = New-AzStorageContext -StorageAccountName $storageAccountName
Write-Host -ForegroundColor Green "Downloading blob and creating snapshot."
$blob = Get-AzStorageBlob -Blob $blobName -Container $containerName -Context $ctx
$blob.ICloudBlob.CreateSnapshotAsync()
Get-AzStorageBlobContent -CloudBlob $blob.ICloudBlob -Context $ctx -Destination $destination -Force | Out-Null
$content = Get-Content -Path $destination -Raw
$occurrences = ($content | Select-String $stringToReplace -AllMatches).Matches.Count
Write-Host -ForegroundColor Green "Total $occurrences occurrences to replace."
Write-Host -ForegroundColor Green "Replacing $stringToReplace with $stringToReplaceWith."
$content.Replace($stringToReplace, $stringToReplaceWith) | Set-Content -Path $destination
Write-Host -ForegroundColor Green "Uploading blob:$($blob.Name) to container:$($blob.BlobClient.BlobContainerName); storage account: $($blob.BlobClient.AccountName)"
Set-AzStorageBlobContent -File $destination -Container $blob.BlobClient.BlobContainerName -Blob $blob.Name -Context $ctx -Force
}
catch {
Write-Host "Something went wrong."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment