Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 8583

3 different behaviors for the same script

$
0
0

Folks

I have a script to copy backup files from local server to a remote archive location.

Delete the files on the archive location that are more than 5 days old.

The same exact script gives three different results:

1. it works well on some servers.

2. on one server it copies the files over irrespective of the age of the file and changes their time stamp

3. on another set of servers, it copies the files irrespective of their age.

Here's the script

=====================================================


#============================================================================
# BEGIN: Config Pt 1

# Where are the local files to be copied?
$source = "Z:\SQLBAK"

# Where are the folders going? \ $destination \ SERVERNAME \ sub folders
$destination = "\\bossqlbak\bossqlbak\"
$destinationLogs = "\\bossqlbak\bossqlbak\$hostname\PS_logs\"

# Files older than this many days shall be deleted...
$deleteOlderThan = "5"

# END: Config Pt 1
#============================================================================#

#============================================================================

# Get the local host name
$hostname = $env:COMPUTERNAME
# Redefine the destination target folder
$destination = Join-Path $destination -childpath $hostname
# Create the destination target folder
New-Item $destination -type directory -ErrorAction SilentlyContinue
# Create the destination logs folder if needed
New-Item $destinationLogs -type directory -ErrorAction SilentlyContinue
# Current date and time
$dateAndTime = get-date -format MMddyyHHmmss
function write-log([string]$info){
       $path = $script:logfile
       if(!(Test-Path -Path $path))
       {
              new-item -Path $path -Value "new file" –itemtype file
       }
    if($loginitialized -eq $false){           
        $FileHeader > $logfile           
        $script:loginitialized = $True           
    }
    $timestamp = Get-Date –f o
    $info = $timestamp + " " + $info 
    $info >> $logfile
}
$script:logfile = "$destinationLogs$hostname-$dateAndTime.txt"
$script:Seperator = ""       
$script:loginitialized = $false           
$script:FileHeader = @"
$seperator
***$hostname Process Information***
"@
#
#============================================================================
$Now = Get-Date
# define LastWriteTime parameter based on $deleteOlderThan
$LastWrite = $Now.AddDays(-$deleteOlderThan)

write-log $lastwrite
write-log "==========STARTED======================================================="
write-log "========================================================================"
write-log "BEGIN: Removal of files in $destination older than $deleteOlderThan days."
write-log "========================================================================"
$Files = Get-Childitem $destination -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}
foreach ($File in $Files)
    {
    if ($File -ne $NULL)
        {
        write-log "Deleting File $File"
        Remove-Item  $File.FullName -ErrorAction SilentlyContinue
        }
    else
        {
        write-log "No more files to delete!"
        }
    }
write-log "========================================================================" 
write-log "END: Removal of files in $destination older than $deleteOlderThan days."
write-log "========================================================================"
write-log "BEGIN: Copy all files to $destination."
write-log "========================================================================"

foreach ($i in Get-ChildItem $source)
{
       write-log "COPYING: $i.FullName"
       Copy-Item -recurse $i.FullName $destination
}

write-log "==========DONE=========================================================="

the Write-Log part I can easily remove as it is not adding any serious value.

 

Any input is welcome. please.


Viewing all articles
Browse latest Browse all 8583

Trending Articles