0

thank you for visiting my first question on this website.

The purpose of this PowerShell script is to query the user for a partial name of a pre-existing computer on the domain. After the query is complete it retrieves the full computer name from a specific OU in active directory, and copies this full computer's name it to the user's clipboard. This is to help save time to the people I work with at the help desk(including myself) who have to perform this action manually every day.

Note:I'm pretty sure the problem isn't with the two 'Get-ADComputer' lines because if I manually enter the full computer name in the script it works exactly as intended. The issue seems to be either with how I'm defining the user input, or how it's being passed along to the variable($PCName) inside of the 'Get-ADComputer' cmdlet.

Here is the script in its entirety, the only thing I omitted is the specific active directory OU - I know it's looking in the right OU, because the lines taken individually and with a manually PC Name entered work great.

$global:PCName=(Read-Host "Enter partial PC name")
write-host "You entered the partial PC Name: $PCName"
return $PCName

#PCName Information Table Display.
Get-ADComputer -SearchBase 'OU=(Disregard)' -Filter 'Name -like "*$PCName*"' -Properties IPv4Address | Format-Table Name,DNSHostName,IPv4Address -A

#Progress indicator advisory message.
Write-Output "Converting $PCname to full computer name and copying result to your clipboard."

#Clip Line - Retrieves full PC name and copies resolved PC name to clipboard.
Get-ADComputer -SearchBase 'OU=(Disregard)' -Filter 'Name -like "*$PCName*"' | Select Name -ExpandProperty Name | Clip

#End of script advisory message.
Write-Output "Full PC Name:$PCName - Resolved and copied to clipboard."

If there's any other fault to be pointed out, I would appreciate it. I have been using PowerShell for less than a week and am a new programmer overall. I've performed no less than 40 google queries and spent at least 3 hours trying to get this to work.

Thank you!

1
  • The $PCName variable is not getting expanded out in your calls to Get-ADComputer. You have the filter string within single quotes which tells powershell to take everything literally (no variable expansion). Commented Mar 16, 2015 at 19:28

4 Answers 4

1
do {
  $computerName = read-host "Enter partial computer name [blank=quit]"
  if ( -not $computerName ) {
    break
  }
  $sb = [ScriptBlock]::Create("name -like '*$computerName*'")
  $computer = get-adcomputer -filter $sb
  if ( $computer ) {
    $computer
    $computer | select-object -expandproperty Name | clip
    "Copied name to clipboard"
  }
  else {
    "Not found"
  }
  ""
}
while ( $true )
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Bill. I will review and try to understand more what's going on here. I am still learning PowerShell from a very basic perspective at the moment, and was hoping to get an answer that closely resembled mine in order to see where I went wrong(and to see the big picture).
0

In PowerShell, single and double quotes each have a different meaning and significance. Variables will only be expanded in double quotes.

Your query does not work because you use single quotes for the parameter:

-Filter 'Name -like "*$PCName*"'

In this string, $PCName will not be replaced by its value. The double quotes are not significant here, because inside a single quoted string, they are just characters.

You can build the parameter like this:

-Filter ('Name -like "*' + $PCName + '*"')

Additionally, you should remove the return statement and in your example there is no need to create a global variable $global:PCName, you can use $PCName instead

4 Comments

Hello Thomas, thank you for answering. This makes sense to me, and I have removed the unnecessary global variable. I have replaced my filter command in both cmdlets to exactly yours, but it throws up an error. "Get-ADComputer : A positional parameter cannot be found that accepts argument '+'." The line, now in full shows ... Get-ADComputer -SearchBase 'OU=(disregard)' -Filter 'Name -like "' + $PCName + '"' -Properties IPv4Address | Format-Table Name,DNSHostName,IPv4Address -A
Ok, my mistake, the filter argument must be enclosed in parentheses, like in my updated example.
Thomas, Your answer worked perfectly after the revision!! I replaced your filter argument with mine and the script does exactly what I wanted. Thank you for taking the time to help me, I really appreciate it. I wish I could vote your answer but it says I need 15 reputation in order to do so.
Consider it done - just noticed the acceptance button. :) Cheers! I'm now building a GUI around the simple script.
0

Your main issue is how you were quoting your -Filter. Variables do not expand inside single quotes. Your query was looking for a computer matching the string literal $pcname as supposed to the variable contents.

Also you make the same call twice which is inefficient. You should also know that it is possible to have more than one match with this so you need to be aware of/ account for that possibility.

$PCName=(Read-Host "Enter partial PC name")
write-host "You entered the partial PC Name: $PCName"

#PCName Information Table Display.
$results = Get-ADComputer -SearchBase 'OU=(Disregard)' -Filter "Name -like '*$pcname*'" -Properties IPv4Address 
$results | Format-Table Name,DNSHostName,IPv4Address -A

#Progress indicator advisory message.
Write-host "Converting $PCname to full computer name and copying result to your clipboard."

#Clip Line - Retrieves full PC name and copies resolved PC name to clipboard.
$results| Select -ExpandProperty Name | Clip

#End of script advisory message.
Write-host "Full PC Name:$PCName - Resolved and copied to clipboard."

I don't see a need for a global variable here so I removed it. Changed all the Write-Output to Write-Host as that is how you were treating them. If nothing else you have them mixed together so picking one would be more my point.

2 Comments

Thank you for providing code - I will need to study this some more before I understand as it varies from my code quite a bit. The reason I was using Write-output is because I read this article : jsnover.com/blog/2013/12/07/write-host-considered-harmful
@Tiki As long as you understand the difference between Write-Output and Write-Host I would not consider -Host harmful especially if you are using simply to convey a message. Which is what you are doing now. To each his own. Again, I only made those changes since you were using both and I wanted it to be consistent.
0

I had a similar issue with filter (building into an ASP application) and solved it by using curly brackets.

$searchterm = "*$($PCName)*"
-Filter {Name -like $searchterm}

The extra $() is most likely unnecessary in this particular instance as we aren't doing anything with the variable, but it's a habit of mine now.

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.