Reputation: 29
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!
Upvotes: 0
Views: 108
Reputation: 4428
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!
Upvotes: 2