首页 > 解决方案 > 用 Powershell 替换字符串

问题描述

我有一个 .properties 文件,我想在其中将字符串 Compass 替换为 BBB。我的问题是:我想提取属于 name, JDBC/,的字符串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"
}

运行属性:

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

标签: powershell

解决方案


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

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

$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)
)

推荐阅读