无法通过计算机掩码在AD中进行搜索

时间:2015-04-02 11:19:04

标签: powershell

我想通过描述中的特定单词在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

如何使用变量进行搜索?

1 个答案:

答案 0 :(得分:1)

你可以这样做一个查询:

$username = "test111" 
Get-ADComputer -Filter "Description -Like '$username*'" -Properties Description | Select -Expand Description

我认为发生的事情是$username可能是$null,因为它没有传递给脚本块。将-Filter更改为使用引号可以使变量正确扩展。在那里扔了-Expand所以你只需要取回一个字符串数组而不是一个Object数组。