Powershell

如何獲取powershell中涉及的robocopy摘要資訊?

  • April 13, 2017

我編寫了一個快速腳本來比較伺服器之間的文件差異,然後提示是否鏡像更改。如果沒有任何更改,我希望它智能地不提示複製。

劇本:

robocopy "$cSource" "$cDestination" /E /L /FP

$cDecision = Read-host "Continue = 1, anything else = Stop"

if ($cDecision -eq "1") {
robocopy "$cSource" "$cDestination" /MIR /FFT /Z /W:5 /MT:64 /XX /log:$cLogLocation

Invoke-item $cLogLocation

}

else { Write-host "Quit!" }

如果 Robocopy 報告不匹配、失敗、附加和複製為 0,我希望 Powershell 不要求繼續。對於這個特定的項目來說這不是很重要,但我可以想到一些有用的用途來進行這種檢查。謝謝!

終於有人在 10 秒內沒有回答的問題!我使用您的問題繼續了解有關 Powershell 的更多資訊。以下對我有用,我希望看看其他人如何精簡這段程式碼:

Clear-Host
$ErrorActionPreference = "Continue"
$DebugPreference = "Continue"
$VerbosePreference = "Continue"

@"

## robocopy_helper.ps1 ########################################################
Usage:        powershell -ExecutionPolicy Bypass -File ./robocopy_helper.ps1

Purpose:      Dry run before Full run of robocopy

History:      07/11/2014  -  Created
###############################################################################

"@

## User Supplied Variables
$cSrc = "C:\Temp"
$cDst = "C:\Temp2"
$cLog = "c:\robo.log"

## Robocopy Dry Run
$robo_test = robocopy "$cSrc" "$cDst" /E /L /FP

## Use Regular Expression to grab the following Table
#               Total    Copied   Skipped  Mismatch    FAILED    Extras
#    Dirs :         1         0         1         0         0         0
#   Files :         1         0         1         0         0         0
$robo_results = $robo_test -match '^(?= *?\b(Total|Dirs|Files)\b)((?!    Files).)*$'

## Convert Table above into an array
$robo_arr = @()
foreach ($line in $robo_results){
   $robo_arr += $line
}

## Create Powershell object to tally Robocopy results
$row = "" |select COPIED, MISMATCH, FAILED, EXTRAS
$row.COPIED = [int](($robo_arr[1] -split "\s+")[4]) + [int](($robo_arr[2] -split "\s+")[4])
$row.MISMATCH = [int](($robo_arr[1] -split "\s+")[6]) + [int](($robo_arr[2] -split "\s+")[6])
$row.FAILED = [int](($robo_arr[1] -split "\s+")[7]) + [int](($robo_arr[2] -split "\s+")[7])
$row.EXTRAS = [int](($robo_arr[1] -split "\s+")[8]) + [int](($robo_arr[2] -split "\s+")[8])

## If there are differences, lets kick off robocopy again
if ( ($row.COPIED + $row.MISMATCH + $row.FAILED + $row.EXTRAS) -gt 0 ){
   robocopy "$cSrc" "$cDst" /MIR /FFT /Z /W:5 /MT:64 /XX /log:$cLog
   Invoke-item $cLog
}
else { Write-host "Folders '$cSrc' and '$cDst' are twins" }

注意:我已將上面的正則表達式子句點更改為星號前的空格。這消除了在文件名/路徑中擷取“文件”或“目錄”或“總計”的風險。原始行在下面供後代使用。

jski 2014/07/16

$ robo_results = $ robo_test -match ‘^(?=. ?\b(Total|Dirs|Files)\b)((?! Files).) $’

引用自:https://serverfault.com/questions/611950