如何结合 Get-ADComputer 在 Powershell 中为 LDAP 查询创建例外列表
How to create an exception list for LDAP query in Powershell combined with an Get-ADComputer
我有一个脚本可以搜索域中的所有机器并提取有关它们的详细信息并在报告中显示它们。
ipmo ActiveDirectory ;
$ADSearchBase = "DC=contoso,DC=chelu,DC=ro,DC=com"
write-host
write-host "Preparing your data..."
write-host
$AD_Results = Get-ADComputer -filter '(Enabled -eq $true)' -SearchScope Subtree -SearchBase $ADSearchBase -properties Description, PasswordNeverExpires, LastLogonTimeStamp, PasswordLastSet, operatingSystem, operatingSystemServicePack, whenCreated, distinguishedname, canonicalname
$count = $AD_Results.count
"Analyzing $count machines..."
# MAIN LOOP
ForEach ($Result In $AD_Results)
{
$i++
if ($i % 16 -eq 0) { $i }
$ComputerName=$result.name
$OS = $result.operatingSystem
$DESC = $result.Description
$DN = $result.distinguishedname
$PNE = $result.passwordneverexpires
if ($ComputerName.Length -ge 15)
{
$ComputerName = $ComputerName.substring(0,15)
}
## BEGIN TIME CONVERSIONS
$LLTS = 0 #AD LastLogonTimestamp
$PLS = 0 #AD PasswordLastSet
If ($result.lastLogonTimeStamp -eq $Null)
{
$T = [Int64]0
}
Else
{
$T = [DateTime]::FromFileTime([Int64]::Parse($result.lastlogontimestamp)).ToString("dd/MM/yyyy HH:mm:ss")
}
$LLTS = $T
$WCR = $result.whencreated.ToString("dd/MM/yyyy HH:mm:ss")
If (!($result.passWordLastSet -eq $Null))
{
$PLS = $result.passwordLastSet.ToString("dd/MM/yyyy HH:mm:ss")
}
## END TIME CONVERSIONS
# 1/2 is in Exceptions?
if ($DN -match "Domain Controllers") {"$computername : DOMAIN CONTROLLER -> Skipping..." ; $Skipped++ ; continue}
if ($DN -match "HVCL") {"$computername : Virtual Cluster Name -> Skipping..." ; $Skipped++ ; continue}
#2/2: isWin?
if ($result.operatingSystem -notlike '*windows*')
{
$Skipped++
continue
}
$isServer=0
if (($DN -match "Servers") -or ($result.operatingSystem -like '*server*'))
{$isServer=1}
脚本根据 DN(专有名称)跳过一些机器,如在
“# 1/2 在例外中?”在“#2/2: isWin?”
与此同时,我收到了用户的请求,要求排除一些无法使用 AD 中的初始查询进行排序的其他(额外)机器,即:
$AD_Results = Get-ADComputer -filter '(Enabled -eq $true)' -SearchScope Subtree -SearchBase $ADSearchBase -properties Description, PasswordNeverExpires, LastLogonTimeStamp, PasswordLastSet, operatingSystem, operatingSystemServicePack, whenCreated, distinguishedname, canonicalname
基本上,用户希望从报告中排除一些特定的机器(machine1、machine2、machine3),这些机器不是真实的计算机帐户,而是 "connection points" 集群资源。现在,有两种方法可以做到这一点:
使用脚本查找所有这些"connection points" 集群资源。检测 CNO 和 VCO 的唯一方法是查看计算机对象的 "Service Principal name" 属性。如果找到 "MSClusterVirtualServer",则该对象是 CNO 或 VCO
这是我能想到的:
$serviceType="MSClusterVirtualServer"
$spns = @{}
$filter = "(servicePrincipalName=$serviceType/*)"
$domain = New-Object System.DirectoryServices.DirectoryEntry
$searcher = New-Object System.DirectoryServices.DirectorySearcher
$searcher.SearchRoot = $domain
$searcher.PageSize = 1000
$searcher.Filter = $filter
$results = $searcher.FindAll()
foreach ($result in $results){
$account = $result.GetDirectoryEntry()
foreach ($spn in $account.servicePrincipalName.Value){
if($spn.contains("$serviceType/")){
$spns[$("$spn`t$($account.samAccountName)")]=1;
}
}
}
$spns.keys | sort-object
实际创建一个 "whitelist" 或 "blacklist" 在其中按名称包含机器,假设将来其他一些用户可能会提出类似的请求,除了机器来自出现在报告中,最后这些机器可能不是虚拟集群。我更喜欢这种方法。我所做的是创建一个 LDAP 过滤器来查找那 3 台特定的机器。这是:
(&(&(&(objectCategory=computer)(objectClass=computer)(|(cn=machine1)(cn=machine2)(cn=machine3)))))
问题:您能否帮我整理一个 IF 子句,该子句指向 csv 格式的 "whitelist",其中包含要从报告中排除的机器的名称列表?白名单应位于脚本所在的同一文件夹中。我应该使用上面的 LDAP 过滤器吗?我该怎么做?
根据您的 $AD_Result
,我会尝试以下方法:
ForEach ($exception In (Get-Content "exceptions.txt")) {
$AD_Result = $AD_Result | ? { $_.Name -ine $exception }
}
为什么您想要 csv 格式的异常文件?
我有一个脚本可以搜索域中的所有机器并提取有关它们的详细信息并在报告中显示它们。
ipmo ActiveDirectory ;
$ADSearchBase = "DC=contoso,DC=chelu,DC=ro,DC=com"
write-host
write-host "Preparing your data..."
write-host
$AD_Results = Get-ADComputer -filter '(Enabled -eq $true)' -SearchScope Subtree -SearchBase $ADSearchBase -properties Description, PasswordNeverExpires, LastLogonTimeStamp, PasswordLastSet, operatingSystem, operatingSystemServicePack, whenCreated, distinguishedname, canonicalname
$count = $AD_Results.count
"Analyzing $count machines..."
# MAIN LOOP
ForEach ($Result In $AD_Results)
{
$i++
if ($i % 16 -eq 0) { $i }
$ComputerName=$result.name
$OS = $result.operatingSystem
$DESC = $result.Description
$DN = $result.distinguishedname
$PNE = $result.passwordneverexpires
if ($ComputerName.Length -ge 15)
{
$ComputerName = $ComputerName.substring(0,15)
}
## BEGIN TIME CONVERSIONS
$LLTS = 0 #AD LastLogonTimestamp
$PLS = 0 #AD PasswordLastSet
If ($result.lastLogonTimeStamp -eq $Null)
{
$T = [Int64]0
}
Else
{
$T = [DateTime]::FromFileTime([Int64]::Parse($result.lastlogontimestamp)).ToString("dd/MM/yyyy HH:mm:ss")
}
$LLTS = $T
$WCR = $result.whencreated.ToString("dd/MM/yyyy HH:mm:ss")
If (!($result.passWordLastSet -eq $Null))
{
$PLS = $result.passwordLastSet.ToString("dd/MM/yyyy HH:mm:ss")
}
## END TIME CONVERSIONS
# 1/2 is in Exceptions?
if ($DN -match "Domain Controllers") {"$computername : DOMAIN CONTROLLER -> Skipping..." ; $Skipped++ ; continue}
if ($DN -match "HVCL") {"$computername : Virtual Cluster Name -> Skipping..." ; $Skipped++ ; continue}
#2/2: isWin?
if ($result.operatingSystem -notlike '*windows*')
{
$Skipped++
continue
}
$isServer=0
if (($DN -match "Servers") -or ($result.operatingSystem -like '*server*'))
{$isServer=1}
脚本根据 DN(专有名称)跳过一些机器,如在 “# 1/2 在例外中?”在“#2/2: isWin?”
与此同时,我收到了用户的请求,要求排除一些无法使用 AD 中的初始查询进行排序的其他(额外)机器,即:
$AD_Results = Get-ADComputer -filter '(Enabled -eq $true)' -SearchScope Subtree -SearchBase $ADSearchBase -properties Description, PasswordNeverExpires, LastLogonTimeStamp, PasswordLastSet, operatingSystem, operatingSystemServicePack, whenCreated, distinguishedname, canonicalname
基本上,用户希望从报告中排除一些特定的机器(machine1、machine2、machine3),这些机器不是真实的计算机帐户,而是 "connection points" 集群资源。现在,有两种方法可以做到这一点:
使用脚本查找所有这些"connection points" 集群资源。检测 CNO 和 VCO 的唯一方法是查看计算机对象的 "Service Principal name" 属性。如果找到 "MSClusterVirtualServer",则该对象是 CNO 或 VCO
这是我能想到的:
$serviceType="MSClusterVirtualServer" $spns = @{} $filter = "(servicePrincipalName=$serviceType/*)" $domain = New-Object System.DirectoryServices.DirectoryEntry $searcher = New-Object System.DirectoryServices.DirectorySearcher $searcher.SearchRoot = $domain $searcher.PageSize = 1000 $searcher.Filter = $filter $results = $searcher.FindAll() foreach ($result in $results){ $account = $result.GetDirectoryEntry() foreach ($spn in $account.servicePrincipalName.Value){ if($spn.contains("$serviceType/")){ $spns[$("$spn`t$($account.samAccountName)")]=1; } } } $spns.keys | sort-object
实际创建一个 "whitelist" 或 "blacklist" 在其中按名称包含机器,假设将来其他一些用户可能会提出类似的请求,除了机器来自出现在报告中,最后这些机器可能不是虚拟集群。我更喜欢这种方法。我所做的是创建一个 LDAP 过滤器来查找那 3 台特定的机器。这是:
(&(&(&(objectCategory=computer)(objectClass=computer)(|(cn=machine1)(cn=machine2)(cn=machine3)))))
问题:您能否帮我整理一个 IF 子句,该子句指向 csv 格式的 "whitelist",其中包含要从报告中排除的机器的名称列表?白名单应位于脚本所在的同一文件夹中。我应该使用上面的 LDAP 过滤器吗?我该怎么做?
根据您的 $AD_Result
,我会尝试以下方法:
ForEach ($exception In (Get-Content "exceptions.txt")) {
$AD_Result = $AD_Result | ? { $_.Name -ine $exception }
}
为什么您想要 csv 格式的异常文件?