powershell - 使用 LDAP 过滤器使用 Get-ADComputer 搜索多个操作系统

标签 powershell scripting active-directory

我正在使用下面的代码通过 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/

相关文章:

active-directory - Active Directory 是否支持 certificateExactMatch LDAP 匹配规则?

powershell - 发布 Web App 添加任务所需的代理配置

powershell - 为什么在 PowerShell 中 "for($i=1; $i -le 1000000; $i++){}"的执行时间比 "for([int]$i=1; $i -le 1000000; $i++){}"快

java - 在 LDAP 中搜索用户

c# - DirectoryEntry() 未通过身份验证

Python DataNitro - "CellRange set to object of wrong length"

java - 通过脚本在特定项目(eclipse)中查找 Java Class 引用

powershell - 如何使用 PowerShell 命令找出已安装的 .NET 框架版本?

java - Vaadin 不允许导入外部脚本

scripting - 构建模式下的 Roblox 玩家