我有一个脚本,搜索一个域中的所有机器,并提取有关它们的详细信息,并将它们呈现在报告中。
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在Exceptions?"one_answers"#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),它们不是真正的计算机帐户,而是集群资源的"连接点"。现在,有两种方法可以做到:
使用脚本查找集群资源的所有这些"连接点"。检测CNO和VCO的唯一方法是查看计算机对象中的"服务主体名称"属性。如果您发现"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
实际创建一个"白名单"或"黑名单",其中按名称包括机器,假设将来一些其他用户可能会提出类似的请求,以排除出现在报告中的机器,并且这些最后的机器可能不是虚拟集群。我更喜欢这种方法。我所做的是创建一个LDAP过滤器来查找这3台特定的机器。
(&(&(&(objectCategory=computer)(objectClass=computer)(|(cn=machine1)(cn=machine2)(cn=machine3)))))
问题:你能帮我把IF子句放在一起,指向csv格式的"白名单"吗?该白名单将包含报告中不包括的机器的名单。白名单应该位于脚本所在的文件夹中。我应该使用上面的LDAP过滤器吗?我怎么做呢?
根据您的$AD_Result
,我会尝试以下内容:
ForEach ($exception In (Get-Content "exceptions.txt")) {
$AD_Result = $AD_Result | ? { $_.Name -ine $exception }
}
为什么你想要你的例外文件在csv格式?