0
$server = Get-ADComputer -SearchBase "a OU in a domain*" -Filter {Name -like "*79234*"} | Select -Property *

This work it pulls the computer object like I want. A computer object with those numbers in the name.

In previous code I define a

$array[$where] = 79234 
$server = Get-ADComputer -SearchBase "a OU in a domain*" -Filter {Name -like "*$($array[$where])*"} | Select -Property *

Fails and does not give me my match. Why?

"$($array[$where])" = 79234 in the commandline..

I did use work around it by create the object name I'd searching for before I search

$FilterName = "*$($Array[$Where])*"

$server = Get-ADComputer -SearchBase "a OU in a domain*" -Filter {Name -like $filterObjectname} | Select -Property *

Gives me the correct object, I think the root issue is they way the values are past between the objects (casting?) but I'm missing it.

Minerbob
  • 447
  • 6
  • 14
  • This question already has an answer :https://stackoverflow.com/questions/20075502/get-aduser-filter-will-not-accept-a-variable It has a better explanation of anything I could come up with - but I think it has to do with the concept of expanding the value of the variable in the string. – Minerbob Jun 02 '18 at 15:32

1 Answers1

0

take a look at this tutorial with arrays. you can not define an array element in powershell like you did.

you can create a hashtable like so:

$array = @{}

and then add elements like:

$array.asdf = 'qwert'

or

$array.$where = 'value'

so for your sample code I would suggest

$array = @{}
$array.$where = 79234
$FilterName = $array.$where
Guenther Schmitz
  • 1,955
  • 1
  • 9
  • 23