Solved

Powershell -contains

Posted on 2010-08-18
8
5,844 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
  • 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
 
LVL 70

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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 70

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
A procedure for exporting installed hotfix details of remote computers using powershell
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

705 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now