2011-09-30 17 views
6

何らかの理由で、私はweb.configファイル内の接続文字列を解析して残酷な時間があります。Powershell regex for connectionStrings?

は、私はすでにのConnectionString得ているが、私は、このような

  • データソース
  • 初期カタログ
  • ユーザー名など
  • ...
としてすべての値を取得しようとしています

接続文字列は次のようになります。

データソース= db.sample.com;ユーザー ID =サンプルユーザー、パスワード=サンプルパスワード、初期 カタログ=サンプルカタログ。

+1

正規表現を使用する必要がありますか?これはPowershellが文字列を分割するために使用するものを2回呼び出したように見えます。 ';'で分割した後、結果をループし、 '='で分割してキーと値のペアを取得します。私は答えとしてこれを入れたいと思いますが、私にはわかりません.PowerShellに分割機能があるのか​​、そしてb)何が呼び出されているのか分かりません。 – CanSpice

+0

それはうまく動作します!ありがとう! –

答えて

12

使用System.Data.Common.DbConnectionStringBuilder

$sb = New-Object System.Data.Common.DbConnectionStringBuilder 
$sb.set_ConnectionString('Data Source=db.sample.com;user id=sample-user;password=sample-password;Initial Catalog=sample-catalog;') 

$sb 

出力:

Key    Value   
---    -----   
data source  db.sample.com 
user id   sample-user  
password  sample-password 
initial catalog sample-catalog 

は詳細参照:DbConnectionStringBuilder does not parse when used in PowerShell

(この変な構文$sb.set_ConnectionString(...)$sb.ConnectionString = ...の代わりに使用されている理由があります)。

関連する問題