Powershell 使用Get-ADForest或([System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()筛选特定域
尝试使用get-adforest或.net命令筛选包含特定字符串(“sa*”)的域。最后,域列表将用于填充表单中的组合框Powershell 使用Get-ADForest或([System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()筛选特定域,powershell,active-directory,Powershell,Active Directory,尝试使用get-adforest或.net命令筛选包含特定字符串(“sa*”)的域。最后,域列表将用于填充表单中的组合框 $DomainList = ($Forest.Domains).name | sort -CaseSensitive foreach ($Domain in $Domainlist){if ($Domain -like "sa*"){$Domains = $Domain}} or get-adforest | where-object {$_.Domains -like
$DomainList = ($Forest.Domains).name | sort -CaseSensitive
foreach ($Domain in $Domainlist){if ($Domain -like "sa*"){$Domains = $Domain}}
or
get-adforest | where-object {$_.Domains -like "sa*"}
期望结果仅包括以sa开头的域
sam.domain.com
sandy.domain.com
等等
相反,会出现多个错误或根本没有结果。两次尝试都很接近 在第一次尝试中,您使用的是
($Forest.Domains).name
,它试图从不存在的域列表中读取name
属性。相反,您需要域列表本身:
$DomainList = (Get-ADForest).Domains | sort -CaseSensitive
foreach ($Domain in $Domainlist) { if ($Domain -like "sa*") { $Domains = $Domain } }
在第二次尝试中,您将来自Get ADForest
的结果管道化到Where对象中,因此结果包括符合条件的任何林,而不是符合条件的任何域。因此,要获得域的筛选列表,您需要将域列表管道化到Where对象中
(Get-ADForest).Domains | Where-Object {$_ -like "sa*"}
请注意,(Get-ADForest)。域
只会给您一个包含域DNS名称的字符串列表。您确实提到了使用。其好处是您可以获得有关每个域的更多信息(如域控制器列表)
如果这对您很重要,您可以获得符合以下条件的对象列表:
[System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().Domains |
Where-Object {$_.Name -like "sa*"}
这应该是可行的:Get-ADForest | Where对象{$\.Domains-like“*.com”}
作为一个例子。如果它不起作用并且您收到一个错误,请更新OP。如果您没有得到任何结果,只需运行Get-ADForest
,并确保Domains
填充了您所期望的内容。谢谢@Gabriel Luci(Get ADForest).Domains |其中对象{$u-like“sa*”}提供的正是我想要的。第一个域只填充一个域,第二个域提供的比我现在需要的多一些。现在,我只是在寻找域列表,以便在表单加载时填充一个组合框。