【发布时间】:2016-04-14 08:28:46
【问题描述】:
我在未连接到域、没有任何模块且运行 PS 2.0 的远程计算机上运行 Powershell。
我想联系我的域的 Active Directory,检查是否有此计算机的条目,并且;如果是,请删除该条目。
通过 ADSI 检查 AD 是否存在计算机很容易。但是,删除不起作用。
到目前为止,这是我的代码:
# Variables
$domain = "Test.com"
$Ldap = "LDAP://$domain"
$Global:AdsiSearcher = $Null
# Function to Delete PC
Function DeleteThisPc ()
{
$CurrentSearch = $Global:AdsiSearcher
$One = $CurrentSearch.FindOne()
$OPath = [adsi]$One.Path
$OPath.psbase.DeleteTree()
问题就在这里。尽管 $OPath 的类型是 System.DirectoryServices.DirectoryEntry 并且属性列表显示了所有属性,但它不允许我删除该对象。
使用“0”参数调用“DeleteTree”的异常:“登录失败: 未知用户名或错误密码。
在 C:\TEMP\Domjoin1.1.ps1:49 char:33 $OPath.psbase.DeleteTree
代码:
# Function to get a ADSISearcher and set it to the global-AdsiSearcher
Function ConnectAD ()
{
$domain = new-object DirectoryServices.DirectoryEntry($Ldap,"$domain\Bob",'1234')
$filter = "(&(objectCategory=computer)(objectClass=computer)(cn=$ComputerName))"
$AdsiSearch = [adsisearcher]""
$AdsiSearch.SearchRoot = $domain
$AdsiSearch.Filter = $filter
$Global:AdsiSearcher = $AdsiSearch
}
# Main Function
Function Sub_Check-ADComputer()
{
ConnectAD
$CurSearch = $Global:AdsiSearcher.findOne()
if($CurSearch -ne $null)
{
DeleteThisPc
}
}
# Start
Sub_Check-ADComputer
即使问题在错误状态下看起来很明显:
登录失败:未知用户名或密码错误。
用户名和密码与我最初用于从 AD 获取对象的用户名和密码相同。所以它确实有效 - 在尝试 deleteTree() 时,我是否必须再次提供凭据?我还为存储对象的 OU 提供了 User FullControl。
编辑:
当我在另一台使用 PS 3.0 的机器上执行此操作时,我收到不同的错误消息:
使用“0”参数调用“DeleteTree”的异常:“访问权限为 否认。 (来自 HRESULT 的异常:0x80070005 (E_ACCESSDENIED))"
【问题讨论】:
标签: powershell active-directory ldap adsi