Count and compare two folders with PowerShell and mail the result
Modify script to your needs:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$From = "yournamek@yourmailserver.com" | |
$To = "recipientnamek@yourmailserver.com" | |
$SMTPServer = "yourSMTPserver.domain.com" | |
$dest1 = "\\192.168.1.1\folder1\" | |
$dest2 = "\\192.168.1.2\folder2\" | |
$Subject = "Comparison of" + $dest1 + " and " + $dest2 | |
$server1 = Get-ChildItem $dest1 -Recurse | |
$server1count = $server1 | Measure-Object | %{$_.Count} | |
Write-Host $dest1 " counted. Counting " $dest2 " now..." | |
$server2 = Get-ChildItem $dest2 -Recurse | |
$server2count = $server2| Measure-Object | %{$_.Count} | |
Write-Host $dest2 " counted." | |
$server1int = [int]$server1count | |
$server2int = [int]$server2count | |
Write-Host $dest1 " has " $server1int " files. " $dest2 " has " $server2int " files." | |
Compare-Object -ReferenceObject $server1.Name -DifferenceObject $server2.Name | Out-File C:\temp\compare_folders.txt | |
if ($server1int -ne $server2int) | |
{ | |
$Body = "The amount of data in " + $dest1 + " and " + $dest2 + " is not the same. Check attached comparison." | |
} | |
else | |
{ | |
$Body = "The amount of data in " + $dest1 + " and " + $dest2 + " is the same. Check attached comparison to make sure that it is empty." | |
} | |
Send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer -Attachments C:\temp\compare_folders.txt | |
#Delete txt files in folder C:\temp\ | |
Remove-Item C:\temp\*.txt |
Kommentarer
Send en kommentar