2017-08-02 7 views
1

ディレクトリからUSBへマシンをコピーする次のコードに3回の再試行を追加し、コピー後に2つのディレクトリが等しいかどうかを確認します。 3回の再試行後に同じでない場合は、以前の設定フォルダを復元します。再試行powershellループ

私はあなたがループを使用する必要があります一度以上の何かをしようとする再試行

[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 

if (Test-Path "D:\Upgrade\CONFIG") { $src_dir = "D:\Upgrade\CONFIG" } 
elseif (Test-Path "F:\Upgrade\CONFIG") { $src_dir = "F:\Upgrade\CONFIG" } 
elseif (Test-Path "G:\Upgrade\CONFIG") { $src_dir = "G:\Upgrade\CONFIG" } 
else { $src_dir = "E:\Upgrade\CONFIG" } 

$dest_dir = "C:\TEST\CONFIG" 
$date_str = Get-Date -UFormat %d%b%Y 
$time_str = Get-Date -UFormat %H%M%S 
$archive_dir = "$dest_dir" + "." + "$date_str" + "." + "$time_str" 

if (Test-Path $src_dir) { Ren "$dest_dir" "$archive_dir" -verbose; Copy-Item "$src_dir" "$dest_dir" -recurse -verbose } 
else { [System.Windows.Forms.MessageBox]::Show("$src_dir found, could not complete !!!") } 

$currentConfig = Get-ChildItem -Recurse $dest_dir | Where-Object { -not $_.PsIsContainer } 


# NEED HELP WITH ADDING RETRIES HERE 


$currentConfig | ForEach-Object { 

# Check if the file, from $dest_dir, exists with the same path under $src_dir 
    If (Test-Path ($_.FullName.Replace($dest_dir, $src_dir))) { 

     # Compare the contents of the two files... 
     If (Compare-Object (Get-Content $_.FullName) (Get-Content $_.FullName.Replace($dest_dir, $src_dir))) { 

      # List the paths of the files containing diffs 
      $_.FullName 
      $_.FullName.Replace($dest_dir, $src_dir) 

     } 
    } 
} 

答えて

0

を実装する方法に問題が生じています。 WhileループまたはDo Whileループ。基本的には、数値を持つ変数を作成し、その数値からカウントダウンします。最後に3つの試行がすべてヒットした場合、スクリプトは設定をリセットできます。

$maxTries = 3 
    While($maxTries -gt 0){ 
     # do work here 
     # if process fails subtract 1 from maxTries 
     # if process succeeds break out of looop 
    } 
+0

このように、私の例では、ディレクトリが等しくない場合はカウンタを減らし、等しい場合は壊れます。それか、別のif文が必要ですか? – user1984300

+0

基本的にループを実行し、実行したい作業を行い、正しく実行されていない場合は1を減らします。作業が完了したら、 'break'を使用してループを早期に中断することができます。 –