0

I have a .properties file in which I want to replace the string Compass with BBB. My question is : I want to extract string which is belong name , JDBC/ , ds_name = '' , java.lang.String then I will update with a new one. BTW, data source name is not fixed its dynamic variable. Just I have written it as sample string.

I have tried the following PowerShell code:

$DName = read-host -prompt "Please Enter Database Name"

ForEach ($client in (Get-Content Clients.txt)) {
    (Get-Content "\\$client\D$\Runtime\run.properties") -replace "$old database name which is extract","$DName" | 
        Out-File "\\$client\D$\Runtime\run.properties"
}

run.properties:

dsid = AdminTask.createDatasource(provider_id, '[-name Compass -jndiName jdbc/Compass 
-dataStoreHelperClassName com.ibm.websphere.rsadapter.MicrosoftSQLServerDataStoreHelper 
-componentManagedAuthenticationAlias TEMP-HRZEMM01Node01/PlatformDataSource -containerManagedPersistence true 
-xaRecoveryAuthAlias TEMP-HRZEMM01Node01/PlatformDataSource -configureResourceProperties [[databaseName java.lang.String Compass] [portNumber java.lang.Integer 1433] [serverName java.lang.String SQLSVR1]]]')

AdminConfig.create('MappingModule', dsid , '[[authDataAlias TEMP-HRZEMM01Node01/PlatformDataSource] [mappingConfigAlias ""]]')

ds_name = 'Compass' #Name copied from your question, update if required
1
  • What was the result of running this script? If there were any error messages, please copy/paste them into the question as text. Please describe what the script is not doing correctly. Commented Oct 10, 2018 at 13:56

1 Answer 1

1

If I understand the question correctly, you would like to first find the database name (which can be anything, Compass is just an example) stored in the .properties file and if found replace that by a value entered in the console.

In that case, I think this should do it:

$newDbName = Read-Host -prompt "Please Enter Database Name"
$clientFile = "Clients.txt"

ForEach ($client in (Get-Content $clientFile)) {
    $content = Get-Content "\\$client\D$\Runtime\run.properties" -Raw
    # see if we can extract the database name from the file
    if ($content -match '(?:-name\s+|jdbc/|databaseName java\.lang\.String\s+|ds_name = '')(?<dbname>[^\s''\]]+)') {
        $oldDbName = $matches['dbname']

        Write-Host "Replacing '$oldDbName' with '$newDbName' for client '$client'" 
        ($content -replace $oldDbName, $newDbName) | 
            Out-File "\\$client\D$\Runtime\run.properties"
    } 
    else {
        Write-Warning "Could not parse the old database name from '\\$client\D$\Runtime\run.properties'.."
    }
}

Regex explanation

(?:                        Match the regular expression below
                           Match either the regular expression below (attempting the next alternative only if this one fails)
      -name                Match the characters “-name” literally
      \s                   Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
         +                 Between one and unlimited times, as many times as possible, giving back as needed (greedy)
   |                       Or match regular expression number 2 below (attempting the next alternative only if this one fails)
      jdbc/                Match the characters “jdbc/” literally
   |                       Or match regular expression number 3 below (attempting the next alternative only if this one fails)
      databaseName\ java   Match the characters “databaseName java” literally
      \.                   Match the character “.” literally
      lang                 Match the characters “lang” literally
      \.                   Match the character “.” literally
      String               Match the characters “String” literally
      \s                   Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
         +                 Between one and unlimited times, as many times as possible, giving back as needed (greedy)
   |                       Or match regular expression number 4 below (the entire group fails if this one fails to match)
      ds_name\ =\ '        Match the characters “ds_name = '” literally
)
(?<dbname>                 Match the regular expression below and capture its match into backreference with name “dbname”
   [^\s'\]]                Match a single character NOT present in the list below
                           A whitespace character (spaces, tabs, line breaks, etc.)
                           The character “'”
                           A ] character
      +                    Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.