【问题标题】:PowerShell foreach not iterating correctlyPowerShell foreach 未正确迭代
【发布时间】:2021-06-07 15:25:16
【问题描述】:

我有一些 SQL 输出,我正试图通过 PowerShell 的 foreach cmdlet 进行操作,但我遇到了让它遍历输出的每一行的问题。

这是我拥有的 PowerShell 代码:

$result = @(D:\sql_select.bat --file D:\SQL.sql)
$output = $result -split "`r`n"

foreach ($l in $output) {
$name = [Regex]::Matches($output, ".+?(?=~)")
$size = [Regex]::Matches($output, "[^~]*$")
$tables += @{$name = $size}
}

这是我正在处理的一些 SQL 输出:

Table1~9.00
Table2~5.61
Table3~1.13
Table4~0.93
Table5~0.72
Table6~0.57

'foreach $l in $output' 似乎没有正常工作,因为它没有将键/值对放入哈希表中。我想让它工作,这样 Table1 将成为一个值为 9.00 的键,Table2 将与 5.61 配对,等等。我怎样才能使它工作?

(注意:我知道 Invoke-SQLCmd 在 PowerShell 中可用并且非常有用,但很遗憾我无法在这里使用它。所以,请不要费心建议将其作为解决方案。)

【问题讨论】:

  • $output.count 的结果是什么?
  • @santisq 结果为 6(返回的表数)。
  • 您正在使用 var $l 进行迭代,但在内部您使用的是 collection$output...
  • @{$name = $size} -> @{$name[0].Value = $size[0].Value}

标签: powershell loops foreach hashtable


【解决方案1】:

所以,我使用您的 SQL 输出作为测试的输入,这应该可以正常工作:

$output='Table1~9.00
Table2~5.61
Table3~1.13
Table4~0.93
Table5~0.72
Table6~0.57' -split '\s+'

foreach ($l in $output) {
    $name = ([Regex]::Match($l, ".+?(?=~)")).Value
    $size = ([Regex]::Match($l, "[^~]*$")).Value
    $tables += @{$name = $size}
}

PS C:\> $tables

Name                           Value                                                                                        
----                           -----                                                                                        
Table1                         9.00                                                                                         
Table2                         5.61                                                                                         
Table3                         1.13                                                                                         
Table4                         0.93                                                                                         
Table5                         0.72                                                                                         
Table6                         0.57   

如果您使用[regex]::Matches,您的匹配项将是一个数组,结果如下所示:

PS C:\> $tables

Name                           Value                                                                                        
----                           -----                                                                                        
Table6                         {0.57, }                                                                                     
Table5                         {0.72, }                                                                                     
Table4                         {0.93, }                                                                                     
Table3                         {1.13, }                                                                                     
Table2                         {5.61, }                                                                                     
Table1                         {9.00, } 

您可能还需要使用$tables += [ordered]@{$name = $size},以防您需要有序哈希表。

【讨论】:

  • 谢谢,这正是我想要的。我还看到了我在 foreach 循环中出错的地方。 :)
  • 没问题,很高兴为您提供帮助
猜你喜欢
  • 2017-03-12
  • 1970-01-01
  • 2011-08-24
  • 2012-08-29
  • 1970-01-01
  • 1970-01-01
  • 2014-05-12
  • 1970-01-01
  • 2016-07-28
相关资源
最近更新 更多