I'm trying to write a script that generates new GUIDs when finding a match. My issue is that I keep getting the same GUID generated for all matches. How do I do this without generating the same GUID for all matches?
$testString = @"
[assembly: Guid Should Replace]
[assembly: Guid Should Replace]
[assembly: Guid Should Replace]
"@
#expected output
#[assembly: "unique guid"]
function ReplaceWithNewGuid {
param($content)
$retval = ($content -ireplace '(?m)(\[assembly: Guid.*$)+', "[assembly: Guid(`"$([guid]::NewGuid())`"]`)")
return $retval
}
ReplaceWithNewGuid($testString)
Example of actual output:
[assembly: Guid("29e784aa-ba4a-4a45-85b8-d6b52916b539"])
[assembly: Guid("29e784aa-ba4a-4a45-85b8-d6b52916b539"])
[assembly: Guid("29e784aa-ba4a-4a45-85b8-d6b52916b539"])
Update
The answer from @Mathias R. Jessen helped me get what I needed. I was thinking I could do this in powershell without using the .net framework libs but, this works as expected.
function ReplaceWithNewGuid {
param($content)
$retval = [regex]::Replace($testString, '(?m)(\[assembly: Guid.*$)+', {param($m) return "[assembly: Guid(`""+ (New-Guid).Guid + "`")]"}, 'IgnoreCase')
return $retval
}