比较我的文件夹和它们的备份文件夹,看看我的备份解决方案是否有效

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

请记住,在 powershell 方面,我是一个彻头彻尾的新手。

我正在寻找一个代码来帮助比较我的文件夹和它们的备份文件夹,这些备份文件夹是用 robocopy 备份的。我需要为提供详细信息的用户生成通知。

#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)

}


}

非常感谢@mhu 帮我修复错误信息。我现在收到正确的气球提示通知,但它说我的文档与其镜像之间有 1414 个文件不同。

重构了您的代码,希望这对您有所帮助...

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"