Differences

This shows you the differences between the selected revisions of the page.

library_example_verify_file_checksum 2015-01-29 library_example_verify_file_checksum 2022-06-16 (current)
Line 1: Line 1:
====== Verify checksum of a remote file against a local file over SFTP/FTP protocol ====== ====== Verify checksum of a remote file against a local file over SFTP/FTP protocol ======
-The following example uses [[library|WinSCP .NET assembly]] from a [[library_powershell|PowerShell]] script. If you have another preferred language, you can easily translate it.+The following script uses [[library|WinSCP .NET assembly]] from a [[library_powershell|PowerShell]] script. If you have another preferred language, you can easily translate it.
-You can use the script (e.g. ''verify.ps1'') [[guide_custom_commands_automation|from WinSCP GUI as a local custom command]]:+The script is distributed in WinSCP installer as a [[extension|WinSCP extension]].
-<code batch+To run the script manually use:  
-powershell.exe -File C:\path\verify.ps1 -sessionUrl !S -localPath "!^!" -remotePath "!/!" -pause+ 
 +<code> 
 +powershell.exe -File C:\path\VerifyFileChecksum.ps1 -sessionUrl &quot;sftp://username:password;fingerprint=ssh-rsa-xxxxxxxxxxx...@example.com/" -localPath "C:\local\path\file.dat" -remotePath "/remote/path/file.dat"
</code> </code>
-Make sure you check //[[ui_customcommand|Use remote files]]// option.+//Note that calculation of a remote file checksum is supported with [[sftp|SFTP]] and [[ftp|FTP]] protocols only, subject to support of [[protocols|respective protocol extension]].//
-//The script uses features available in the latest beta release only.// &amp;beta +&lt;code powershell - VerifyFileChecksum.ps1> 
- +# @name         Verify &Checksum 
-//Note that calculation of remote file checksum is supported with [[sftp|SFTP]] and [[ftp|FTP]] protocols only, subject to support of [[protocols|respective protocol extension]].// +# @command     powershell.exe -ExecutionPolicy Bypass -File &quot;%EXTENSION_PATH%&quot;
- +#                   -sessionUrl "!E" -localPath "!^!" -remotePath "!/!" -pause
-&lt;code powershell&gt;+# ··················-sessionLogPath &quot;%SessionLogPath%&quot; 
 +# @description  Compares checksums of the selected local and remote file 
 +# @flag        RemoteFiles 
 +# @version ·····6 
 +# @homepage    ~~SELF~~ 
 +# @require      WinSCP 5.16 
 +# @option      SessionLogPath -config sessionlogfile 
 +# @optionspage  ~~SELF~~#options 
 +·
param ( param (
-    [Parameter(Mandatory)] +    # Use Generate Session URL function to obtain a value for -sessionUrl parameter. 
-    $sessionUrl = "sftp://user:mypassword;fingerprint=ssh-rsa-xx-xx-xx@example.com/", +    $sessionUrl = "sftp://user:mypassword;fingerprint=ssh-rsa-xxxxxxxxxxx...@example.com/", 
-    [Parameter(Mandatory)]+    [Parameter(Mandatory = $True)]
    $localPath,     $localPath,
-    [Parameter(Mandatory)]+    [Parameter(Mandatory = $True)]
    $remotePath,     $remotePath,
 +    $sessionLogPath = $Null,
    [Switch]     [Switch]
-    $pause = $False+    $pause
) )
 +·
try try
{ {
    Write-Host $localPath     Write-Host $localPath
 +·
    # Calculate local file checksum     # Calculate local file checksum
    $sha1 = [System.Security.Cryptography.SHA1]::Create()     $sha1 = [System.Security.Cryptography.SHA1]::Create()
    $localStream = [System.IO.File]::OpenRead($localPath)     $localStream = [System.IO.File]::OpenRead($localPath)
-    $localChecksum = [BitConverter]::ToString($sha1.ComputeHash($localStream)) +    $localChecksum = [System.BitConverter]::ToString($sha1.ComputeHash($localStream)) 
 +·
    Write-Host $localChecksum     Write-Host $localChecksum
       
    # Load WinSCP .NET assembly     # Load WinSCP .NET assembly
-    Add-Type -Path (Join-Path $PSScriptRoot "WinSCPnet.dll")+    $assemblyPath = if ($env:WINSCP_PATH) { $env:WINSCP_PATH } else { $PSScriptRoot } 
 +····Add-Type -Path (Join-Path $assemblyPath "WinSCPnet.dll")
    # Setup session options     # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions     $sessionOptions = New-Object WinSCP.SessionOptions
-    $sessionOptions.ParseUrl($sessionUrl); +    $sessionOptions.ParseUrl($sessionUrl) 
 +·
    $session = New-Object WinSCP.Session     $session = New-Object WinSCP.Session
 +·
    try     try
    {     {
 +        $session.SessionLogPath = $sessionLogPath
 +
        # Connect         # Connect
        $session.Open($sessionOptions)         $session.Open($sessionOptions)
               
        Write-Host $remotePath         Write-Host $remotePath
 +·
        # Calculate remote file checksum         # Calculate remote file checksum
-        $remoteChecksum = [BitConverter]::ToString($session.CalculateFileChecksum("sha-1", $remotePath))+        $remoteChecksumBytes = $session.CalculateFileChecksum("sha-1", $remotePath) 
 +        $remoteChecksum = [System.BitConverter]::ToString($remoteChecksumBytes)
        Write-Host $remoteChecksum         Write-Host $remoteChecksum
    }     }
Line 63: Line 78:
        $session.Dispose()         $session.Dispose()
    }     }
 +·
    # Compare cheksums     # Compare cheksums
    if ($localChecksum -eq $remoteChecksum)     if ($localChecksum -eq $remoteChecksum)
Line 76: Line 91:
    }     }
} }
-catch [Exception]+catch
{ {
-    Write-Host $_.Exception.Message+    Write-Host "Error: $($_.Exception.Message)"
    $result = 1     $result = 1
} }
- +· 
-# Pause use -pause switch was used+# Pause if -pause switch was used
if ($pause) if ($pause)
{ {
Line 88: Line 103:
    [System.Console]::ReadKey() | Out-Null     [System.Console]::ReadKey() | Out-Null
} }
 +·
exit $result exit $result
</code> </code>
 +===== [[options]] Options =====
 +
 +In the //Session log file// you can specify a path to a [[logging|session log file]].
 +
 +In the //Keyboard shortcut//, you can specify a [[custom_key_shortcuts|keyboard shortcut]] for the extension.

Last modified: by martin