0

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

3
  • You are ambiguous. Without knowing the file1.?txt ?csv and file2 layout it's difficult to help. Commented Jun 14, 2018 at 7:31
  • @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 ) Commented Jun 14, 2018 at 15:33
  • 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. Commented Jun 14, 2018 at 15:55

2 Answers 2

0

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for info , unfortunately i have your suggestion but i got the following error (Select-Object : Property "Name" cannot be found.) . I have update the above code to reflect all what i have so far .
You'll have to post the format of file2.csv or just change Name to name of column where you have computer names. If it's standard list you'll have to use Get-Content instead of Import-Csv. That's all I can tell based on info you provided.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.