2016-06-24 57 views
1

我試圖讓我的PS腳本看目錄如下替換...PowerShell中發現,在不同的目錄

C:\FAKEENV 
└───DEVbox 
    ├───PRD 
    | └──BatFile.txt 
    └───STG 
     └──BatFile.txt 

我所試圖做的是查看是否不.TXT在改變過去12個小時。如果他們有,將BAT文件中的字符串從STG文件夾中的'dev'替換爲'stg',將PRD文件夾中的'stg'替換爲''(無)。我改變了變量,並操縱了我的代碼幾個小時,但沒有運氣。我知道我的邏輯是錯誤的,因爲當我經歷它時,永遠不會去操縱bat文件的if語句。

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

首先確保你只能得到與-File開關Get-ChildItem(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

它現在至少已經通過了邏輯,但是我在set-content部分獲得了拒絕訪問權限:Set-Content:對路徑'C:\ FakeEnv \ DEVbox'的訪問被拒絕。 – Maverick94

+0

我沒有尋找其他錯誤,你將不得不調試更多...(或嘗試'-Force'和管理員權限) – sodawillow