-1
(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = myhost)(PORT = 1521))(CONNECT_DATA = (SERVICE_NAME = ora)))

I want to replace the HOST name with a new value using powershell, Thank you

mellifluous
  • 2,345
  • 2
  • 29
  • 45

2 Answers2

2

Use the -replace operator:

$str = '(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = myhost)(PORT = 1521))(CONNECT_DATA = (SERVICE_NAME = ora)))'

$str -replace '(?<=\(HOST = )[^)]+', 'newhost'
mklement0
  • 382,024
  • 64
  • 607
  • 775
1

this uses the same -replace operator as used by mklement0, but uses a simpler pattern for those like myself who are regex-challenged. [grin]

$OldHost = 'myhost'
$NewHost = 'NewHostName'

$InStuff = '(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = myhost)(PORT = 1521))(CONNECT_DATA = (SERVICE_NAME = ora)))'

$OutStuff = $InStuff -replace "HOST = $OldHost", "HOST = $NewHost"

$InStuff
$OutStuff

output ...

(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = myhost)(PORT = 1521))(CONNECT_DATA = (SERVICE_NAME = ora)))
(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = NewHostName)(PORT = 1521))(CONNECT_DATA = (SERVICE_NAME = ora)))
Lee_Dailey
  • 7,292
  • 2
  • 22
  • 26