Object comparison

This topic contains 5 replies, has 3 voices, and was last updated by Profile photo of Dammin m Dammin m 1 year, 9 months ago.

  • Author
    Posts
  • #22834
    Profile photo of Dammin m
    Dammin m
    Participant

    Hey guys, would really appreciate some help here.
    I'm building a powershell script to retrieve and delete adcomputers which haven't logged in for a certain amount of time. However, there are some computer which I use once or twice a year which I do not want to remove. I have therefore created a white list.
    My script is as follows:

    # get list of computers
    $computerList = Get-ADComputer -Filter {LastLogonDate -lt $time} -Properties Name,LastLogonDate | select -Property Name,LastLogonDate
    # import white list
    $importedWhiteList = Import-Clixml -Path "$filePath\whitelist.xml"
    #check which computer are not in the white list and add them to another list
    $filteredList = foreach($c in $computerList) {if($importedWhiteList -notcontains $c) {$c}
    # I then delete the old computers from the filtered list.....

    The issue is that even if $c is a computer which I know exists in the white list, it still comes out as false, as if the computer is not there.
    If I pipe a computer from the $importedWhiteList | gm it is the same type object as if I pipe from get-adcomputer | gm and the same object as $c | gm
    The white list was created by piping get-adcomputer -filter.... | export-clixml , it was not created manually so I don't think the list is the issue.

    Help would be really appreciated. I'm new to powershell, so if you have a better practice way of solving my issue I would really appreciate that too.

    Thanks.

  • #22835
    Profile photo of Don Jones
    Don Jones
    Keymaster

    The objects that get re-hydrated by Import-CliXML aren't precisely the same as what's produced by Get-ADComputer. You might try running Get-ADComputer to Export-CliXML, and then comparing imported vs. imported, rather than imported vs. live.

  • #22836
    Profile photo of Dammin m
    Dammin m
    Participant

    Hi Don

    Thanks for helping. Is there a way to specify the parameter which the comparison uses? for example, not to compare them as objects but to compare the computer.Name as strings?

  • #22837
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Not so much. It actually does an every-property comparison. If you want to do something else, it gets more complex and you end up writing your own code. Me, I'd probably just create arrays of strings and then use -contains on those.

  • #22883
    Profile photo of Matt McNabb
    Matt McNabb
    Participant

    Try just comparing the DN value instead of the whole object:

    $filteredList = foreach($c in $computerList) {if($importedWhiteList.DistinguishedName -notcontains $c.DistinguishedName) {$c}
  • #22884
    Profile photo of Dammin m
    Dammin m
    Participant

    Aha! that worked! You sir are a genius.

You must be logged in to reply to this topic.