【问题标题】:PowerShell find and replace in different directoriesPowerShell 在不同目录中查找和替换
【发布时间】:2016-10-27 04:21:51
【问题描述】:

我正在尝试让我的 PS 脚本在目录中如下所示...

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"
    }
  }    

【问题讨论】:

    标签: powershell replace find


    【解决方案1】:

    首先确保您仅获取带有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'
    
    #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"
        }
    }
    

    另外,尽量保持缩进一致。

    【讨论】:

    • 现在至少可以通过逻辑了,但是我在设置内容部分被拒绝访问:设置内容:对路径“C:\FakeEnv\DEVbox”的访问被拒绝。
    • 我没有寻找其他错误,您必须进行更多调试...(或尝试使用-Force 和管理员权限)
    猜你喜欢
    • 1970-01-01
    • 2011-10-09
    • 1970-01-01
    • 2012-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-12
    相关资源
    最近更新 更多