Items not replaced in string after -replace

This topic contains 5 replies, has 3 voices, and was last updated by  Kevyn 3 months, 1 week ago.

  • Author
    Posts
  • #79283

    Mike Wingeier
    Participant

    I have a text file of system connections that are read into arrays so that I can run find and replace on each entry to update the server names. The entries look like the following:

    ConnectionName =
      (DESCRIPTION =
        (ADDRESS_LIST =
          (ADDRESS = (PROTOCOL = TCP)(HOST = HOSTNAME )(PORT = 1521))
        )
        (CONNECT_DATA =
          (SERVICE_NAME = ConnectionName)
        )
    

    I am running a series of IF statements on the array, my objective being if the connection name equals something, then I change the HOST line to the new entry and write it to a file. This results in lines of code like this:

    if ($result[$i] -match "^ConnectionName") { $result[$i]-replace ('OldHost','NewHost') | Out-File $TempFile -Encoding ascii -Append ; Add-Content $TempFile  

    I am writing to a file because the array element is unchanged in memory, but it is correct when it gets written to the file. When I look at the array in memory, it still has its original data. Why is this happening? It would be so much easier to change the elements in memory and then write the entire array back out to the file, but I have had no success in doing that. Thank you for your assistance.

  • #79285

    Don Jones
    Keymaster

    The -replace operator creates a new string. It doesn't modify the original string.

    $original = "XYZ"
    $new = $original -replace "X","A"

    $original will still be XYZ; $new will be AYZ.

    You've not captured the new string so it's being emitted to the pipeline.

  • #79291

    Mike Wingeier
    Participant

    Thank you so much for that lightning quick reply, Don. As a follow-up newbie question, how do I accomplish dealing with the new string for this purpose? When I put $new[i] = $replace[i], it didn't like that at all and I got "array index expression missing or not valid." I tried to just use a single variable, $new, but as expected, that just overwrote every item in the array with the same information. I've been dong this for about 3 months and I am not seasoned enough to know what I don't know. I've been searching the internet for a week on this problem.

  • #79303

    Kevyn
    Participant

    If you're trying to replace the contents of $replace[$i], you can do the following:

    $replace[$i] = $replace[$i] -replace "OldString","NewString"
    

    BTW, you need a space before the dash(-), in your above code:

    $result[$i]-replace ('OldHost','NewHost')
    

    Corrected:

    $result[$i] -replace ('OldHost','NewHost')
    
  • #79316

    Mike Wingeier
    Participant

    Thanks again, both of you. It seems so obvious now that I hate to have posted it, but that was the quickest and most helpful set of responses I have ever had on any forum bar none.

  • #79327

    Kevyn
    Participant

    Not a problem. We're glad to help. There are no "stupid" questions here. When you need help, you need help.

    If I may, I'd like to suggest the following resources to help you learn powershell better. I highly recommend them. The books are co-authored by Don Jones and Jeffrey Hicks who happen to run this site. If you buy the physical books (pbooks as they call them), you also get the digital copies free in several forms (pdf, kindle, etc...).

    Beginning Book/Courses:
    -Learn Windows PowerShell In A Month Of Lunches (Chapter 9 is especially awesome at explaining the pipeline.)
    –Getting Started with Microsoft PowerShell: https://mva.microsoft.com/en-US/training-courses/getting-started-with-microsoft-powershell-8276?l=vOd1PSWy_9204984382 (A Series of videos with Jeffrey Snover (helped create/design Windows Powershell) & Jason Helmick; Watching this in concert with reading the Learn Windows PowerShell In A Month Of Lunches book really helped the info sink in.)
    –Getting Started with PowerShell Desired State Configuration (DSC): https://mva.microsoft.com/en-US/training-courses/getting-started-with-powershell-desired-state-configuration-dsc-8672?l=ZwHuclG1_2504984382 (Not yet started this.)

    Advanced Book/Courses:
    -Learn PowerShell Toolmaking In A Month Of Lunches (Reading through this now.)
    –Advanced Tools & Scripting with PowerShell 3.0 Jump Start: https://mva.microsoft.com/en-US/training-courses/advanced-tools-scripting-with-powershell-30-jump-start-8277?l=WOWaGUWy_8604984382 (A Series of videos with Jeffrey Snover (helped create/design Windows Powershell) & Jason Helmick); Watching this in concert with reading the Learn PowerShell Toolmaking In A Month Of Lunches book has really helped the info sink in.)
    –Advanced PowerShell Desired State Configuration (DSC) and Custom Resources : https://mva.microsoft.com/en-US/training-courses/advanced-powershell-desired-state-configuration-dsc-and-custom-resources-8702?l=3DnsS2H1_1504984382 (Not yet started this.)

You must be logged in to reply to this topic.