I am new in powershell and I have created the following script which it extracts what's between http:// and the next /, transform it and then replace the intial match:
$fileName = "myfile"
$newEnvironment = "NewEnvironment"
$config = Get-Content $fileName
$newConfig = $config | % { $_ -replace "http://www.site.de", "http://site.de.$newEnvironment" }
$newConfig = $newConfig | % { $_ -replace "http://www.site.com.tr", "http://site.com.tr.$newEnvironment" }
$newConfig = $newConfig | % { $_ -replace "http://www.site.fr", "http://site.fr.$newEnvironment" }
$newConfig = $newConfig | % { $_ -replace "http://www.site.pl", "http://site.pl.$newEnvironment" }
$newConfig = $newConfig | % { $_ -replace "http://www.site-1.be", "http://site-1.be.$newEnvironment" }
$newConfig = $newConfig | % { $_ -replace "http://www.site-1.nl", "http://site-1.nl.$newEnvironment" }
$newConfig = $newConfig | % { $_ -replace "http://www.site.it", "http://site.it.$newEnvironment" }
$newConfig | Set-Content $fileName
I'm trying to make it better, maybe using regex or something else but not using hard coded text. Could anyone please help me with this ?
I was thinking something like:
$path = "myFile";
Get-Content $path | Foreach {$_ -replace "(?<=http://).+?(?=/.*)",".+?(?=/.*).newEnvironment"};
Set-Content $path;
But It did not work, even if it was setting the links in this way:
http://.+?(?=/.*).newEnvironment/asd/test.aspx