Powershell - Comparing two arrays returns an invalid result

I was hoping someone would be able to help with the problem that I encountered in comparing arrays.

Here is some background. I have a powershell script that generates a list of domain controllers, checks to see if they are online, and then writes data to a csv file. It works great.

I want to run this week and send a warning if any domain controllers are added or removed.

I have another script that imports the previously generated CSV into an array, then I create a new list of domain controllers and send this list to a new array. I am trying to compare these two using compare-object and export a new CSV with a list of differences.

However, I test this by manually modifying the old CSV, and I find that no matter which domain controller I remove from the previous list or where I add a new one, it always reports that this change is the last domain controller in the list.

For example, here's what my CSV file looks like:

"DC_Name","Ping_Response" "dc1.testcompany.com","online" "dc2.testcompany.com","online" "dc3.testcompany.com","online" "dc4.testcompany.com","online" 

If I checked this by deleting "dc3.testcompany.com", "online" and running the script again, he knows that the list has changed, but he reports that the new entry is "dc4.testcompany.com", "online" and not "dc3.testcompany.com", "online".

Similarly, if I change my old CSV to look like this:

 "DC_Name","Ping_Response" "dc1.testcompany.com","online" "dc2.testcompany.com","online" "dc5.testcompany.com","online" "dc3.testcompany.com","online" "dc4.testcompany.com","online" 

he will report that the one that is different from the new list is "dc4.testcompany.com", "online", and not the one I added in the middle.

I have to do something wrong with my comparison. Here is the important part of the script I'm using:

 $newDCArray = @(Import-CSV DC_List.csv) $oldDCArray = @(Import-CSV Last_DC_List.csv) $diff = Compare-Object $oldDCArray $newDCArray if($diff -eq $null) { Write-Host "No changes to DCs" } else { Write-Host "There have been changes to DCs" $diff| Export-CSV DC_Change_List.csv -notypeinformation Send-MailMessage –From " xxxx@xxxxxxxx.com " –To " xxxx@xxxxxxxx.com " –Subject "Alert - Domain Controllers Added or Removed" –Body "DC Report Attached" –SmtpServer "smtp.xxxxxxxxxxx.net" -Attachments "DC_Change_List.csv" } 

Please let me know if anyone has any ideas!

Thanks! John

+4
source share
2 answers

The objects returned by Import-Csv are not “real” objects with which you must compare. These are dynamically created user objects. You will notice that if you check the explicit check $newDCArray[0] -eq $oldDCArray[0] , it will return $false . Since there is simply no proper plumbing to check for equality, Compare-Object has no hope of proper operation.

This is normal. Compare-Object has the -Property parameter, which allows you to look at 1 or more specific properties of these objects. Properties are real objects (strings), so the comparison works. In your case, you are interested in 2 properties - DC_Name and Ping_Response , since you want to know whether a new or remote server, or if an unknown server is disconnected.

This should work for you:

 Compare-Object $oldDCArray $newDCArray -Property 'DC_Name','Ping_Response' 

Also, make sure that -SyncWindow is large enough to make the comparison accurate. Or sometimes it’s easier to just sort the arrays before comparing, so you don’t have to worry about that.

+4
source

Here is the version of the accepted answer that automatically determines the properties:

 $newDCArray = Import-CSV DC_List.csv $oldDCArray = Import-CSV Last_DC_List.csv $propNew = ($newDCArray | Get-Member | ?{$_.MemberType -eq "NoteProperty" | Select -expand Name}) $propOld = ($oldDCArray | Get-Member | ?{$_.MemberType -eq "NoteProperty" | Select -expand Name}) $prop = ($propNew + $propOld) | Get-Unique $diff = Compare-Object $oldDCArray $newDCArray -Property $prop 

Note that Get-Unique stuff, which performs a separate union, only matters when the columns of the csv file are different. Otherwise, you can use the properties of one of the objects.

0
source

Source: https://habr.com/ru/post/1434692/


All Articles