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

时间:2015-03-16 19:08:33

标签: powershell powershell-v4.0

感谢您访问本网站的第一个问题。

此PowerShell脚本的目的是向用户查询域中预先存在的计算机的部分名称。查询完成后,它将从活动目录中的特定OU中检索完整的计算机名称,并将此完整计算机的名称复制到用户的剪贴板中。这是为了帮助我在每天必须手动执行此操作的服务台(包括我自己)工作的人员节省时间。

注意:我非常确定问题不在于两个“Get-ADComputer”'行,因为如果我在脚本中手动输入完整的计算机名称,它将按预期完全。问题似乎在于我如何定义用户输入,或者如何将其传递给' Get-ADComputer'内部的变量($ PCName)。小命令。

这是完整的脚本,我唯一省略的是特定的活动目录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."

如果有任何其他错误需要指出,我将不胜感激。我使用PowerShell的时间不到一周,而且我是一名全新的程序员。我执行了不少于40次谷歌查询,并花了至少3个小时试图让这个工作。

谢谢!

4 个答案:

答案 0 :(得分:0)

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 )

答案 1 :(得分:0)

您的主要问题是如何引用-Filter。变量不会在单引号内扩展。您的查询正在查找与字符串文字$pcname匹配的计算机,该计算机应与变量内容相匹配。

同样,你进行两次相同的调用,效率很低。您还应该知道可能有多个匹配,因此您需要了解/说明这种可能性。

$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."

我不认为这里需要一个全局变量,所以我删除了它。将所有Write-Output更改为Write-Host,因为这就是您对待它们的方式。如果没有其他你把它们混合在一起所以选择一个将更多我的观点。

答案 2 :(得分:0)

在PowerShell中,单引号和双引号各有不同的含义和含义。变量只能用双引号扩展。

您的查询不起作用,因为您对参数使用单引号:

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

在此字符串中,$ PCName不会被其值替换。双引号在这里并不重要,因为在单引号字符串中,它们只是字符。

您可以像这样构建参数:

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

此外,您应该删除return语句,并且在您的示例中不需要创建全局变量$ global:PCName,您可以使用$ PCName而不是

答案 3 :(得分:0)

我在过滤器(构建到ASP应用程序中)中遇到了类似的问题,并使用大括号解决了这个问题。

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

额外的$()在这个特定的实例中很可能是不必要的,因为我们没有对变量做任何事情,但现在这是我的习惯。