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:
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:
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.
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.
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.
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…).