Link to home
Start Free TrialLog in
Avatar of enthuguy
enthuguyFlag for Australia

asked on

Powershell single line file exists check

Hi Powershell experts,

please help me in the logic. Below is the current single line script.

foreach($line in Get-Content C:\filepath.txt){(Get-Content $line).replace('DB_URL', ${RDSInstance.Endpoint.Address}) ^| Set-Content $line}

Open in new window


Would like extend a bit by
  1. check if file exists, then apply replace logic,
  2. else get next line
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Haven't tested this but it should work:

$FileCheck = "c:\filepath.txt"; if (!test-path $filecheck){Exit}else {try{foreach($line in Get-Content C:\filepath.txt){(Get-Content $line).replace('DB_URL', ${RDSInstance.Endpoint.Address}) ^| Set-Content $line}}catch {write-host "Something Went Wrong Processing line $($line)"}}

Open in new window

oBdA has the better solution..
Avatar of enthuguy

ASKER

Thanks a lot oBdA, ITguy,
will test these :)