Solved

Powershell -contains

Posted on 2010-08-18
8
5,907 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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
 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Send SMS from VB via Sierra Wireless Modem 2 22
No Data for DropDown List 2 25
Call Controller Action Method from ASPX 2 15
Get processes with highest usage 2 21
Synchronize a new Active Directory domain with an existing Office 365 tenant
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

777 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