Reputation: 55
I am using PowerShell to export a list of users who are a member of the administrators group in a text file. I am then using Compare-Object to look for the differences. I am looking to automate this process and email me if a difference is found. I am able to compare the two files and send an email, but I realize I don't know how to add the output of the compare to the email.
Upvotes: 0
Views: 383
Reputation: 11304
Maybe this is what you're looking for:
$changedEntries = Compare-Object (gc adminreport.txt) (gc adminreport2.txt) | ?{$_.SideIndicator -eq "<="} | select -expandproperty inputobject
Write-Host "Changed entries: $changedEntries"
if ($changedEntries) {
$joinedChangedEntries =
send-mailmessage smtpserver mail .mydomaincom -To [email protected] -From [email protected] -Subject 'Admin member changed' -Body
}
UPDATE:
I tested above code with simple int-arrays.
$a = 1..4
$b = 4..8
$changedEntries = Compare-Object $a $b | ?{$_.SideIndicator -eq "<="} | select -ExpandProperty inputobject
$joinedEntries = $changedEntries -join ","
if ($changedEntries) { write-host "Found changed entries: $joinedEntries" }
$joinedEntries.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
So the code skeleton is the same as posted above.
Upvotes: 1