简体   繁体   English

Powershell 删除电脑 Object

[英]Powershell Delete Computer Object

Can some one help me on the next code im trying to run.. it seem's to be ok for me but does not delete the Object when i execute-it有人可以帮助我解决我正在尝试运行的下一个代码吗?它对我来说似乎没问题,但在我执行它时不会删除 Object

Import-Module ActiveDirectory
Clear-Host
$Computer = Read-Host "Type in the Host to Delete"
$rute = Get-ADComputer -Identity:"CN=$Computadora,OU=GDL,OU=ClientComputers,OU=ZAP,OU=MX,DC=kabi,DC=ads,DC=fresenius,DC=com" -Server:"DCKABI02.kabi.ads.fresenius.com"


if($rute.Contains($Computer)){
Clear-Host
Remove-ADComputer -Identity=$Computadora,OU=GDL,OU=ClientComputers,OU=ZAP,OU=MX,DC=kabi,DC=ads,DC=fresenius,DC=com" -Server:"DCKABI02.kabi.ads.fresenius.com" -Confirm:$false
#Clear-Host
Write-Host "The Computer Exist and it has been deleted" -ForegroundColor Green
Start-Sleep -Seconds 5

} else{
Clear-Host
Write-Host "The Host does not exist on AD" -ForegroundColor Red
Start-Sleep -Seconds 3
}

try to delete a Active directory object.. expected to work尝试删除活动目录 object.. 预计可以工作

Your code is not very clear and seems overengineered, $rute.Contains($Computer) will never ever be $true , you probably meant $rute.DistinguishedName.Contains($Computer) which could be $true but .Contains is case-sensitive so it could also be $false .您的代码不是很清楚,似乎设计过度, $rute.Contains($Computer)永远不会是$true ,您可能是$rute.DistinguishedName.Contains($Computer)可能是$true.Contains区分大小写所以它也可能是$false

Your Read-Host statement is assigned to $Computer but then you're using $Computadora .您的Read-Host语句已分配给$Computer但您正在使用$Computadora Also, it's unclear why you are hardcoding OU=GDL,OU=ClientComputers,OU=ZAP,OU=MX,DC=kabi,DC=ads,DC=fresenius,DC=com , I would assume you want to use this OU as your -SearchBase .此外,不清楚为什么要对OU=GDL,OU=ClientComputers,OU=ZAP,OU=MX,DC=kabi,DC=ads,DC=fresenius,DC=com进行硬编码,我假设您想将此 OU 用作你的-SearchBase

Here is how you can approach and will most likely work:以下是您可以采用并且最有可能奏效的方法:

$param = @{
    SearchBase = "OU=GDL,OU=ClientComputers,OU=ZAP,OU=MX,DC=kabi,DC=ads,DC=fresenius,DC=com"
    LDAPFilter = "(name={0})" -f (Read-Host "Type in the Host to Delete")
    Server     = "DCKABI02.kabi.ads.fresenius.com"
}
$computer = Get-ADComputer @param

if($computer) {
    Clear-Host
    $computer | Remove-ADComputer -Server "DCKABI02.kabi.ads.fresenius.com" -Confirm:$false
    Write-Host "The Computer Exist and it has been deleted" -ForegroundColor Green
    Start-Sleep -Seconds 5
}
else {
    Clear-Host
    Write-Host "The Host does not exist on AD" -ForegroundColor Red
    Start-Sleep -Seconds 3
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM