【发布时间】:2014-08-11 01:03:53
【问题描述】:
我正在尝试编写可以从给定域中检索 OU 列表以进行进一步处理的 powershell 代码。我只想要一个纯文本列表(出于各种原因)。我无法在这些机器上安装 AD 模块。
$tld = "lan"
$domain = "contoso"
$adLogin = "Administrator"
$mainOU = "Workstations"
$dnSuffix = "OU=$mainOU,DC=$domain,DC=$tld"
$computerPrefix = ""
$computerSuffix = ""
$DomainDN = "LDAP://$domain.$tld"
$adDomain = "$domain" + "." + "$tld"
$adAuthName = "$adDomain\$adLogin"
$Credential = Get-Credential -Credential $adAuthName
# There's probably a better way to do this, but the adsi searcher documentation is hard to sift through, and these machines can't install the AD module.
$remoteObject = New-Object -TypeName System.DirectoryServices.DirectoryEntry -ArgumentList $DomainDN,$($Credential.UserName),$($Credential.GetNetworkCredential().password)
$adObj = ([ADSISearcher]"ObjectClass=OrganizationalUnit")
$adObj.SearchRoot = $remoteObject
$adObj.PropertiesToLoad.AddRange("CanonicalName")
$ouList = $adObj.findall().Properties.canonicalname | Select-String $mainOU | Out-String
$ouList = $ouList.ToString()
$removeText = "$domain" + "." + "$tld" + "/" + "$mainOU"
$ouList = $ouList -replace $removeText
$ouList = $ouList -replace "/"
$ouList = $ouList.Trim()
$ouList = $ouList.Split("`n")
echo $ouList
在目标域内的计算机上运行它会产生以下结果:(这些是用于实验室模拟的虚假 OU。)
chips
vinegar
salsa
但是在域外的计算机上运行它不会产生任何结果。 Powershell 似乎没有提供任何工具来解决原因。是的,我已经验证我可以从那台机器上解析 contoso.lan。有人有什么建议吗?
【问题讨论】:
-
我将其复制粘贴到我的家用计算机(未加入域)上,该计算机通过 VPN 连接到 AD 网络。一旦我将
$tld、$domain、$adLogin和$mainOU更改为与该域相对应的值,它就完美地工作了(太棒了)。我想不出它不起作用的原因。 -
我的设置可能有点奇怪。我真的不确定,但我确实找到了一个更优雅的解决方案来解决我之前的问题,它也有效。我将把它作为答案发布。
-
您可能遇到了安全限制。我存在用于禁止域外计算机进行身份验证的策略。
-
我在实验室环境中对其进行了测试,该环境只是全新安装的 Windows Server 2012 R2 Datacenter 版本,仅提升到名为 contoso.lan 的域。不知道为什么它不起作用。我展示的代码正是我使用的(域名、tld 等)。
标签: .net powershell active-directory