Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get-ADComputer -Filter name not taking variable for computer search

Tags:

powershell

I've tried to create a powershell script which allows me to find the correct computer by it's service tag in AD. So far I could do it by writing the service tag in the script, but when I wanted to pass it with variable, it doesn't let me do it, and shows this error:

Get-ADComputer : A positional parameter cannot be found that accepts argument 'GCX0YY1"'. At line:2 char:1 + Get-ADComputer -Filter 'Name -like "*'$name'"'

$name = Read-Host 'Write the computers service tag'
Get-ADComputer -Filter 'Name -like "*'$name'"'
like image 469
Maraudingas Avatar asked Sep 03 '25 02:09

Maraudingas


1 Answers

The $name variable doesn't get resolved if you use single quotes on your string. You could use a string format (alias -f) to get your variable in place:

$name = Read-Host 'Write the computers service tag'
Get-ADComputer -Filter ('Name -like "{0}"' -f $name)
like image 138
Martin Brandl Avatar answered Sep 05 '25 23:09

Martin Brandl