Announcement

Collapse
No announcement yet.

compare/check if remote file is newer than local file, and copy if newer

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • compare/check if remote file is newer than local file, and copy if newer

    Hi All,

    Working on the below powershell script to check if a remote file is newer than a local file on a server. If both files are the same, then nothing will happen and script will end. At first I tried using the get-filehash function...but that was taking forever to check the remote file and never completed. The method I'm using now is with the get-item function, and then comparing the .LastWriteTime between the files. That isn't working either...does anyone know what I'm doing wrong here? Any help is appreciated:

    Code:
    $filter_db_local = "c:\Program Files\Websense\Web Security\bin\Websense"
    $filter_db_local_old = "c:\Program Files\Websense\Web Security\bin\Websense_old.bak"
    $filter_db_remote = "\\10.210.1.22\c$\Program Files\Websense\Web Security\bin\Websense"
    $filter_path_local = "c:\Program Files\Websense\Web Security\bin\"
    $filter_service = "Websense Filtering Service"
    $filter_process = "EIMServer"
    
    # Get file info
    $hashSrc = Get-Item $filter_db_local
    $hashDest = Get-Item $filter_db_remote
    
    # check if filtering db's are same version; if the same do nothing, if not then rename and copy
    If ($hasDest.LastWriteTime -gt $hashSrc.LastWriteTime)
    {
    
        # kill filtering process...it never stops cleanly
        Stop-Process -Name $filter_process -Force
    
        # remove old backup file if it exists
        If (Test-Path $filter_db_local_old) {
            Remove-Item $filter_db_local_old
        }
    
        # rename current active filter db to .bak file
        Rename-Item $filter_db_local $filter_db_local_old
    
        # copy db from consho filtering server
        Copy-Item -Path $filter_db_remote -Destination $filter_path_local
    
        # start websense service
        Start-Service -displayname $filter_service
    }

  • #2
    Are you getting any errors when you run it manually? Have you checked each step of the script to make sure it's doing what you expect?
    Regards,
    Jeremy

    Network Consultant/Engineer
    Baltimore - Washington area and beyond
    www.gma-cpa.com

    Comment


    • #3
      Hi Jeremy,

      Script completes in like 2 seconds with no errors. Almost like it thinks the files are the same...when I know for a fact they are not and are different sizes with different timestamps for date modified.

      Comment


      • #4
        I think I may have found the issue. You're doing the wrong compare. You're checking to see if the destination is newer than the source. It never is so it never runs. It is by definition the same or older. Try this:

        Code:
        $filter_db_local = "c:\Program Files\Websense\Web Security\bin\Websense"
        $filter_db_local_old = "c:\Program Files\Websense\Web Security\bin\Websense_old.bak"
        $filter_db_remote = "\\10.210.1.22\c$\Program Files\Websense\Web Security\bin\Websense"
        $filter_path_local = "c:\Program Files\Websense\Web Security\bin\"
        $filter_service = "Websense Filtering Service"
        $filter_process = "EIMServer"
        
        # Get file info
        $hashSrc = Get-Item $filter_db_local
        $hashDest = Get-Item $filter_db_remote
        
        # check if filtering db's are same version; if the same do nothing, if not then rename and copy
        If ($hasDest.LastWriteTime -lt $hashSrc.LastWriteTime){
        
            # kill filtering process...it never stops cleanly
            Stop-Process -Name $filter_process -Force
        
            # remove old backup file if it exists
            If (Test-Path $filter_db_local_old) {
                Remove-Item $filter_db_local_old
            }
        
            # rename current active filter db to .bak file
            Rename-Item $filter_db_local $filter_db_local_old
        
            # copy db from consho filtering server
            Copy-Item -Path $filter_db_remote -Destination $filter_path_local
        
            # start websense service
            Start-Service -displayname $filter_service
        }
        Regards,
        Jeremy

        Network Consultant/Engineer
        Baltimore - Washington area and beyond
        www.gma-cpa.com

        Comment


        • #5
          Sorry I guess that was a bit confusing the way I have it scripted. The destination/remote file will be the file that will change and will have a new timestamp. So the destination should always be the newer file. I'm still not sure why that wasn't working...but I decided to try and use the .Length comparison instead for file size. If both file sizes are not the same, then do the copy. This seems to be working now...waiting for the script/copy process to finish. Thanks.

          Code:
          If ($hasDest.Length -ne $hashSrc.Length)

          Comment


          • #6
            Ah, didn't (and maybe still don't) understand what's happening with the files but I'm glad you got something working for you.
            Regards,
            Jeremy

            Network Consultant/Engineer
            Baltimore - Washington area and beyond
            www.gma-cpa.com

            Comment


            • #7
              Well something is still not right...the copy finished and I know that part of the script works. But now both files are the same, and they have the same file size. I tested the script again just now and it still goes through with the copy thinking that the files are different.

              Maybe this is an issue with UNC paths?

              See below for the tests I did within the powershell console...clearly shows the file sizes are the same, but the operators just don't think so:

              Code:
              PS C:\Users\SA_WEBSENSE> $filter_db_remote = "\\10.210.1.22\c$\Program Files\Websense\Web Security\bin\Websense"
              PS C:\Users\SA_WEBSENSE> $hashDest = Get-Item $filter_db_remote
              PS C:\Users\SA_WEBSENSE>
              PS C:\Users\SA_WEBSENSE> Write-Host $hashDest.Length
              2015390464
              PS C:\Users\SA_WEBSENSE> $filter_db_local = "c:\Program Files\Websense\Web Security\bin\Websense"
              PS C:\Users\SA_WEBSENSE> $hashSrc = Get-Item $filter_db_local
              PS C:\Users\SA_WEBSENSE> write-host $hashSrc.Length
              2015390464
              PS C:\Users\SA_WEBSENSE> If ($hasDest.Length -ne $hashSrc.Length) { Write-Host "not equal!"}
              not equal!
              PS C:\Users\SA_WEBSENSE> If ($hasDest.Length -eq $hashSrc.Length) { Write-Host "equal!"}
              PS C:\Users\SA_WEBSENSE>
              PS C:\Users\SA_WEBSENSE>
              PS C:\Users\SA_WEBSENSE>
              PS C:\Users\SA_WEBSENSE>
              PS C:\Users\SA_WEBSENSE> Write-host $hashSrc
              C:\Program Files\Websense\Web Security\bin\Websense
              Last edited by ekrengel; 26th August 2015, 14:41.

              Comment


              • #8
                The code contains everytime the same typo...

                It is in the line:
                If ($hasDest.LastWriteTime -gt $hashSrc.LastWriteTime)

                where $hasDest.LastWriteTime should be $hashDest.LastWriteTime

                This posting is provided "AS IS" with no warranties, and confers no rights.

                __________________

                ** Remember to give credit where credit's due **
                and leave Reputation Points for meaningful posts

                Comment

                Working...
                X