2

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.

9
  • It would be good if you could give an example of the invalid data. So do you have duplicate names in the 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)
    – Zoredache
    Apr 25, 2017 at 21:10
  • BTW, there is a ElseIf statement that you can use that would make your code less ugly. So If (cond) {} ElseIf (cond) {} ElseIf (cond) {} ElseIf (cond) {} Else {}.
    – Zoredache
    Apr 25, 2017 at 21:13
  • For the the duplicate data I do not need to throw the error . Also, I update my script with else if. Originally I had split all of these out and was adding each invalid piece to a separate list and I had just copied and pasted the If statements from the separate scripts. This is necessary because the area where the data is coming from insists they have verified the data. Examples of bad data: any server OS and any machine that has been active in the past 3 days. Script updated above. Apr 25, 2017 at 22:50
  • I see you updated things but did the sort-object -unique remove the duplicate data you said you were having problems with?
    – Zoredache
    Apr 25, 2017 at 23:00
  • I just finished testing and it did remove the duplicates. It is still getting stuck at the checking for server OS section. Apr 25, 2017 at 23:04

1 Answer 1

1
$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

This is the final result. I used the try/catch option to suppress errors. My issue was with the -contains comparison. Thanks for all the help!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .