4

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

2 Answers 2

3

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)
Sign up to request clarification or add additional context in comments.

1 Comment

You are welcome. You can also use double qutoes like Chard mentioned in his answer.
2
Get-ADComputer -Filter "Name -like '*$name'"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.