2025年2月4日 星期二

PowerShell 使用 ssh 驗證本地與遠端檔案的 SHA256 是否一致

PowerShell 使用 ssh 驗證本地與遠端檔案的 SHA256 是否一致

要驗證傳輸到底有沒有傳對用的函式

# 獲取遠端檔案的雜湊值
function Get-RemoteFileHash {
    [CmdletBinding()]
    param ( 
        # 遠端檔案路徑
        [Parameter(Mandatory, ValueFromPipeline)]
        [string]$Path,
        # 遠端登入資訊
        [Parameter(Mandatory)]
        [string]$RemoteLoginInfo,
        # 遠端私鑰
        [Parameter(Mandatory)]
        [string]$IdentityFile,
        # 遠端作業系統
        [Parameter(Mandatory)]
        [ValidateSet('Windows', 'Linux')]
        [string]$RemoteOS
    ) begin {
        # 檢查私鑰是否存在
        if (-not (Test-Path $IdentityFile)) {
            Write-Error "Cannot find path '$IdentityFile' because it does not exist."
        }
        # 定義不同作業系統的雜湊命令
        $hashCommands = @{
            'Windows' = "powershell `"(Get-FileHash -Path ([WildcardPattern]::Escape('$Path')) -Algorithm SHA256 -EA 1).Hash`""
            'Linux'   = "sha256sum '$Path' 2>&1 >/dev/null && sha256sum '$Path' | cut -d' ' -f1 | tr '[:lower:]' '[:upper:]'"
        }
        # SSH 連線參數設定
        $sshParams = @(
            '-oBatchMode=yes'
            "-oIdentityFile=$IdentityFile"
            $RemoteLoginInfo
        )
    } process {
        # 執行遠端雜湊命令
        $hash = & ssh @sshParams $hashCommands[$RemoteOS] 2>&1
        # 檢查 ssh 命令是否成功
        if ($LASTEXITCODE -ne 0) {
            Write-Error "ssh command failed: $hash" 
            return $null
        }
        # 檢查雜湊值格式是否正確 (SHA-256 應為 64 個十六進制字符)
        if (-not ($hash -match '^[A-F0-9]{64}$')) {
            Write-Error "The hash value '$hash' returned from command '$hashCommand' is not a valid SHA-256 format"
            return $null
        }
        return $hash
    }
}


使用範例

# SSH 連線設定
$remoteLoginInfo = "chg@192.168.3.53"
$identityFile = "${env:USERPROFILE}\.ssh\id_ed25519"
$remoteOS = "Linux"
$remotePath = "/home/chg/work/Tester.bats"

Get-RemoteFileHash $remotePath `
        -RemoteLoginInfo $remoteLoginInfo `
        -IdentityFile $identityFile `
        -RemoteOS $remoteOS

執行完會自動返回遠端檔案的哈希值,再來比較本地端即可知道是否一致了。




2025-02-04 PowerShell 修改檔案日期與時間 (3)

PowerShell 修改檔案日期與時間 (3)

tags: 部落格文章


新文章有更易用的方式詳細請看這篇



關於檔案的 修改時間 和 存取時間有什麼差別可以看這篇站內文


字串格式和日期格式怎麼互轉可以參考這一邊



修改檔案日期 (簡易版)

常用的變更修改日期功能,拉出來獨立封裝的函式複製過去就可以用了。

# 查看日期
irm bit.ly/34DB0Kb|iex; ChangeWriteTime "README.md"
irm bit.ly/34DB0Kb|iex; ChangeWriteTime "TestDir"

# 變更修改日期(通用格式)
irm bit.ly/34DB0Kb|iex
ChangeWriteTime "Readme.md" "1999-02-13 23:59:59" -Simple

# 變更修改日期(繁體中文)
irm bit.ly/34DB0Kb|iex
ChangeWriteTime "TestDir" "2022/2/1 上午 00:00:00"

# 變更修改日期(日文)
irm bit.ly/34DB0Kb|iex
ChangeWriteTime "TestDir" "2022/2/1 午前 00:00:00"

# 過濾資料夾檔案
irm bit.ly/34DB0Kb|iex
ChangeWriteTime "Test" "1999-02-13 23:59:59" -Simple -Filter:@("*.txt","*.md") -Force










修改檔案日期 (完整版)

這個是完整的功能,日期與讀檔案要自己處理

# 查看檔案日期
irm bit.ly/34DB0Kb|iex; FileDateEditor (Get-Item ".\README.md")

# 變更檔案修改日期(簡潔版)
$Date = New-DateTime "2022-02-13 21:00:00" -Simple
$File = Get-Item ".\README.md"
irm bit.ly/34DB0Kb|iex; FileDateEditor $File $Date




詳細用法
# 載入函式
irm bit.ly/34DB0Kb|iex

# 日期
$Date = New-DateTime "2022-02-13 21:00:00" -Simple
# 讀取檔案
$File = Get-Item ".\README.md"
$File = Get-ChildItem "TestDir" -Recurse

# 查看檔案日期
FileDateEditor $File

# 變更檔案修改日期(簡潔版)
FileDateEditor $File $Date

# 變更個別日期
FileDateEditor $File -CreationTime:$Date
FileDateEditor $File -LastWriteTime:$Date
FileDateEditor $File -LastAccessTime:$Date

# 變更 [建立、修改、存取] 日期
FileDateEditor $File -AllDate:$Date

# 變更 [修改、存取] 日期 (其他自己類推可任意組合)
FileDateEditor $File -LastAccessTime:$Date -LastWriteTime:$Date




日期解決方案

這個程式的API範例,推薦優先使用1,2和3是方便用的效能較差。

# 載入函式
irm bit.ly/34DB0Kb|iex

# 日期1
New-DateTime "2022-02-13 21:00:00" -Simple
New-DateTime "1999-05-12 12:00:00" "yyyy-MM-dd HH:mm:ss"

# 日期2
New-DateTime "1999/02/13 午前 04:15:45" -JP
New-DateTime "2022年02月13日 午前 04:55:55" -JP
New-DateTime "2022年02月13日, 午前 04:55:55" -JP
New-DateTime "1999/02/13 上午 04:15:45" -TW
New-DateTime "1999年02月13日 上午 04:15:45" -TW
New-DateTime "1999年02月13日, 上午 04:15:45" -TW

# 日期3 - 可以省掉後面國家自動偵測當前系統格式
New-DateTime "1999/02/13 午前 04:15:45"
New-DateTime "1999年02月13日 午前 04:15:45"
New-DateTime "1999年02月13日, 午前 04:15:45"
New-DateTime "1999/02/13 上午 04:15:45"
New-DateTime "1999年02月13日 上午 04:15:45"
New-DateTime "1999年02月13日, 上午 04:15:45"