views:

21

answers:

1

I am using Invoke-SQLCmd to get a list of items that I want to process. In that list may be an item that indicates that the entire list needs to be bypassed. I have tried to figure out how to use the -contains and -notcontains operator but I am not having any luck. This would be the pseudo code:

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

$list is a System.array of DataRows. The operator is supposed to work on an array but I guess an array of DataRows is not the kind it works on. Any help would be greatly appreciated.

A: 

your $dont_process variable is a string, is it not? The -contains and -notcontains operators use the -eq operator on each item in the list. The -eq operator is a .Net comparison, so the DataRow class must support a comparison between between a DataRow and a string. According to MSDN, it does not.

In fact, I'm not entirely sure how you'd compare a DataRow and a string. Would it be the name of the first Item in the row?

For the purposes of this answer, let's pretend you want to compare the .ToString() of the DataRow.

You'll probably have to resort to the more-longwinded way of doing things:

$list = Invoke-SQLCmd ...

$dontProcess = "dont process"
$shouldProcess = $true    
$foreach ($dataRow in $list) { 
    if ($dataRow.ToString() -eq $dontProcess) { 
        $shouldProcess=$false; 
    } 
}

if($shouldProcess) {
    # processing goes here
}
tenpn
Thanks - Actually, I did more searching over the weekend and found this, which works nicely also $list = New-Object System.Collections.ArrayList $dont_process = "dont_process" Invoke-Sqlcmd ......| foreach {[Voidd] $$list.add($_.name)} If ($list -cnotontains $dont_process) {#process here}
MG48