简体   繁体   English

将 ADSI 输出管道传输到另一个用于查找域计算机的 powershell 脚本

[英]piping ADSI output into another powershell script for finding domain computers

I have moved to a company that has legacy servers and no RSAT so I cannot use AD module and have to make sure the script is v2 compliant.我已搬到一家拥有旧服务器且没有 RSAT 的公司,因此我无法使用 AD 模块,并且必须确保脚本符合 v2 标准。

I have written a discovery script to find all the domains in the forest and I am trying to pipe it into another script to findout all the dns hostnames of the computers in the domains that I have access too but keep getting various errors.我编写了一个发现脚本来查找林中的所有域,并且我试图将其通过管道传输到另一个脚本中,以查找我也可以访问的域中计算机的所有 dns 主机名,但不断收到各种错误。

Domains In Forest Code:森林代码中的域:

$Root = [ADSI]"LDAP://RootDSE"
$oForestConfig = $Root.Get("configurationNamingContext")
$oSearchRoot = [ADSI]("LDAP://CN=Partitions," +     $oForestConfig)
$AdSearcher = [adsisearcher]"(&(objectcategory=crossref)    (netbiosname=*))"
$AdSearcher.SearchRoot = $oSearchRoot
$Domains = $AdSearcher.FindAll()
return $Domains|$Domains = "dc=" + $Domains.Name.Replace(".",     ",dc=") |Out-File C:\domains.txt

Code to find dns host names:查找 dns 主机名的代码:

$doms = Get-Content C:\domains.txt
foreach ($dom in $doms) {
$AD = (([adsisearcher]"").Searchroot.path)
IF ($AD -notlike "LDAP://*") {$AD ="LDAP://$AD"}
$AD.Filter = "(&(objectCategory=Computer)(name=$item))"
$Computers = $AD.Filter.FindAll()
$ComputerNames = $Computers.Properties.dnshostname
}

Any ideas?有任何想法吗?

You can use all the .NET classes in PowerShell, which can make things easier here.您可以在 PowerShell 中使用所有 .NET 类,这可以使这里的事情变得更容易。 In fact, [adsi] and [adsisearcher] are "type accelerators" for the DirectoryEntry and DirectorySearcher classes.事实上, [adsi][adsisearcher]DirectoryEntryDirectorySearcher类的“类型加速器”。

For this use case, you can use Forest.GetCurrentForest() to find the forest and read all the domains in the forest.对于此用例,您可以使用Forest.GetCurrentForest()查找林并读取林中的所有域。 Then, for each domain, find all the computers.然后,对于每个域,查找所有计算机。

Here is an example of how you would do that.以下是您将如何执行此操作的示例。 It's probably not going to be formatted the way you want, but you can change that how you want.它可能不会按照您想要的方式进行格式化,但您可以根据需要进行更改。

$forest = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()

foreach ($domain in $forest.Domains) {
    $domain.Name
    $searcher = [adsisearcher]"(objectCategory=Computer)"
    $searcher.SearchRoot = [adsi]"LDAP://$($domain.Name)"
    $searcher.PropertiesToLoad.Add("dNSHostName") | Out-Null

    foreach ($comp in $searcher.FindAll()) {
        $comp.Properties["dnsHostName"][0]
    }
}

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

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