none
Script to copy and paste the latest modified log files from one server to other server(the servers are in the different domains) RRS feed

  • Question

  • Friends,

    I am beginner to the power shell  and i am trying to write a script that copies the latest modified log file from one server to other server (servers are in different domains), while copying it should check for the credentials and then execute the script.

    Please let me know if the script is correct or any corrections to be made.


    $sourcePath = 'sourcepath'
    $destPath = 'Destinationpath'
    $compareDate = (Get-Date).AddDays(-1);
    $LastFileCaptured = 
    Get-ChildItem -Path $sourcePath | where {
        $_.Extension.EndsWith('.log') -and 
        $_.LastWriteTime -gt $compareDate 
        } | Sort LastAccessTime -Descending | 
    select -First 1  | select  -ExcludeProperty Name, LastAccessTime
    Write-Host $LastFileCaptured.Name  $LastFileCaptured.LastAccessTime
    $LastFileCaptured = Get-ChildItem -Recurse | Where-Object
    {
      $_.LastWriteTime.AddDays(-1) -gt (get-date)
      }
      Write-Host $LastFileCaptured
        Get-ChildItem $sourcePath -Recurse -Include '.log' | Where-Object
    {
        $_.LastWriteTime.AddDays(-1).ToString("yyyy/MM/dd") -gt (get-date).ToString("yyyy/mm/dd")
      }   | Foreach-Object  
    {
          $destDir = Split-Path ($_.FullName -Replace [regex]::Escape($sourcePath), $destPath)
         if (!(Test-Path $destDir))
        {
           New-Item -ItemType directory $destDir | Out-Null
      }
     Copy-Item $_ -Destination $destDir
    }



    • Edited by Purushottam Rathi Sunday, March 24, 2019 5:17 AM
    • Moved by Bill_Stewart Monday, July 29, 2019 8:08 PM This is not "fix/debug/rewrite script for me" forum
    Monday, March 18, 2019 4:58 PM

All replies

  • Please post your code correctly.  Please format the code correctly and use the code posting tool provided on the edit bar.  Please edit your original post and fix it.


    \_(ツ)_/

    Monday, March 18, 2019 5:17 PM
  • I have corrected my code can anyone have a look 
    Friday, March 22, 2019 9:07 AM
  • To copy a file use "Copy-Item".

    help copy-item -online

    Red the complete help and examples.

    You need to learn how to format your code.  What you have posted is unreadable:

    PowerShell Style Guidelines


    \_(ツ)_/


    • Edited by jrv Friday, March 22, 2019 9:13 AM
    Friday, March 22, 2019 9:12 AM
  • When I do a copy of items and want to make a mirror of my log files on another server without all the copying files every time the script runs I use Robocopy /MIR which does not copy over any existing files. It looks at source and destination  and will only copy over newer files and or files that do not exist in on the destination server. 

    https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy

    • Proposed as answer by ComputerScott Friday, March 29, 2019 6:54 PM
    Friday, March 29, 2019 6:53 PM
  • (servers are in different domains), while copying it should check for the credentials and then execute the script.


    I don't see anywhere in your code where you check for credentials.

    Please let me know if the script is correct or any corrections to be made.


    I don't think that we're here to review untested code line by line running in an environment that we know nothing about. Please test your code and if you get an error or something you can't figure it out, post the error and as much detail as you can. Someone will try to help you. 

    Hint: use the -whatif switch on the copy-item command for testing. 


    • Edited by MotoX80 Friday, March 29, 2019 8:20 PM
    Friday, March 29, 2019 8:11 PM