Answer the question
In order to leave comments, you need to log in
How to change only the first found value in powershell?
Good evening, tell me how can I modify the code so that only the first match found is changed (and not everything after -match)
$VPNServer = "vpn.domain.ru"
$ConfigVPN = "$env:APPDATA\Microsoft\Network\Connections\Pbk\rasphone.pbk"
$output = (Get-Content ("$ConfigVPN") -Raw) -split '(?=\+\])' |
foreach {
if ($_ -match "$VPNServer"){
$tmp = $_
$tmp = $tmp -replace 'ExcludedProtocols=\d+','ExcludedProtocols=8'
$tmp = $tmp -replace 'DisableClassBasedDefaultRoute=\d+','DisableClassBasedDefaultRoute=1'
$tmp = $tmp -replace 'PreferredHwFlow=\d+','PreferredHwFlow=1'
$tmp = $tmp -replace 'PreferredProtocol=\d+','PreferredProtocol=1'
$tmp = $tmp -replace 'PreferredCompression=\d+','PreferredCompression=1'
$tmp = $tmp -replace 'PreferredSpeaker=\d+','PreferredSpeaker=1'
$tmp = $tmp -replace 'AutoTiggerCapable=\d+','AutoTiggerCapable=1'
$tmp } else {$_}
}
$output = $output -join ''
$output
Answer the question
In order to leave comments, you need to log in
I think you should go the more general route.
since rasphone.pbk is actually an ini, you should look for something about reading ini files on powershell
, for example, this is a classic
https://devblogs.microsoft.com/scripting/use-power...
Hello.
If you want a solution in your own code, then you simply do not have enough break
in the if condition .
Here is a simple demo example:
$hostPC = ("ya.ru", , "ya.ru", "ya.ru", "google.ru","docs.microsoft.com")
foreach ($item in $hostPC) {
if($item -eq "ya.ru"){
Write-Host("MEOW");
break;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question