简体   繁体   English

如何在Get-ADComputer中将用户的输入变量用作搜索查询

[英]How to use a user's input variable as a search query in Get-ADComputer

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. 此PowerShell脚本的目的是向用户查询域中现有计算机的部分名称。 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. 查询完成后,它将从活动目录中的特定OU检索完整的计算机名,并将该完整的计算机名复制到用户剪贴板。 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. 注:我敢肯定,这个问题是不是与这两个“GET-ADComputer”线,因为如果我手动它的工作原理完全如预期,输入脚本的完整计算机名称。 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. 问题似乎与我如何定义用户输入或如何将其传递到“ Get-ADComputer” cmdlet内的变量($ PCName)有关。

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. 这是完整的脚本,我唯一省略的是特定的活动目录OU-我知道它在正确的OU中查找,因为单独输入并手动输入PC名称的行效果很好。

$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. 我使用PowerShell不到一个星期,总体上是一名新程序员。 I've performed no less than 40 google queries and spent at least 3 hours trying to get this to work. 我已经执行了不少于40个Google查询,并花费了至少3个小时来使它正常工作。

Thank you! 谢谢!

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 )

Your main issue is how you were quoting your -Filter . 您的主要问题是如何引用-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. 您的查询正在寻找一台与变量内容匹配的字符串文字$pcname的计算机。

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. 将所有Write-Output更改为Write-Host因为这就是您对待它们的方式。 If nothing else you have them mixed together so picking one would be more my point. 如果没有其他问题,您将它们混在一起,那么选择一个将是我的重点。

In PowerShell, single and double quotes each have a different meaning and significance. 在PowerShell中,单引号和双引号分别具有不同的含义和意义。 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. 在此字符串中,$ PCName不会替换为其值。 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 此外,您应该删除return语句,并且在您的示例中无需创建全局变量$ global:PCName,可以改用$ PCName。

I had a similar issue with filter (building into an ASP application) and solved it by using curly brackets. 我在过滤器(内置到ASP应用程序)中也遇到了类似的问题,并使用大括号解决了该问题。

$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. 在此特定情况下,多余的$()很可能是不必要的,因为我们对变量不做任何事情,但这是我的习惯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM