by jdevich28 at 2012-09-17 12:11:02
Hello,by althought at 2012-09-18 01:34:24
I was hoping someone would be able to help with a problem I’ve been running into comparing arrays.
Here’s some background. I have a powershell script that generates a list of domain controllers, checks to see if they are online and then writes the data to a csv file. This works fine.
I want to run this weekly and send an alert if any domain controllers are added or removed.
I have another script that imports the previously generated CSV to an array, then I generate a new list of domain controllers and send that list to a new array. I am attempting to compare the two using compare-object, and export a new CSV with a list of differences.
However, I’m testing this by manually modifying the old CSV, and I’ve found that no matter which domain controller I remove from the previous list or where I add a new one, it always reports that the change is the last domain controller on the list.
For example, here’s what my CSV file looks like:
If I test this by deleting “dc3.testcompany.com”,“online” and running the script again, it knows that the list has changed, but it reports that the new entry is “dc4.testcompany.com”,“online” and not “dc3.testcompany.com”,“online”.
Similarly, if I modify my old csv to look like this:
it will report that the one that is different from the new list is “dc4.testcompany.com”,“online”, not the one I’ve added in the middle.
I must be doing something wrong with my comparison. Here is relevant part of the script I’m using:
# Send new CSV report to array
$newDCArray = @(Import-CSV DC_List.csv)
# Send old CSV report to array
$oldDCArray = @(Import-CSV Last_DC_List.csv)
# Compare both arrays to find added/deleted DCs
$diff = Compare-Object $oldDCArray $newDCArray
# Report if there are differences
if($diff -eq $null)
Write-Host “No changes to DCs”
Write-Host “There have been changes to DCs”
$diff| Export-CSV DC_Change_List.csv -notypeinformation
Send-MailMessage â€“From “email@example.com” â€“To “firstname.lastname@example.org” â€“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!
Hello John,by jdevich28 at 2012-09-18 05:48:40
I’m fairly new to both posting to Forums & Powershell, but though I’d take a look the problem you have come accross & perhaps familiarise myself with using the compare-object at the same time.
Firstly, I got same result as you straight away - even though list 2 has DC 5 in it, I still seem to see DC4 as being the different value.
However, if I add the property param and select just DC_Name , it works as I would expect & finds DC5 as being the new one in the list.
Maybe that will help? I know you still need to do the online/ offline check, but perhaps that could be done seperatly.
Be interesting to see if anyone figures out what is actually going here?!
All the best.
Thanks Colin, that’s exactly what I needed. If I add the Ping_Response field it works perfect.
$diff = Compare-Object $oldDCArray $newDCArray -property ‘DC_Name’,'Ping_Response’
This works great, thanks again!