用Powershell替换字符串

时间:2018-10-10 12:42:24

标签: powershell

我有一个.properties文件,其中我想用BBB替换字符串Compass。我的问题是:我想提取属于的字符串 nameJDBC/ds_name = ''java.lang.String,然后我将更新一个新的。顺便说一句,数据源名称不固定其动态变量。我只是将其写为示例字符串。

我尝试了以下PowerShell代码:

$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 个答案:

答案 0 :(得分:1)

如果我正确理解了这个问题,那么您想首先找到存储在.properties文件中的数据库名称(可以是任何数字,Compass只是一个例子),如果找到,则用输入的值替换它在控制台中。

在这种情况下,我认为应该这样做:

$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'.."
    }
}

正则表达式说明

(?:                        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)
)