DbConnectionStringBuilder在PowerShell中使用时不进行分析
我正在尝试使用DbConnectionStringBuilder在PowerShell中使用时不进行分析,powershell,Powershell,我正在尝试使用DbConnectionStringBuilder的功能来解析像用户输入这样的连接字符串。这在C#中非常有效: 输出: smtp.gmail.com 587 you@gmail.com server = 'smtp.gmail.com'; port = 587; user = you@gmail.com server=smtp.gmail.com;port=587;user=you@gmail.com smtp.gmail.com 587 you@gmail.com apost
DbConnectionStringBuilder
的功能来解析像用户输入这样的连接字符串。这在C#中非常有效:
输出:
smtp.gmail.com
587
you@gmail.com
server = 'smtp.gmail.com'; port = 587; user = you@gmail.com
server=smtp.gmail.com;port=587;user=you@gmail.com
smtp.gmail.com
587
you@gmail.com
apostrophes="Some 'value'";quotations='Some "value"';semicolons="1; 2; 3";multiline="Line1
Line2
Line3";name with spaces="Some value";name with == sign="Some value"
Key Value
--- -----
apostrophes Some 'value'
quotations Some "value"
semicolons 1; 2; 3
multiline Line1
Line2
Line3
name with spaces Some value
name with = sign Some value
但它在PowerShell中不起作用。也就是说,这是PowerShell中的字面翻译代码
$sb = New-Object System.Data.Common.DbConnectionStringBuilder
$sb.ConnectionString = "server = 'smtp.gmail.com'; port = 587; user = you@gmail.com"
$sb["server"]
$sb["port"]
$sb["user"]
根本不产生任何输出
你知道为什么吗?如何使DbConnectionStringBuilder
在PowerShell中作为解析器工作?这可能是一个bug(无论如何都是一个问题),我将很快提交它。看起来PowerShell不会对实现IDictionary
的类的属性调用setter(就像DbConnectionStringBuilder
那样,解析字符串的是ConnectionString
的setter)
$sb = New-Object System.Data.Common.DbConnectionStringBuilder
$sb.Add("server","smtp.gmail.com")
$sb.Add("port",587)
$sb.Add("user","you@gmail.com")
$sb["server"]
$sb["port"]
$sb["user"]
以下是两个演示(原始和解决方案):
输出:
smtp.gmail.com
587
you@gmail.com
server = 'smtp.gmail.com'; port = 587; user = you@gmail.com
server=smtp.gmail.com;port=587;user=you@gmail.com
smtp.gmail.com
587
you@gmail.com
apostrophes="Some 'value'";quotations='Some "value"';semicolons="1; 2; 3";multiline="Line1
Line2
Line3";name with spaces="Some value";name with == sign="Some value"
Key Value
--- -----
apostrophes Some 'value'
quotations Some "value"
semicolons 1; 2; 3
multiline Line1
Line2
Line3
name with spaces Some value
name with = sign Some value
就解决方法而言,我们免费获得了一个功能强大的解析器,用于连接字符串数据。下面是演示如何解析非常复杂的输入:
# get the parser
$sb = New-Object System.Data.Common.DbConnectionStringBuilder
# input the string to parse using this workaround way
$sb.PSObject.Properties['ConnectionString'].Value = @'
Apostrophes = "Some 'value'";
Quotations = 'Some "value"';
Semicolons = '1; 2; 3';
Multiline = Line1
Line2
Line3;
Name with spaces = Some value;
Name with == sign = Some value;
'@
# get the parsed results
# the string: not the original but parsed and re-formatted
$sb.ConnectionString
# the data: parsed key/values
$sb | Format-Table -AutoSize -Wrap
输出:
smtp.gmail.com
587
you@gmail.com
server = 'smtp.gmail.com'; port = 587; user = you@gmail.com
server=smtp.gmail.com;port=587;user=you@gmail.com
smtp.gmail.com
587
you@gmail.com
apostrophes="Some 'value'";quotations='Some "value"';semicolons="1; 2; 3";multiline="Line1
Line2
Line3";name with spaces="Some value";name with == sign="Some value"
Key Value
--- -----
apostrophes Some 'value'
quotations Some "value"
semicolons 1; 2; 3
multiline Line1
Line2
Line3
name with spaces Some value
name with = sign Some value
System.Data.Common.DbConnectionStringBuilder实现IDictionary
。Powershell有一个使用
的字典速记,它允许检索和分配键/值对,就像键是属性一样:
$dict = @{}
$dict["key1"] = 'value1'
$dict.key2 = 'value2'
您可以看到,它将整个连接字符串存储为键/值对,而不是以这种方式存储在ConnectionString
属性上:
$sb = New-Object System.Data.Common.DbConnectionStringBuilder
$sb.ConnectionString = "server = 'smtp.gmail.com'; port = 587; user = you@gmail.com"
$sb #formatted table of key/value pairs
解决此问题的最简单方法是调用为属性生成的set
/get
方法:
$sb = New-Object System.Data.Common.DbConnectionStringBuilder
$sb.set_ConnectionString("server = 'smtp.gmail.com'; port = 587; user = you@gmail.com")
$sb["server"]
$sb["port"]
$sb["user"]
我没有您传入的数据Add()
。这就是问题的关键:任务是从一个输入连接字符串中提取数据,比如string.ah,在这个字符串上不确定——看起来应该是一样的,但正如您所指出的,它不是。你可以从stringdata$($sb.ConnectionString-split”;“-join”`n”):)这样的复杂字符串怎么样:撇号=“'foo'”;引文='条形';分号='1;2.3'; 简单值=一些值
DbConnectionStringBuilder
是一款强大的解析器,我们应该免费获得它的功能。我期待着看到你的答案。这种情况再次发生。我一提出问题,很快就会找到答案。问题确实会刺激你。我将发布答案(关于为什么和解决方法),但让我把这个问题保持开放一段时间,作为一个挑逗和一个很好的PowerShell练习(除非有人立即需要答案)。我提交了这个问题:非常好。set\u ConnectionString()
技巧对我来说是新的,它显然比我在回答中提出的方法简单。谢谢我记得为属性生成了get
/set
方法,这些方法实际上在IL中被调用。C#不会让你直接给他们打电话,但Powershell更松散,所以我试了一下,学到了一些新东西。但我也从你那里学到了一些东西,PSObject
是Powershell中基本反射的关键。这是否曾作为bug正式提交[是否有链接]?在过去6年多的时间里,它是否得到了解决(如果是,从哪个版本开始)。。。