首页 > 解决方案 > 从 txt 禁用多个计算机帐户

问题描述

$Computers = Get-Content c:\temp\computers.txt

foreach ($Computer in $Computers) {
    $ADComputer = $null
    $ADComputer = Get-ADComputer $Computer -Properties Description

    if ($ADComputer) {
        Add-Content C:\temp\computers.log -Value "Found $Computer, disabling"
        Set-ADComputer $ADComputer -Description "Computer Disabled on $(Get-Date)" -Enabled $false
    } else {
        Add-Content C:\temp\computers.log -Value "$Computer not in Active Directory"
    }
}

有人可以验证上面的代码是否正确吗?上面的代码假设:

客户没有任何我们可以测试的测试环境。所以我只是不想冒险。

标签: powershellactive-directory

解决方案


原始代码工作正常。所以我将其发布为答案。

$Computers = Get-Content c:\temp\computers.txt

foreach ($Computer in $Computers) {
    $ADComputer = $null
    $ADComputer = Get-ADComputer $Computer -Properties Description

    if ($ADComputer) {
        Add-Content C:\temp\computers.log -Value "Found $Computer, disabling"
        Set-ADComputer $ADComputer -Description "Computer Disabled on $(Get-Date)" -Enabled $false
    } else {
        Add-Content C:\temp\computers.log -Value "$Computer not in Active Directory"
    }
}

推荐阅读