gpt4 book ai didi

powershell - 如何从 Active Directory 远程删除 AD 计算机 - Powershell

转载 作者:行者123 更新时间:2023-12-04 00:59:34 25 4
gpt4 key购买 nike

我在未连接到域、没有任何模块且运行 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 并且属性列表显示了所有属性,但它不允许我删除该对象。

Exception calling "DeleteTree" with "0" argument(s): "Logon failure: unknown user name or bad password.

At C:\TEMP\Domjoin1.1.ps1:49 char:33 $OPath.psbase.DeleteTree <<<< () CategoryInfo: NotSpecified: (:) [], MethodInvocationException FullyQualifiedErrorId : DotNetMethodException

代码:

# 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

尽管问题似乎很明显,因为错误指出:

Logon failure: unknown user name or bad password.

用户名和密码与我最初用于从 AD 获取对象的用户名和密码相同。所以它确实有效 - 在尝试 deleteTree() 时,我是否必须以某种方式再次提供凭据?我还在存储对象的 OU 上授予了用户 FullControl。

编辑:

当我在另一台装有 PS 3.0 的机器上执行此操作时,我收到不同的错误消息:

Exception calling "DeleteTree" with "0" argument(s): "Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))"

最佳答案

我发现了问题。

当使用 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
}

关于powershell - 如何从 Active Directory 远程删除 AD 计算机 - Powershell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36617612/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com