如何在 Get-ADComputer 中使用用户的输入变量作为搜索查询
How to use a user's input variable as a search query in Get-ADComputer
感谢您访问我在本网站上的第一个问题。
此 PowerShell 脚本的目的是向用户查询域中已有计算机的部分名称。查询完成后,它会从活动目录中的特定 OU 中检索完整的计算机名称,并将此完整的计算机名称复制到用户的剪贴板。这是为了帮助我在帮助台工作的人(包括我自己)节省时间,他们每天都必须手动执行此操作。
注意:我很确定问题不在这两行 'Get-ADComputer' 上,因为如果我在脚本中手动输入完整的计算机名称,它会 完全正常 如预期。问题似乎是我如何定义用户输入,或者它是如何传递给 'Get-ADComputer' cmdlet 中的变量 ($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 google 次查询,并花费了至少 3 个小时来尝试使其正常工作。
谢谢!
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 )
您的主要问题是您如何引用 -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
,因为这就是您对待它们的方式。如果没有别的,你把它们混合在一起,所以选择一个更符合我的观点。
在PowerShell中,单引号和双引号各有不同的含义和意义。变量只会用双引号展开。
您的查询无效,因为您对参数使用了单引号:
-Filter 'Name -like "*$PCName*"'
在此字符串中,$PCName 不会被其值替换。双引号在这里并不重要,因为在单引号字符串中,它们只是字符。
您可以这样构建参数:
-Filter ('Name -like "*' + $PCName + '*"')
此外,您应该删除 return 语句,并且在您的示例中无需创建全局变量 $global:PCName,您可以改用 $PCName
我在过滤器(构建到 ASP 应用程序中)遇到了类似的问题,并通过使用大括号解决了它。
$searchterm = "*$($PCName)*"
-Filter {Name -like $searchterm}
在这个特定的实例中,额外的 $() 很可能是不必要的,因为我们没有对变量做任何事情,但这是我现在的习惯。
感谢您访问我在本网站上的第一个问题。
此 PowerShell 脚本的目的是向用户查询域中已有计算机的部分名称。查询完成后,它会从活动目录中的特定 OU 中检索完整的计算机名称,并将此完整的计算机名称复制到用户的剪贴板。这是为了帮助我在帮助台工作的人(包括我自己)节省时间,他们每天都必须手动执行此操作。
注意:我很确定问题不在这两行 'Get-ADComputer' 上,因为如果我在脚本中手动输入完整的计算机名称,它会 完全正常 如预期。问题似乎是我如何定义用户输入,或者它是如何传递给 'Get-ADComputer' cmdlet 中的变量 ($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 google 次查询,并花费了至少 3 个小时来尝试使其正常工作。
谢谢!
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 )
您的主要问题是您如何引用 -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
,因为这就是您对待它们的方式。如果没有别的,你把它们混合在一起,所以选择一个更符合我的观点。
在PowerShell中,单引号和双引号各有不同的含义和意义。变量只会用双引号展开。
您的查询无效,因为您对参数使用了单引号:
-Filter 'Name -like "*$PCName*"'
在此字符串中,$PCName 不会被其值替换。双引号在这里并不重要,因为在单引号字符串中,它们只是字符。
您可以这样构建参数:
-Filter ('Name -like "*' + $PCName + '*"')
此外,您应该删除 return 语句,并且在您的示例中无需创建全局变量 $global:PCName,您可以改用 $PCName
我在过滤器(构建到 ASP 应用程序中)遇到了类似的问题,并通过使用大括号解决了它。
$searchterm = "*$($PCName)*"
-Filter {Name -like $searchterm}
在这个特定的实例中,额外的 $() 很可能是不必要的,因为我们没有对变量做任何事情,但这是我现在的习惯。