我想通过描述中的特定单词在AD中找到计算机的描述。
$username = "test111"
Get-ADComputer -filter {Description -Like 'test111*'} -Properties Description | select Description # this works ok
Get-ADComputer -filter {Description -Like "$username*"} -Properties Description | select Description # shows nothing, no error
如何使用变量进行搜索?
答案 0 :(得分:1)
你可以这样做一个查询:
$username = "test111"
Get-ADComputer -Filter "Description -Like '$username*'" -Properties Description | Select -Expand Description
我认为发生的事情是$username
可能是$null
,因为它没有传递给脚本块。将-Filter
更改为使用引号可以使变量正确扩展。在那里扔了-Expand
所以你只需要取回一个字符串数组而不是一个Object数组。