我正在使用下面的代码通过 Get-ADComputer
获取 Active Directory 信息,它工作正常
$computersFilter= "(&(operatingSystem=*Windows 7*)(name=*-*)(!name=V7-*)(!name=*-none)(!name=*-oncall)(!name=*-blackbaud)(!name=sc-win7-1)(!name=ut-swclient-01))"
$computers= Get-ADComputer -LDAPFilter $computersFilter -Property LastLogonDate | Select-Object Name, OperatingSystem,LastLogonDate
$computers | Select Name, LastlogonDate, OperatingSystem | Export-Csv $ServiceTagsPath -NoTypeInformation
我还想检索操作系统高于 Windows 7(Windows 8、8.1 和 Windows 10)的计算机,但是当我像这样更改过滤器时:
(&(operatingSystem=*Windows 7*)(operatingSystem=*Windows 8*)(operatingSystem=*Windows 10*) ...
$computers
变量没有返回任何内容
那么正确的做法是什么?
最佳答案
您定义 $computersFilter
的方式,如果您查看它的类型,您会注意到它是 string
类型。因此它不返回任何东西。
PS C> $computersFilter = "(&(operatingSystem=*Windows7*))(name=*-*)(!name=V7-*)(!name=*-none)(!name=*-oncall)(!name=*-blackbaud)(!name=sc-win7-1)(!name=ut-swclient-01))"
PS C> $computersFilter.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
$computersFilter
应声明为数组,以便根据对象集合评估 -LDAPFilter
参数,如下所示 -
PS C>$computersFilter = @("operatingSystem=*Windows7*", "name=*-*", "!name=V7-*", "!name=*-none", "!name=*-oncall", "!name=*-blackbaud", "!name=sc-win7-1", "!name=ut-swclient-01")
PS C> $computersFilter.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
关于powershell - 使用 LDAP 过滤器使用 Get-ADComputer 搜索多个操作系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49895088/