How to check if local file modified date is different then replace file if it is

This topic contains 4 replies, has 2 voices, and was last updated by Profile photo of Charles Downing Charles Downing 2 years, 2 months ago.

  • Author
    Posts
  • #19179
    Profile photo of Darren Scordecchia
    Darren Scordecchia
    Participant

    Hi, I'm pretty new to powershell and was looking for some assistance

    I would like a PowerShell script for log on script to do the following when a user logs on

    Check if the following file exists
    C:\Users\%username%\AppData\LocalLow\Sun\Java\Deployment\security\exception.sites
    If it does then compare the modified date of the file with a copy on a server share
    If the file on the server share is newer then replace the file

    So I can test if the file exists with

    Test-Path C:\Users\$env:username\AppData\LocalLow\Sun\Java\Deployment\security\exception.sites

    Then I can check the last write time with

    Get-Item C:\Users\$env:username\AppData\LocalLow\Sun\Java\Deployment\security\exception.sites | select LastWriteTime

    I can also copy the file from the server with

    Copy-Item "\\server01\share$\java_exceptions\exception.sites" "C:\Users\$env:username\AppData\LocalLow\Sun\Java\Deployment\security\"

    I'm just not sure how to compare the files and string it all together.

    Any help would be much appreciated

    Darren

  • #19180
    Profile photo of Charles Downing
    Charles Downing
    Participant

    You've done most of the work. You just need to add your conditionals. Try something like this:

    # if the file exists already
    if (Test-Path C:\Users\$env:username\AppData\LocalLow\Sun\Java\Deployment\security\exception.sites) {
    	# get local file info
    	$localFile = Get-Item "C:\Users\$env:username\AppData\LocalLow\Sun\Java\Deployment\security\exception.sites"
    	# get remote file info
    	$remoteFile = Get-Item "\\server01\share$\java_exceptions\exception.sites"
    	
    	# if the remote file is newer than the local file
    	if ($remoteFile.LastWriteTime -gt $localFile.LastWriteTime)
    	{
    		Copy-Item "\\server01\share$\java_exceptions\exception.sites" "C:\Users\$env:username\AppData\LocalLow\Sun\Java\Deployment\security\"
    	}
    }
    # if the file does not already exist
    else
    {
    	Copy-Item "\\server01\share$\java_exceptions\exception.sites" "C:\Users\$env:username\AppData\LocalLow\Sun\Java\Deployment\security\"
    }
  • #19194
    Profile photo of Darren Scordecchia
    Darren Scordecchia
    Participant

    Brilliant!

    First run and it works exactly as it should, just need to fire it into login script and test again

    Thanks for your help

    Live long and [s]prosper[/s] PowerShell 🙂

    Darren

  • #19198
    Profile photo of Darren Scordecchia
    Darren Scordecchia
    Participant

    Only had to add one line to get it working 100%

    Just need to add command to create the folder if it didn't exist to the else statement. Then when Java creates the rest of the structure and files the exception.sites file is already there 😀

    New-Item -ItemType directory -Path C:\Users\$env:username\AppData\LocalLow\Sun\Java\Deployment\security

    Thanks again

    Darren

  • #19209
    Profile photo of Charles Downing
    Charles Downing
    Participant

    No worries. Glad it worked!

You must be logged in to reply to this topic.