0
votes

Could you guys please help me with the following scenario . Here is what i am trying to accomplish .

  1. I generate a report of computers that need that need to be disabled in AD = file1.txt

  2. I have a pre-made list of Computers to exclude from getting disabled = file2.txt

  3. I have a script to disable a list of computers from a text file but i would like to exclude whatever computer that exist in the file2.txt .

Here is what i have so far

$toBeDisabled = Import-CSV \\SERVER\file1.csv

$toBeExcluded = Import-CSV \SERVER\file2.csv

$toBeDisabled = $toBeExcluded | Where-Object {($toBeExcluded | Select-Object -ExpandProperty Name) -NotContains $_.Name}

ForEach ($Computer in $toBeDisabled.DeviceName)

{ $Computer = $Computer.Trim()

$ADComputer = Get-ADComputer $Computer -Properties Description

If ($ADComputer)

{ Add-Content c:\temp\computers.log -Value "The following PC $Computer has been found and disabled"

Set-ADComputer $ADComputer -Description "$($ADComputer.Description)- Disable due to inactivity - $(Get-Date) - by $env:UserName " -Enabled $False

}

Else

{ Add-Content c:\temp\computers.log -Value "$Computer not Found in Active Directory or Was disabled before"

}

}

Im getting the following error (Select-Object : Property "Name" cannot be found)

Thanks

2
You are ambiguous. Without knowing the file1.?txt ?csv and file2 layout it's difficult to help.user6811411
@LotPings . I have csv file1 ( computers that will get disabled ) , the file2.txt notepad ( computers that i want to be excluded if it exists in file1.csv . both files have computers ( ex: PC-678789 )Dandano
At least file1.csv seems to have the header DeviceName - this is what I call layout. It's your task to include such information in your edited question.user6811411

2 Answers

0
votes

The best approach here would be to just compare these two files first:

$toBeExcluded = Import-Csv C:\temp\file2.csv
$toBeDisabled = $toBeExcluded | Where-Object {($toBeExcluded | Select-Object -ExpandProperty Name) -NotContains $_.Name}

The above assumes that file2.csv has the same format (so it has property Name).

Then, instead of $Computers you just have to use $toBeDisabled in foreach.

0
votes

I figured that out .

Compare-Object $file1 $file2 -Property "DeviceName" | Where SideIndicator -eq "<=" | Select DeviceName | Export-Csv "\Server\DisableList.csv" -NoType

Thanks everyone for help