1

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
}
Community
  • 1
  • 1
Terrance
  • 11,764
  • 4
  • 54
  • 80
  • [guid]::NewGuid() should always generate a unique value... that's the full script? – Austin T French Sep 13 '17 at 14:26
  • 2
    String interpolation happens before your command runs. So you generate the GUID to replace with once and then all the replaces happen. I think having each value be unique is going to be a lot more complex. – BenH Sep 13 '17 at 14:26
  • 1
    Sorry, but which `[assembly: Guid...]` should be replaced? – Wiktor Stribiżew Sep 13 '17 at 14:29
  • @WiktorStribiżew the ones that say "Guid Should Replace" – Terrance Sep 13 '17 at 14:29
  • @AustinFrench yep, it is – Terrance Sep 13 '17 at 14:30
  • @Terrance Does that mean that only those should be replaced that are at the start/end of the line? Please provide an expected output. – Wiktor Stribiżew Sep 13 '17 at 14:31
  • The more I look at this, the more I'd go with a string builder, split the full string, check each word for a match by guid, and then append the old or new guid to the string builder... – Austin T French Sep 13 '17 at 14:34
  • I'm asking about getting a unique string output per iteration using -replace vs the other question which is asking about rolling their own replacement function. Just because the two have similar topics and similar answers does not make this a duplicate. Just because the answer is 1 in the expression 3-2 and 30-29 does not make the question the same. – Terrance Sep 13 '17 at 15:09

2 Answers2

5

You could use Regex.Replace() directly, allows you to pass a scriptblock in place of the MatchEvaluator delegate parameter:

$testString = @"
[assembly: Guid Should Replace]
[assembly: Guid Should Replace]    
[assembly: Guid Should Replace]
"@

[regex]::Replace($testString, 'Guid Should Replace', {param($m) return (New-Guid).Guid}, 'IgnoreCase')

You should see that it returns 3 distinct identifiers

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • Yeah, the only problem was with the regex for the original input. After the edit, it is now a dupe of https://stackoverflow.com/questions/8163061/passing-a-function-to-powershells-replace-function – Wiktor Stribiżew Sep 13 '17 at 14:49
  • @WiktorStribiżew I tried this with my existing regex and it works as expected. It's not the regex but this is the best answer. – Terrance Sep 13 '17 at 14:56
  • @Terrance Yes, it is, all the problem for you was how to use a callback inside Regex.Replace. – Wiktor Stribiżew Sep 13 '17 at 14:57
-1
(New-Guid).Guid

Presto, Guids on demand.

And your function repaired:

function ReplaceWithNewGuid
{
    param($content)

    $retval = ForEach ($Line in ($content -split "`n"))
    {
        $Line -replace 'guid.*$',"Guid(`"$((New-Guid).Guid)`"]"
    }
    Return $retval
}
mklement0
  • 382,024
  • 64
  • 607
  • 775
Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63