Powershell script to modify file content

Need power-shell script to modify parameter in configuration file

I'm attaching an test file where I need to modify GIM_USE_SSL value to 0 using power shell on 100 of servers. Need a way to automate this thing kindly find the below parameter which needs to be modified using script

                                                                     'ts' => '2009-07-15 17:05:17.0',
                                                                     'value' => '1'
LVL 10
Manikandan NarayanswamySecurity Specialist & IBM Security GuardiumAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Try this; $InFile and $OutFile may be identical.
$InFile = "D:\Temp\test.txt"
$OutFile = "D:\Temp\test2.txt"
$Param = 'GIM_USE_SSL'
$NewValue = '0'

$RegEx = "(?<=\A\s*'value'\s*=>\s*')(?<Value>.*?)(?='\s*\Z)"
$MatchFound = $false
$InParamSection = $false
@(Get-Content -Path $InFile) | ForEach-Object {
	If ($_ -match "'$($Param)' => {") {
		$InParamSection = $true
	} Else {
		If ($InParamSection) {
			If ($_ -match "\A\s*}\s*,\s*\Z") {
				$InParamSection = $false
			} Else {
				If ($_ -match $RegEx) {
					$MatchFound = $true
					$_ -replace $RegEx, $NewValue
					Write-Host "[$($InFile)] $($Param): replaced old value '$($Matches['Value'])' with '$($NewValue)'" -ForegroundColor Yellow
				} Else {
		} Else {
} | Set-Content -Path $OutFile
If (-not $MatchFound) {
	Write-Error "[$($InFile)] no match found for $($Param)!"

Open in new window

Ben Personick (Previously QCubed)Lead Network EngineerCommented:
The following code will do the needful on the file.

$TestFile = "C:\Admin\Experts-Exchange\Test.txt"
$xmt = Get-Content $TestFile
for ($i=0; $i -lt $xmt.count;$i++) {if ($xmt[$i] -match ".*'GIM_USE_SSL'.*" ) {$SSL_Index=$i; break}}
for ($i=$SSL_Index; $i -lt $xmt.count;$i++) {if ($xmt[$i] -match ".*'value'.*" ) {$SSLValue_Index=$i; break}}
$xmt[$SSLValue_Index] = $xmt[$SSLValue_Index] -replace "'1'","'0'"
$xmt | set-content $TestFile

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Manikandan NarayanswamySecurity Specialist & IBM Security GuardiumAuthor Commented:
Thanks for the help
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
Glad to help :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.