Link to home
Start Free TrialLog in
Avatar of shauncroucher
shauncroucherFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Powershell -contains

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
Avatar of Bryan Butler
Bryan Butler
Flag of United States of America image

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"}
 
Avatar of shauncroucher

ASKER

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
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")
ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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

Chris, you are a master, thanks

Shaun