【问题标题】:Powershell -contains/-notcontains - having trouble with an array result from SQLServerPowershell -contains/-notcontains - SQLServer 的数组结果有问题
【发布时间】:2011-03-25 10:01:54
【问题描述】:

我正在使用 Invoke-SQLCmd 来获取我想要处理的项目列表。在该列表中可能有一个项目表明需要绕过整个列表。我试图弄清楚如何使用 -contains 和 -notcontains 运算符,但我没有任何运气。这将是伪代码:

$dont_process = "dont process"
$list = Invoke-SQLCmd ........
if ($list -notcontains $dont_process) {processing goes here}

$list 是 DataRows 的 System.array。该运算符应该在一个数组上工作,但我猜 DataRows 数组不是它工作的那种。任何帮助将不胜感激。

【问题讨论】:

    标签: sql-server powershell


    【解决方案1】:

    你的$dont_process 变量是一个字符串,不是吗? -contains-notcontains 运算符对列表中的每个项目使用 -eq 运算符。 -eq 运算符是 .Net 比较,因此 DataRow 类必须支持 DataRow 和字符串之间的比较。根据 MSDN,它是does not

    事实上,我不完全确定您将如何比较 DataRow 和字符串。它会是该行中第一个项目的名称吗?

    为了回答这个问题,假设您想比较 DataRow 的 .ToString()

    您可能不得不诉诸更冗长的做事方式:

    $list = Invoke-SQLCmd ...
    
    $dontProcess = "dont process"
    $shouldProcess = $true    
    $foreach ($dataRow in $list) { 
        if ($dataRow.ToString() -eq $dontProcess) { 
            $shouldProcess=$false; 
        } 
    }
    
    if($shouldProcess) {
        # processing goes here
    }
    

    【讨论】:

    • 谢谢-实际上,我在周末进行了更多搜索,发现了这个,它也很好用 $list = New-Object System.Collections.ArrayList $dont_process = "dont_process" Invoke-Sqlcmd ... ...| foreach {[Voidd] $$list.add($_.name)} If ($list -cnotontains $dont_process) {#process here}
    猜你喜欢
    • 2012-12-03
    • 2013-03-08
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 2021-08-05
    • 2011-01-31
    • 2014-06-24
    • 1970-01-01
    相关资源
    最近更新 更多