2

I Have an array called $a

echo $a gives:

url                                                               load                                                             
---                                                               ----                                                             
win-coll2.astest.org                                              9                                                                
win-coll.astest.org                                               110                                                              
win-coll4.astest.org                                              110                                                              
win-coll5.astest.org                                              140                                                              
win-coll3.astest.org                                              410 

now i want to know, if the array contains a sting eg: win-coll2.astest.org should lead to true and win-coll7.astest.org should lead to false

is there a proper way to do this without a foreach?

with foreach:

$IsInArray = false
$ProofString = "win-coll2.astest.org"
foreach( $item in $a){
  if($item.url -eq $ProofString){
    $IsInArray = true
  }
}

1 Answer 1

4

In powershell v4 (and possibly v3) you can do the following:

[PS] > $a.url -contains 'win-coll2.astest.org'
True
[PS] > $a.url -contains 'win-coll7.astest.org'
False

Otherwise, I think the easiest, least painful way is either do a foreach or a foreach-object...

3
  • since which WinServ is PS v4 supported? Is it available on 2008?
    – Joel
    Dec 19, 2014 at 15:38
  • I think you need R2, take a look at microsoft.com/en-us/download/details.aspx?id=40855 for more info about it. Dec 19, 2014 at 15:42
  • 1
    @Joel PoSh v4, through the Windows Management Framework v4 is available on Server 2008 R2, but not Server 2008. Dec 19, 2014 at 15:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .