【问题标题】:How to remotely delete an AD-Computer from Active Directory - Powershell如何从 Active Directory 中远程删除 AD 计算机 - Powershell
【发布时间】: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


    【解决方案1】:

    我发现了问题。

    当使用invoke 命令时,除非-argumentlist 指定,否则不会传输变量。我发现的另一种方法如下,这是我现在正在使用的方法,效果很好。

    $domain = "DOMAINNAME"
    $AdUser = "$domain\JoinDom"
    $AdPW = "PASSWORD"
    $AdPass = convertto-securestring -string $AdPW -AsPlainText -Force
    $AdCred = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdUser,$AdPass 
    $ThisComputer = $Env:COMPUTERNAME
    $RetValue = $true
    Function CheckExist ()
    {
        $ErrorActionPreference = ‘SilentlyContinue’
        $Ascriptblock = $ExecutionContext.InvokeCommand.NewScriptBlock("get-adcomputer $ThisComputer")
        $Ret = Invoke-Command -ComputerName SERVERNAME -ScriptBlock $Ascriptblock -Credential $AdCred    
        $ErrorActionPreference = ‘Continue’
        return $Ret
    }
    $ExistBefore = CheckExist
    if($ExistBefore -ne $null)
    {
            $scriptblock = $ExecutionContext.InvokeCommand.NewScriptBlock("Remove-ADComputer $ThisComputer")
            Invoke-Command -ComputerName SERVERNAME -ScriptBlock $scriptblock -Credential $AdCred
            $ExistAfter = CheckExist
            if($ExistAfter -ne $null){$RetValue = $false}
    }
    if($RetValue -ne $false)
    {
        Add-computer -domainname $domain -credential $Adcred -OUPath "OU=MyOU,DC=DOMAIN,DC=DE"
        Restart-Computer -Force
    }
    

    【讨论】:

      【解决方案2】:

      如果您的域控制器运行 Windows Server 2008 或更高版本,您可以利用 PowerShell 会话来避免使用 ADSI。 只需运行以下命令:

      Enter-PSSession -ComputerName domaincontroller.test.com -Credential (Get-Credential)
      

      然后运行Import-Module ActiveDirectory 以允许您使用Get-ADComputerRemove-ADComputer

      【讨论】:

      • 这似乎是一个非常好的方法。使用我的域管理员帐户,我可以使用它从域外部远程运行脚本。谢谢! - 一个问题仍然存在,当使用我创建的服务帐户时,它仍然给我一个“访问被拒绝”。您是否知道该服务帐户需要哪些组或权限才能运行这样的脚本?
      • 您的服务帐户将需要相关组织单位上的创建/删除计算机对象。您可以使用“Active Directory 用户和计算机”管理控制台中的“控制委派向导”。 technet.microsoft.com/en-us/library/cc732524.aspx
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-18
      • 2017-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-17
      相关资源
      最近更新 更多