创build要禁用的计算机列表

我一直负责自动化我们禁用/删除旧电脑。 不幸的是,我发现我为这项任务提供的数据有很多错误,而且我遇到了validation它的问题。 这里的要求是计算机账号必须存在,不能重复,不能是服务器的操作系统,电脑的账号密码在最近10天内不能重置。 我已经能够单独validation所有这些,但是当我尝试将validation结合到一个脚本中时,我失败了。 具体来说,我无法通过重复的数据步骤。 这里是代码:

$file = "D:\Transcripts\ADPCverify\" + (get-date -Format yyyymmdd-hhmmss) + ".txt" start-transcript -LiteralPath $file $date = Get-Date $computers = Get-Content D:\Content\ADPCverify\unverified.txt | sort-object -unique $list = Get-Content D:\Content\ADPCDisable\computers.txt $name = 'null' ForEach($computer in $computers){ $prevname = $name $name = (Get-ADComputer -Identity $computer -Server server).name $PCObject = Get-ADComputer -Identity $computer -Server server -Properties * $OS = $PCObject.OperatingSystem $pwdLastSet = [DateTime]::FromFiletime([Int64]::Parse($PCobject.pwdLastSet)) $TimeSince = New-TimeSpan $pwdLastSet $date if($name -eq $prevname){ Add-Content D:\Content\ADPCDisable\FailedComputers.txt $computer write-host "Machine " + $computer + " does not exist and has been added to the failed computers list." }elseif($OS -contains 'Windows Server'){ Add-Content D:\Content\ADPCDisable\FailedComputers.txt $computer write-host "Machine " + $computer + " has a server OS and will be added to the failed computer list." }elseif($TimeSince.totaldays -lt 10){ Add-Content D:\Content\ADPCDisable\FailedComputers.txt $computer write-host "Machine " + $computer + "'s password was reset " + $TimeSince.totaldays + " days ago and has been added to the failed computer list." }else{ Add-Content D:\Content\ADPCDisable\Computers.txt $name write-host "Machine " + $name + " has been succesfully added to the computers to disable list." } } Stop-Transcript 

脚本似乎无法通过第二个if语句。 请让我知道,如果我需要提供更多的信息或我缺less一个括号。

 $file = "D:\Transcripts\ADPCverify\" + (get-date -Format yyyymmdd-hhmmss) + ".txt" start-transcript -LiteralPath $file $date = Get-Date $computers = Get-Content D:\Content\ADPCverify\unverified.txt | sort-object -unique $list = Get-Content D:\Content\ADPCDisable\computers.txt $name = 'null' $server = '' ForEach($computer in $computers){ Try{ $PCObject = Get-ADComputer -Identity $computer -Server $server -Properties * $name = $PCObject.Name $OS = $PCObject.OperatingSystem $pwdLastSet = [DateTime]::FromFiletime([Int64]::Parse($PCobject.pwdLastSet)) $TimeSince = New-TimeSpan $pwdLastSet $date if($OS.StartsWith('Windows Server')){ Add-Content D:\Content\ADPCDisable\FailedComputers.txt $computer write-host "Machine " $computer " has a server OS and will be added to the failed computer list." }elseif($TimeSince.TotalDays -lt 10){ Add-Content D:\Content\ADPCDisable\FailedComputers.txt $computer write-host "Machine " $computer "'s password was reset " + $TimeSince.TotalDays + " days ago and has been added to the failed computer list." }else{ Add-Content D:\Content\ADPCDisable\Computers.txt $name write-host "Machine " $name " has been succesfully added to the computers to disable list." } } Catch{ Add-Content D:\Content\ADPCDisable\FailedComputers.txt $computer write-host "Machine " $computer " does not exist and has been added to the failed computers list." } } Stop-Transcript 

这是最后的结果。 我使用了try / catch选项来抑制错误。 我的问题是与容纳比较。 感谢所有的帮助!