Count and compare two folders with PowerShell and mail the result

Modify script to your needs:

$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

Populære opslag fra denne blog

Microsoft Office 2016 and the AUTO_ACTIVATE property

Access Exchange online with Office 365 multi-factor authentication (MFA/2FA)

Disable logon and logoff events (event id 4624, 4625, 4634 (and all the other ones...))