2016-06-24 4 views
1

나는 다음과 같은 디렉토리에 내 PS 스크립트 보이게하려고에 대체 PowerShell을은 ... 찾아 다른 디렉토리

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

무엇 하나 .txt 인가에 변경된 경우 내가보고하면된다하려고 지난 12 시간. 그럴 경우 STG 폴더의 'dev'에서 'stg'까지의 bat 파일의 문자열을 PRD 폴더의 'stg'에서 ''(nothing)로 바꿉니다. 나는 변수를 변경하고 운이없는 몇 시간 동안 내 코드를 조작했다. 나는 내 논리가 잘못되었다는 것을 안다. 왜냐하면 내가 단계를 밟을 때 박쥐 파일을 조작 할 if 문에 절대로 도달하지 않기 때문이다. 먼저 만 Get-ChildItem (PS v3의)에 대한 -File 스위치를 사용하여 파일을 얻을 수 있도록

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

, 다음에 테스트를 폴더 이름을 얻고 실행

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

관련 문제