简体   繁体   English

如何在Powershell中结合Get-ADComputer为LDAP查询创建例外列表

[英]How to create an exception list for LDAP query in Powershell combined with an Get-ADComputer

I have a script that searches all machines in a domain and is pulling details about them and presents them in a report. 我有一个脚本,可以搜索域中的所有计算机,并提取有关它们的详细信息并将其显示在报告中。

    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}

The script is skipping some machines based on their DN (distinguishedname) as it can be seen in the "# 1/2 is in Exceptions?" 该脚本正在根据其DN(可分辨名称)跳过某些计算机,如“#1/2是否在异常中?”所示。 and in "#2/2: isWin?" 并在“#2/2:isWin?”中

Meanwhile I got a request from a user to except some other (extra) machines that cannot be sorted using the initial query in the AD, which is: 同时,我收到了用户的请求,除了其他一些(额外的)计算机无法使用AD中的初始查询进行排序之外,其他请求是:

$AD_Results = Get-ADComputer -filter '(Enabled -eq $true)' -SearchScope Subtree -SearchBase $ADSearchBase -properties Description, PasswordNeverExpires, LastLogonTimeStamp, PasswordLastSet, operatingSystem, operatingSystemServicePack, whenCreated, distinguishedname, canonicalname

Basically the user wants to except from the report some specific machines (machine1, machine2, machine3) which are not real computer accounts but "connection points" for clustered resources. 基本上,用户希望从报告中排除一些特定的机器(机器1,机器2,机器3),这些机器不是真实的计算机帐户,而是群集资源的“连接点”。 Now, there are 2 ways to do that: 现在,有两种方法可以做到这一点:

  1. To use a script to find all these"connection points" for clustered resources. 使用脚本查找群集资源的所有这些“连接点”。 The only way to detect CNO and VCO is to look at "Service Principal name" attribute from the computer object. 检测CNO和VCO的唯一方法是查看计算机对象中的“服务主体名称”属性。 If you find "MSClusterVirtualServer" then the object is a CNO or a VCO 如果找到“ MSClusterVirtualServer”,则该对象是CNO或VCO

    Here is what I could come up with: 这是我能想到的:

     $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 
  2. To actually create a "whitelist" or "blacklist" where to include machines by names, assuming that in the future some other users might come up with similar requests to except machines from showing up in the report, and that these last machines might not be Virtual Clusters. 要实际创建一个“白名单”或“黑名单”,在其中按名称包括计算机,假设将来某些其他用户可能会提出与除报告中显示的计算机以外的其他计算机类似的请求,而这些最后的计算机可能不会虚拟集群。 I prefer this method. 我更喜欢这种方法。 What I did is to create an LDAP filter to find that 3 specific machines. 我要做的是创建一个LDAP过滤器来查找这3台特定的计算机。 Here it is: 这里是:

      (&(&(&(objectCategory=computer)(objectClass=computer)(|(cn=machine1)(cn=machine2)(cn=machine3))))) 

QUESTION: Can you help me put together an IF clause that will point towards a "whitelist" in csv format that will contain the names list of the machines to be excepted from the report? 问题:您能帮我组合一个IF子句,该子句指向csv格式的“白名单”,其中包含要从报告中排除的计算机的名称列表吗? The whitelist should reside on the same folder where the script is residing. 白名单应位于脚本所在的同一文件夹中。 Should I use the above LDAP filter? 我应该使用上面的LDAP过滤器吗? How do I do that? 我怎么做?

Based on your $AD_Result I would try something along these lines: 根据您的$AD_Result我将尝试以下方法:

ForEach ($exception In (Get-Content "exceptions.txt")) {
   $AD_Result = $AD_Result | ? { $_.Name -ine $exception }
}

Why did you want your exceptions file in csv format? 为什么要用csv格式的例外文件?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM