I have been tasked with automating our disabling/deleting of old computers. Unfortunately I found out the hard way that the data I am being given for this task has many errors and I am having issues validating it. The requirements here are that the computer account must exist, it cannot be a duplicate, it cannot be a server OS and the account password for the computer must not have been reset in the last 10 days. I have been able to validate all of this seperately but when I attempt to combine the validation into one script I am failing. Specifically I am unable to get past the duplicate data step. Here is the code:
$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
The script seems to be unable to go past the second if statement. Please let me know if I need to provide any more information or I am missing a bracket.
D:\Content\ADPCDisable\computers.txt
file? Do you actually need to throw an error in that case, or is simply de-duplicating that file enough? (Dedup with something like$list = Get-Content D:\Content\ADPCDisable\computers.txt | sort-object -unique
)ElseIf
statement that you can use that would make your code less ugly. SoIf (cond) {} ElseIf (cond) {} ElseIf (cond) {} ElseIf (cond) {} Else {}
.