Powershell regex for connectionStrings?

2019-06-17 01:44发布

For some reason I'm having a brutal time parsing the connection string in the web.config file.

I've already gotten connectionString but I'm trying to get all the values such as the

  • Data Source
  • Initial Catalog
  • Username
  • etc...

The connection string looks like:

Data Source=db.sample.com;user id=sample-user;password=sample-password;Initial Catalog=sample-catalog;

1条回答
闹够了就滚
2楼-- · 2019-06-17 02:32

Use 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

Output:

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

See also for more details: DbConnectionStringBuilder does not parse when used in PowerShell

(That is why this funny syntax $sb.set_ConnectionString(...) is used instead of $sb.ConnectionString = ...).

查看更多
登录 后发表回答