GET-IT: TEAMS DAY | 1-Day Free Virtual Conference all about Teams. Here on Petri.com - 8/12/20 GET-IT: TEAMS DAY - 8/12/20

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

Home Forums Scripting PowerShell compare/check if remote file is newer than local file, and copy if newer

Viewing 1 post (of 1 total)
  • Author
    Posts
  • Avatar
    ekrengel
    Member
    #165688

    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:

    $filter_db_local = “c:Program FilesWebsenseWeb SecuritybinWebsense”
    $filter_db_local_old = “c:Program FilesWebsenseWeb SecuritybinWebsense_old.bak”
    $filter_db_remote = “\10.210.1.22c$Program FilesWebsenseWeb SecuritybinWebsense”
    $filter_path_local = “c:Program FilesWebsenseWeb Securitybin”
    $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
    }
    [/CODE][CODE]
    $filter_db_local = “c:Program FilesWebsenseWeb SecuritybinWebsense”
    $filter_db_local_old = “c:Program FilesWebsenseWeb SecuritybinWebsense_old.bak”
    $filter_db_remote = “\10.210.1.22c$Program FilesWebsenseWeb SecuritybinWebsense”
    $filter_path_local = “c:Program FilesWebsenseWeb Securitybin”
    $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
    }
    [/CODE]

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.