locked
Need powershell script to copy log files with name of the grandparent folder RRS feed

  • Question

  • We have several servers that save log files locally and I'm trying to automate logging to a single network location with a name of the parent folder where log files are stored and date modified time stamp

    Current setup now:

    Server 1

    D:\Projects\Ohio\Release1\logs

    Server 2

    D:\Projects\Ohio\Release2\logs

    What it should be like

    R:\Logs\Release1_date_modified

    R:\Logs\Release2_date_modified

    All servers have R drive mapped so files would be copied "locally"



    • Edited by alexserd Wednesday, August 5, 2015 1:29 AM
    Saturday, August 1, 2015 1:42 AM

Answers

  • Here's a starter you can play with:

    $logDir = 'C:\Scripts\PowerShell Scripts\Misc Testing\8-3-2015'
    $copyDir = 'R:\Logs\'
    
    Get-ChildItem $logDir -File | ForEach {
    
        $parentFolder = $_.Directory.Name
        $timeStamp = $_.LastWriteTime.ToString('MMddyyyy')
    
        $path = Join-Path -Path $copyDir -ChildPath "$($parentFolder)_$($timeStamp)"
    
        Write-Host "Path to copy to is: $path" -ForegroundColor Green
    
    }

    This is an example only, you'll need to build on this.


    • Marked as answer by alexserd Wednesday, August 5, 2015 5:16 PM
    Monday, August 3, 2015 8:24 PM

All replies

  • You forgot to post your script and ask your question.


    Saturday, August 1, 2015 3:02 AM
  • Mike, I'm very new at writing scripts - still trying to figure out what I'm doing. That said - I found 2 separate scripts that do what I need (sortof) script 1 - rename log files to parent folder and script 2 copies files to remote drive - I need somehow figure out the way to put both of them together.  My apologies for my ignorance - I'm just looking for some guidance

    $path = Split-Path -parent $MyInvocation.MyCommand.Definition
     
    Function renameFiles
    {
      # Loop through all directories
      $dirs = dir $path -Recurse | Where { $_.psIsContainer -eq $true }
      Foreach ($dir In $dirs)
      {
        # Set default value for addition to file name
        $i = 1
        $newdir = $dir.name + "_"
        # Search for the files set in the filter (*.log in this case)
        $files = Get-ChildItem -Path $dir.fullname -Filter *.log -Recurse
        Foreach ($file In $files)
        {
          # Check if a file exists
          If ($file)
          {
            # Split the name and rename it to the parent folder
            $split    = $file.name.split(".jpg")
            $replace  = $split[0] -Replace $split[0],($newdir + $i + ".log")
     
            # Trim spaces and rename the file
            $image_string = $file.fullname.ToString().Trim()
            "$split[0] renamed to $replace"
            Rename-Item "$image_string" "$replace"
            $i++
          }
        }
      }
    }
    # RUN SCRIPT
    renameFiles
    "SCRIPT FINISHED"


    $Source=Read-Host "Please type the source directory you want to backup"
    $Destination=Read-Host "Please type the path directory you want to copy the backup files"
    $Folder=Read-Host "Please type the root name folder"
    $validation=Test-Path $Destination
     
     
    New-PSDrive -Name "Backup" -PSProvider FileSystem -Root $Destination
     
    if ($validation -eq $True){
             
           Set-Location Backup:
    }
    else{
         
        Write-Host "Error!Run Script Again"
     
        break
    }
     
        robocopy $Source $Destination\$Folder *.* /mir /sec
     
         
     
     
    Function Pause{
     
         Write-Host "Backup Sucessfull!!! `n"
     
         Write-Host "Press any key to continue..."
     
         $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | Out-Null
     
    }
     
     
     
    Pause


    • Edited by alexserd Monday, August 3, 2015 7:27 PM
    Monday, August 3, 2015 7:26 PM
  • Here's a starter you can play with:

    $logDir = 'C:\Scripts\PowerShell Scripts\Misc Testing\8-3-2015'
    $copyDir = 'R:\Logs\'
    
    Get-ChildItem $logDir -File | ForEach {
    
        $parentFolder = $_.Directory.Name
        $timeStamp = $_.LastWriteTime.ToString('MMddyyyy')
    
        $path = Join-Path -Path $copyDir -ChildPath "$($parentFolder)_$($timeStamp)"
    
        Write-Host "Path to copy to is: $path" -ForegroundColor Green
    
    }

    This is an example only, you'll need to build on this.


    • Marked as answer by alexserd Wednesday, August 5, 2015 5:16 PM
    Monday, August 3, 2015 8:24 PM
  • Also tried this

    $today = Get-Date -UFormat "%Y-%m-%d"

    $logs="D:\Projects\Ohio\Rel.AW\BuildScripts\logs"
    $backups="R:\logs\CruiseControl\Rel.AW"
    $thisbackup = $backups+$today

    robocopy $storage $thisbackup /e /xf *.*
    foreach ($f in Get-ChildItem $storage -recurse -file)
    {
        if ($f.LastWriteTime -gt ($(Get-Date).AddDays(-1)))
        {
        Copy-Item $f.FullName -Destination $thisbackup$($f.Fullname.Substring($storage.length))
        }

    }

    anf this

    $fileName = (Get-Date).ToString() + "D:\Projects\Ohio\Rel.AW\BuildScripts\logs"
    Copy-Item -Path "D:\Projects\Ohio\Rel.AW\BuildScripts\logs" -Destination "R:\logs\CruiseControl\Rel.AW\$fileName"

    Monday, August 3, 2015 8:27 PM
  • Thank you Mike, following scripts works! But...

    $logDir = 'D:\Projects\Ohio\Rel.AW\BuildScripts\logs'
    $copyDir = 'R:\Logs\CruiseControl\Rel.AW'

    Get-ChildItem $logDir -File | ForEach {
        $parentFolder = $_.Directory.Name
        $timeStamp = $_.LastWriteTime.ToString('MMddyyyy')
        $path = Join-Path -Path $copyDir -ChildPath "$($parentFolder)_$($timeStamp)"
        Write-Host "Path to copy to is: $path" -ForegroundColor Green
    }
    robocopy $logDir $path /log+:R:\Logs\CruiseControl\Rel.AW\log

    Now I'm trying to find a way to rename "Rel.AW" static name to automatically be named as steam name since this script needs to be called by several servers each having a different steam name. How can I reference that ?

    Tuesday, August 4, 2015 9:54 PM