简体   繁体   English

测试计算机 object 是否存在于 PowerShell 的 Active Directory 中

[英]Test if computer object exists in Active Directory in PowerShell

I have a problem checking if a computer object exists in AD or not.我在检查计算机 object 是否存在于 AD 中时遇到问题。 To be specific it should go into an "else" if not.具体来说,如果不是,它应该将 go 变成“else”。

This is my code:这是我的代码:

if (Get-ADComputer "$Computer") {
        Get-ADComputer $Computer | Set-ADComputer -Enabled $false
        Start-Sleep -s 2 
        Get-ADComputer $Computer | Move-ADObject -TargetPath $PathOU
        Start-Sleep -s 2 
} else {
    write-host "Example"
}

When the input computer doesn't exist, the problem is, the command Get-ADComputer "$Computer" already fails in the if statement, so it never reaches the else .当输入计算机不存在时,问题是Get-ADComputer "$Computer"命令在if语句中已经失败,因此它永远不会到达else

How can I avoid that?我怎样才能避免这种情况?

Thanks in advance.提前致谢。

Use Try Catch instead:请改用Try Catch

try {
    Get-ADComputer $Computer | Set-ADComputer -Enabled $false
    Start-Sleep -s 2 
    Get-ADComputer $Computer | Move-ADObject -TargetPath $PathOU
    Start-Sleep -s 2 
catch {
    Write-Host "Example"
}

Add the Get-ADComputer to a variable before the if block to help.Get-ADComputer添加到if块之前的变量以提供帮助。 Also before setting that variable with the command, set it to null per the below example.同样在使用命令设置该变量之前,请按照以下示例将其设置为 null。 In addition to that, change up the if block to first check if it does not exist and to run that logic first then else run logic if it does exist.除此之外,更改if块以首先检查它是否不存在并else运行该逻辑然后如果它确实存在则运行逻辑。

$comp = "";
$comp = Try {Get-ADComputer "$Computer"} Catch {$false};

if (!$comp) {
    write-host "Example"
    } else {
        $comp | Set-ADComputer -Enabled $false
        Start-Sleep -s 2 
        $comp | Move-ADObject -TargetPath $PathOU
        Start-Sleep -s 2
    };

Supporting Resources配套资源

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

相关问题 Powershell中计算机的活动目录组 - Active directory groups of a computer in powershell PowerShell-遍历Active Directory中的计算机名称 - PowerShell - iterating over computer names in Active Directory 在Active Directory中重置计算机的PowerShell命令 - PowerShell Command for reset Computer in Active Directory 在 AD 中创建计算机对象后,需要借助 Powershell 脚本将 Active Directory 中的计算机对象移动到新的 OU - Need Assistance With a Powershell Script to Move a Computer Object in Active Directory to New OU As Soon as the Computer Object Gets Created in AD 在Active Directory中创建时禁用计算机对象 - Disabling Computer Object on Creation in Active Directory 使用C#和Powershell从不属于域的计算机访问Active Directory - Accessing Active Directory From a Computer Not Part of the Domain With C# and Powershell Powershell不会从Active Directory中提取所有计算机名称 - Powershell won't pull every computer name from Active Directory 如何从Active Directory远程删除AD计算机 - Powershell - How to remotely delete an AD-Computer from Active Directory - Powershell Powershell:在Active Directory中查找第一个免费计算机名称 - Powershell : Find the first free computer name in Active Directory Powershell 查询活动目录并自动生成未使用的计算机名 - Powershell Query Active Directory and automatically generate an unused computer name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM