Search code examples
powershellpowershell-4.0

Comparing my folders with their back up folders to see if my backup solution is working


Please bare in mind I am a complete novice when it comes to powershell.

I am looking for a code that helps compare my folders with their back up folders, that are backed up with robocopy. I need to produce a notification for the user providing details.

#Create the balloon tip notification
function ShowBalloonTipInfo 
{
 
[CmdletBinding()]
param
(
[Parameter()]
$Text,
 
[Parameter()]
$Title,
 
#It must be 'None','Info','Warning','Error'
$Icon = 'Info'
)
 
Add-Type -AssemblyName System.Windows.Forms
 
#So your function would have to check whether there is already an icon that you can reuse. This is done by using a "shared variable", which really is a variable that has "script:" scope.
if ($script:balloonToolTip -eq $null)
{
#we will need to add the System.Windows.Forms assembly into our PowerShell session before we can make use of the NotifyIcon class.
$script:balloonToolTip = New-Object System.Windows.Forms.NotifyIcon 
}
 
$path = Get-Process -id $pid | Select-Object -ExpandProperty Path
$balloonToolTip.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
$balloonToolTip.BalloonTipIcon = $Icon
$balloonToolTip.BalloonTipText = $Text
$balloonToolTip.BalloonTipTitle = $Title
$balloonToolTip.Visible = $true
 
#I thought to display the tool tip for one seconds so i used 2000 milliseconds when I call ShowBalloonTip.
$balloonToolTip.ShowBalloonTip(2000)

}


}

Thank you very much to @mhu for helping me fix the error message. I now get the correct balloon tip notification but it says there are 1414 files that are different between my documents and its mirror.


Solution

  • Refactored your code, hope this helps...

    Set-StrictMode -Version Latest
    $ErrorActionPreference = "Stop"
    
    # Create the balloon tip notification
    function Show-BalloonTipInfo
    {
        [CmdletBinding()]
        param
        (
            [Parameter(Mandatory = $true)]
            [String] $Text,
    
            [String] $Title,
    
            [ValidateSet('None','Info','Warning','Error')]
            [String] $Icon = 'Info'
        )
    
        Add-Type -AssemblyName "System.Windows.Forms"
    
        $path = Get-Process -Id $pid | Select-Object -ExpandProperty "Path"
    
        $balloonToolTip = New-Object -TypeName "System.Windows.Forms.NotifyIcon"
        $balloonToolTip.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
        $balloonToolTip.BalloonTipIcon = $Icon
        $balloonToolTip.BalloonTipText = $Text
        $balloonToolTip.BalloonTipTitle = $Title
        $balloonToolTip.Visible = $true
    
        #I thought to display the tool tip for one seconds so i used 2000 milliseconds when I call ShowBalloonTip.
        $balloonToolTip.ShowBalloonTip(2000)
    }
    
    function Get-Directories
    {
        [CmdletBinding()]
        param
        (
            [Parameter(Mandatory = $true)]
            [String] $Path,
    
            [Parameter(Mandatory = $true)]
            [String] $Exclude
        )
    
        $pathLength = $Path.Length
    
        if (Test-Path -Path $Path -PathType Container)
        {
            return Get-ChildItem -Path $Path -Recurse -Exclude $Exclude -File -ErrorAction SilentlyContinue |
                Foreach-Object { $_.FullName.Substring($pathLength + 1) } |
                Sort-Object
        }
    }
    
    function Write-Log
    {
        [CmdletBinding()]
        param
        (
            [Parameter(Mandatory = $true)]
            [String] $Message,
    
            [Parameter(Mandatory = $true)]
            [String] $Path
        )
    
        $timestamp = Get-Date -format "dd-MM-yyyy HH:mm:ss"
        $text      = "$($timestamp): $message"
    
        Write-Information -MessageData $text -InformationAction Continue
        Add-Content -Path $Path -Value $text -Force -Encoding utf8
    }
    
    $logFile = "$env:Temp\sync.log"
    
    # get content count
    $folders = @(
        @{ "Path" = [Environment]::GetFolderPath("MyDocuments"); "Exclude" = "*.pst,*.ost,*.iso";       "Files" = @() },
        @{ "Path" = "D:\Documents_mirror";                       "Exclude" = "*.pst,*.ost,*.iso";       "Files" = @() },
        @{ "Path" = [Environment]::GetFolderPath("Desktop");     "Exclude" = "*.pst,*.ost,*.iso,*.lnk"; "Files" = @() },
        @{ "Path" = "D:\Desktop_mirror";                         "Exclude" = "*.pst,*.ost,*.iso,*.lnk"; "Files" = @() }
    )
    foreach ($folder in $folders)
    {
        Write-Log -Message "Searching for files in '$($folder.Path)'..." -Path $logFile
        $folder.Files = @(Get-Directories -Path $folder.Path -Exclude $folder.Exclude)
    }
    
    Write-Log -Message "Comparing..." -Path $logFile
    $documentsDiffCount = @(Compare-Object -ReferenceObject $folders[0].Files -DifferenceObject $folders[1].Files).Count
    $desktopDiffCount   = @(Compare-Object -ReferenceObject $folders[2].Files -DifferenceObject $folders[3].Files).Count
    
    $message = ""
    if (($documentsDiffCount -ge 1) -and ($documentsDiffCount -ge 1))
    {
        $message = "There are $documentsDiffCount file(s) in your documents and $desktopDiffCount file(s) on your desktop "
    }
    elseif ($documentsDiffCount -ge 1)
    {
        $message = "There are $documentsDiffCount file(s) in your documents "
    }
    elseif ($documentsDiffCount -ge 1)
    {
        $message = "There are $desktopDiffCount file(s) on your desktop "
    }
    
    if ($message)
    {
        $message += "that were not backed up. Please open robocopy and click 'Run Profile'"
    }
    else
    {
        $message = "Your files have been successfully backed up."
    }
    
    Write-Log -Message "Compare result: $message" -Path $logFile
    Show-BalloonTipInfo -Text "Back up: $message"