0

I new in Powershell world and trying to write a script which perform following:

  • Get List of Computers from a text file
  • Get list of Users from a text file
  • Control if the computer name is added in LogonWorkstations field in each user account

Here is the script I have written as of yet.

$Computers = Get-Content Computers.txt
$Users = Get-Content -Path Users.txt | Sort-Object -Unique
$ADUsers = Get-ADUser -Filter * -Properties LogonWorkstations -SearchScope Subtree -SearchBase "OU=ck,OU=users-com,DC=domain,DC=com" |
Where-Object {$Users -contains $_.Name} | Format-List Name,LogonWorkstations

As the script shows I read and retrieve property for Users and have list of computers in text file.

There are 50+ computers and users my question is how can I compare this line wise example check if computer from line 1 of Computers.txt exist in LogonWorkstations property of user from line 1 of Users.txt?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
9944990
  • 444
  • 1
  • 5
  • 16
  • As an aside: It's best to [avoid the use of script blocks (`{ ... }`) as `-Filter` arguments](https://stackoverflow.com/a/44184818/45375) and, indeed, your attempt to access a _property_ of `$_` inside the script block [will fail](https://stackoverflow.com/a/51138705/45375). – mklement0 Oct 21 '19 at 15:01

2 Answers2

1

If each line of both files are corresponding, you can use a simple for loop to iterate through both lists simultaneously. $ADUsers will contain the output of ADUser objects matching the conditions.

$ADUsers = for ($i = 0; $i -lt $Users.Count; $i++) {
    Get-ADUser -Filter "Name -eq '$($Users[$i])'" -Properties LogonWorkstations |
        Where-Object { ($_.LogonWorkstations -split ',') -contains $Computers[$i] }
}

Since LogonWorkstations contains a comma-separated string, you will have to do some string manipulation. Using the -split operator on the , character will result in an array of strings. The -contains operator works nicely when comparing an item or collection of items to a single item.


If you want to compare the LogonWorkstations value of a user to any computer in the list, you can do something like the following:

$ADUsers = foreach ($User in $Users) {
    Get-ADUser -Filter "Name -eq '$User'" -Properties LogonWorkstations | Where-Object {
        Compare-Object -Ref ($_.LogonWorkstations -split ',') -Dif $Computers -IncludeEqual -ExcludeDifferent
    }
}

Compare-Object here will only return a value if there is an exact match.


Note: I believe the LogonWorkstations attribute has been replaced with UserWorkstations attribute. Both may work now but may not be guaranteed in the future.

AdminOfThings
  • 23,946
  • 4
  • 17
  • 27
0

I haven't tried the below code but hopefully, you will be able to work out any little issues:

$computers = Get-Content -Path #PATHTOCOMPUTERS.TXT
$users = Get-Content -Path #PATHTOUSERS.TXT | Sort-Object -Unique

#Set a counter to zero
$counter = 0

foreach ($user in $users){

    try{

        #Get the current user from AD
        $adUser = Get-ADUser -Filter { Name -eq $user} -Properties LogonWorkStations -ErrorAction Stop

        #Uses the current index using $counter to get the correct computer from computers.txt and 
        #checks if the user has it setup as a LogonWorkStation
        if ($adUser.LogonWorkstations -eq $computers[$counter]){
            Write-Host "$user has $computer as a logon workstation"
        }else{
            Write-Host "$user doesn't have $computer as a logon workstation"
        }

    }catch{
        Write-Host "Failed to get the AD user"
    }

    #Increment the $counter
    $counter++
}
I.T Delinquent
  • 2,305
  • 2
  • 16
  • 33
  • Thanks for the code however it is not working for me, the condition is not met I Get User001 doesn't have as a logon workstation. The LogonWorkstations has more than one computer name which are comma seperated i even tried with your code changing -eq to -contains in If statement still not working. – 9944990 Oct 21 '19 at 14:56
  • 1
    As an aside: It's best to [avoid the use of script blocks (`{ ... }`) as `-Filter` arguments](https://stackoverflow.com/a/44184818/45375). – mklement0 Oct 21 '19 at 15:00