Reputation: 187
Working on a script that takes a FQDN from user input, then enters that server and domain name and adds it to a text file after the pattern ID=
.
Have a text file and inside is a line that has ID=[some number]
.
I want to have a cmdlet change the [some number]
that is found after ID=
to 2 variables that I defined.
Solution from Ansgar:
$name,$domain,$rest = $serverName.split('.',3)
(get-content 'c:\file.txt') -replace "(ID=).*", "`$1$domain$name" | set-content 'c:\file.txt'
Using Powershell v2.
Upvotes: 0
Views: 72
Reputation: 200293
Simply replace that number with the content of the variables, then write the text back to the file:
(Get-Content 'C:\file.txt') -replace '(ID=)\[\d*\]', "`$1$domain$name" |
Set-Content 'C:\file.txt'
Upvotes: 1