2016-06-24 5 views
1

を検索と置換のいずれか.TXTがに変更されているかどうかを確認しています過去12時間それらがある場合、STGフォルダの 'dev'から 'stg'へのbatファイルの文字列を置き換え、PRDフォルダの 'stg'から ''(nothing)を置き換えます。変数を変更し、何時間も自分のコードを操作しました。私は自分のロジックが間違っていることを知っています。なぜなら、ステップバイステップでは、バットファイルを操作するifステートメントに決して辿り着かないからです。PowerShellは私がやろうとしています何 <pre><code>C:FAKEENV └───DEVbox ├───PRD | └──BatFile.txt └───STG └──BatFile.txt </code></pre> <p>...私は次のようにディレクトリ内の私のPSスクリプトの表情を作るしようとしている別のディレクトリに</p>を

Clear-Host 
$date = Get-Date 
$hours = '-12'  

#$StgDestFile = 'C:\FakeEnv\DEVbox\STG' 
#$PrdDestFile = 'C:\FakeEnv\DEVbox\PRD' 
$dest_file = 'C:\FakeEnv\DEVbox' 

foreach($file in (Get-ChildItem $dest_file -Recurse)) 
{ 
    #Check for changes from last $hours 
    if($file.LastWriteTime -gt ($date).AddHours($hours))    
    { 
     if($file -eq 'STG') 
     { 
     (Get-Content $dest_file\STG\BatFile.txt) | Foreach-Object { 
      $_ -replace 'dev', 'stg' ` 
      #Format: -replace 'SOMETHING', 'SOMETHING ELSE' `     
     } | Set-Content $dest_file  
     #Insert logic for uninstall and install of 'feature' 
     #Insert logic for confirmation message of changes 
     } 
     elseif($file -eq 'PRD')  
     { 
     (Get-Content $dest_file\PRD\BatFile.txt) | Foreach-Object { 
      $_ -replace 'dev', 'stg' ` 
      #Format: -replace 'SOMETHING', 'SOMETHING ELSE' `     
     } | Set-Content $dest_file  
     #Insert logic for uninstall and install of 'feature' 
     #Insert logic for confirmation message of changes  
     } 
    } 
    } 
    #If no changes occurded, log that nothing happened  
    else 
    { 
    $LogDestination = 'C:\FakeEnv\Logs' 
    $LogPathExist = Test-Path "$LogDestination" 
    #Do this if folder does not exist 
    #If the folder has not already been created, create it and write to log file 
    if($LogPathExist -eq $false) 
    { 
     New-Item $LogDestination -ItemType directory 
     New-Item $LogDestination\log.log -ItemType file 
     Add-Content $LogDestination\log.log "Script detected no changes in $dest_file on $date" 
    } 
    #Do this is folder does exist 
    #if the folder exists, append to log file 
    else 
    { 
     Add-Content $LogDestination\log.log "`nScript detected no changes in $dest_file on $date" 
    } 
    }  

答えて

0

まずあなただけGet-ChildItemため-Fileスイッチ(PS v3の)を持つファイルを取得し、フォルダ名を取得し、それに対してテストを実行し確認してください。

Clear-Host 

$date = Get-Date 
$hours = '-12' 
#$StgDestFile = 'C:\FakeEnv\DEVbox\STG' 
#$PrdDestFile = 'C:\FakeEnv\DEVbox\PRD' 
$dest_file = 'C:\FakeEnv\DEVbox' 

#get files only 
foreach($file in (Get-ChildItem $dest_file -File -Recurse)) { 

    if($file.LastWriteTime -gt ($date).AddHours($hours)) { 

     #get parent directory name 
     $parent = Split-Path $file.DirectoryName -Leaf 

     if($parent -eq 'STG') { 
      (Get-Content $dest_file\STG\BatFile.txt) | Foreach-Object { 
       $_ -replace 'dev', 'stg' ` 
       #Format: -replace 'SOMETHING', 'SOMETHING ELSE' ` 
      } | Set-Content $dest_file 
     } elseif($parent -eq 'PRD') { 
      (Get-Content $dest_file\PRD\BatFile.txt) | Foreach-Object { 
       $_ -replace 'dev', 'stg' ` 
       #Format: -replace 'SOMETHING', 'SOMETHING ELSE' ` 
      } | Set-Content $dest_file 
     } 
    } 
} else { 
    $LogDestination = 'C:\FakeEnv\Logs' 
    $LogPathExist = Test-Path "$LogDestination"   
    if($LogPathExist -eq $false) { 
     New-Item $LogDestination -ItemType directory 
     New-Item $LogDestination\log.log -ItemType file 
     Add-Content $LogDestination\log.log "Script detected no changes in $dest_file on $date" 
    } else { 
     Add-Content $LogDestination\log.log "`nScript detected no changes in $dest_file on $date" 
    } 
} 

また、あなたのインデントを維持しよう一貫している。

+0

少なくともロジックを通過していますが、設定内容の部分でアクセスが拒否されます。セット内容:パス 'C:\ FakeEnv \ DEVbox'へのアクセスが拒否されました。 – Maverick94

+0

私はあなたがもっとデバッグする必要のある他のエラーを探していませんでした(あるいは '-Force'とadmin権限で試してみてください) – sodawillow

関連する問題

 関連する問題