Reputation: 118
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'"'
Upvotes: 4
Views: 5649
Reputation: 58961
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)
Upvotes: 3