-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
  • 1,535
  • 18
  • 40

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
  • 312,089
  • 56
  • 508
  • 622
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,109
  • 2
  • 20
  • 25