[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6173
  • Last Modified:

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
0
shauncroucher
Asked:
shauncroucher
  • 3
  • 2
  • 2
  • +1
1 Solution
 
Bryan ButlerCommented:
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
 
shauncroucherAuthor Commented:
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
 
Bryan ButlerCommented:
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
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
Chris DentPowerShell DeveloperCommented:

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
 
shauncroucherAuthor Commented:
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
 
Chris DentPowerShell DeveloperCommented:

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
 
soostibiCommented:
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
 
shauncroucherAuthor Commented:
Chris, you are a master, thanks

Shaun
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now