0

Very new to Powershell (and programming in general), so please bear with me. I recently created a script that would allow the user to modify system names within a backup file, then create a new .txt with the new settings. The problem I ran into is that the script only works for systems that follow our naming convention-- new systems or incorrectly-named systems cause the script to fail because its searching for a name that doesn't exist yet. This is what I have so far:

$name = Read-Host 'What is the system name?'
$currentdate = Get-Date -Format "MM_DD_YY_HH_MM_SS"
if ($name -notlike "Sample*")
    {
        Read-Host 'Please begin naming conventions with "Sample".'
        }
        else {
        (Get-Content C:\Test_Backup.txt)|
Foreach-Object {$_-replace 'Sample[a-z , 0-9]*' , $name}|
Out-file C:\Test_Success.txt} 

So, for example, when you open the backup file of a new/incorrect system and look for the system name, it appears as NAME: "Wrongname123." The script doesn't find "Sample*" in the file, so nothing happens. What I'd like to do is change the script to work with new/incorrectly-named systems by replacing any name that appears within the quotations to the correct, user-input name. I'm not even sure this is possible, and I haven't been able to find an answer through Google searching thus far, which I imagine is because I'm having such difficulty explaining what I want it to do. I'd really appreciate any help I can get.

EDIT:

What I'm doing is creating backups of system settings in a .txt. When you open the backup file, it looks something like this:

NAME: "System1" VOLUME: "30" INPUT: "RGB" etc...

What I've done is create a "master" backup file so that users can quickly upload the file to a new system without having to manually enter all the proper settings. The only issue with this is that the user has to change the name when uploading it to a new system (each system has a unique name). My example of the backup file is extremely simplified-- there's tons of information in the file, so it's not easy to go in and manually change this. When I run the script, it prompts the user to enter a name for the system they're configuring. So if the system is currently named "Sample6" and the user wants to change it to "Sample7," there are no issues. However, if the system is named "Wrongname1," the script won't work because it's looking for the "Sample" naming convention. So what I'm trying to figure out is how I can get the script to look for NAME:"[any value here]" and then change whatever values are contained within quotations. Hope this makes a little more sense!

Blake
  • 29
  • 6
  • i , as a beginner, try to stay away from regular expressions, especially in that form. from what i gather you might be having issues with the flow/anchoring. Second, it looks like "Sample" is a wanted pattern but then you're replacing it? :) Cam you post an example or two of both good and bad lines? – Jaqueline Vanek Dec 24 '15 at 17:54
  • Hi Jaqueline, thanks for responding. I updated my original post-- I'm hoping it makes a little more sense now! – Blake Dec 24 '15 at 18:15
  • well, why dont you just read the system name and if its "bad" either change it or record it? also, you might consider using different "text" file format -- JSON or psd1 seem suitable for such a task, also xml but its gonna be a bit of a overkill and it's a bit harder to work with – Jaqueline Vanek Dec 24 '15 at 18:25
  • ah, i think i got your issue, finally :D, in the txt you have ''S1, S2, S3" and if the systems is B4 you have nothing to compare against, stil it's supossed to be one of the three, right? – Jaqueline Vanek Dec 24 '15 at 18:40
  • 1
    Yes! Sorry for the poor explanation! James understood what I was trying to do, fortunately :) – Blake Dec 24 '15 at 18:56

1 Answers1

2

If I understand your question, you are looking to replace the contents between the quotation marks with the user input. If that is the case, simply replace your regular expression with the one from this answer, like so:

Foreach-Object {$_-replace '(["''])(?:(?=(\\?))\2.)*?\1' , $name}|

Note the double apostrophe since PowerShell uses apostrophes for constant strings. If that is what you intend, make sure you upvote that poster's answer, once you have enough reputation.

If Test_Backup.txt contains a list of colon-separated attributes, like this:

NAME: "WrongName123"
LAST_BACKUP:  "12_24_15_12_00_00"
FAVORITE_COLOR:  "Blue"

then your script will replace all values in quotation marks. If you only want to replace the one marked NAME, you will want to insert a Where-Object call into the pipeline. I'm using -like but you can use -match as well:

(Get-Content C:\Test_Backup.txt) |
Where-Object { $_ -like 'NAME:*' } |
Foreach-Object {$_ -replace '(["''])(?:(?=(\\?))\2.)*?\1', $name} |
Out-File C:\Test_Success.txt

Happy programming!

Community
  • 1
  • 1
JamesQMurphy
  • 4,214
  • 1
  • 36
  • 41