Solved

Powershell -contains

Posted on 2010-08-18
8
6,002 Views
Last Modified: 2013-11-08
I am struggling to figure out why the -contains options when searching through an array works for one array and not for another?

I'm trying to use the neat method here: http://technet.microsoft.com/en-us/library/ee692798.aspx

The Code:

$arrColours = "Black","Blue","Green"
if ($arrColours -contains "Black"){Write-Host "True"}

$arrServices = Get-Service | Select-Object name

$arrservices[0] #show first element

if ($arrServices -contains "Alerter"){Write-Host "True"}else{Write-Host "False"}

Open in new window


The results:

True

Name                                                                                          
----                                                                                          
Alerter  
                                                                                   
False


So, it finds the colour, but when I try to load services, and see if a certain service exists in the array it does not work?

I think it has something to do with the type of array, but I don't know how to convert the array to an array of strings?

Any help would be great,

Thanks

Shaun
0
Comment
Question by:shauncroucher
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 16

Expert Comment

by:Bryan Butler
ID: 33466617
I'm not sure what's up with that, but how about this:

$arrColours = "Black","Blue","Green"
if ($arrColours -contains "Black"){Write-Host "True"}
$arrServices = Get-Service | Select-Object name
$arrservices[0] #show first element
if ($arrServices |?{$_.name -eq "Alerter"){Write-Host "True"}else{Write-Host "False"}
 
0
 
LVL 27

Author Comment

by:shauncroucher
ID: 33466770
Hi,

Thanks for that, I did test that method and it works but I would prefer not to pipe to where-object if possible, I liked the neatness (and probable efficiency) that the -contains uses.

If anyone know why it doesn't seem to work with an object array, that would be good.

Thanks

Shaun
0
 
LVL 16

Expert Comment

by:Bryan Butler
ID: 33467970
I think you have to use an array of strings with the -contains, so you will have to pipe/loop again to change it somehow as you have an array of object.

You could use "out-string" to convert to one long string and then search it which returns True/False:
(Get-Service | Select-Object name | out-string).contains("serviceName")
0
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 
LVL 71

Accepted Solution

by:
Chris Dent earned 500 total points
ID: 33472848

It's because $arrServices is an array of objects and you can't implicitly test the name property.

You can get around that when generating the list, returning only the property value. Then you can use contains. Otherwise you're stuck with Where-Object.

Chris
$arrServices = Get-Service | ForEach-Object { $_.Name }

$arrServices[0] # This is a String

if ($arrServices -contains "Alerter"){Write-Host "True"}else{Write-Host "False"}

Open in new window

0
 
LVL 27

Author Comment

by:shauncroucher
ID: 33474106
Thanks developedtester for your help,

Chris, that's exactly what I was after, thank you. So what is happening here is that the $arrServices variable is loaded with the name property of each Get-Service object, and so holds an array of strings that I can work with.

It makes perfect sense when I look at it like that.

Do you think this would be any more efficient that using the where-object?

Thanks Chris

Shaun
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 33474179

Where-Object is more flexible (because it can deal with the members of an object), but it depends on your aim really. I doubt you'll notice any difference for such a small operation.

Chris
0
 
LVL 13

Expert Comment

by:soostibi
ID: 33474204
Or this is even simpler. With 'select-object -expandproperty' you "grabs" the property to one level higher, so it is now 'the' object, not just a property.

$arrServices = Get-Service | Select-Object -expandproperty name  
if ($arrServices -contains "Alerter"){Write-Host "True"}else{Write-Host "False"}

Open in new window

0
 
LVL 27

Author Comment

by:shauncroucher
ID: 33496203
Chris, you are a master, thanks

Shaun
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question