Link to home
Start Free TrialLog in
Avatar of tfontanilla
tfontanilla

asked on

powershell copy-item with success or fail notification logs

i current have a backup script with ability to send me an email confirming backup is finished.
I need to revise the script as follow;
Check latest backup=
Latest backup not availabe, copy the day before=
if copy success, output log "Backup copy successfully"
if copy failed, output log "Backup copy failed"

This my script, it work by i need modify like i said above. Thank for all potential help!

$source = 'C:\path'
$destination = '\\path'
$logProgress = 'c:\path\copyLog.txt'
get-childitem $source -recurse | foreach {
copy $_.fullname $destination -errorAction silentlyContinue
if($? -eq $false){echo "$source did not copy ok to $destination" | out-file -append $logProgress}
else
{echo "$source copied OK to $destination" | out-file -append $logProgress}
}
Send-MailMessage -To email.com `
-From senders `
-Subject "Backup" `
-attachment c:\logs\copyLog.txt `
-Body "The Bakup is finished. to see data backup, please check path!" `
-SmtpServer mail4.creditriskmonitor.com
get-childitem c:\path -recurse | where {$_.lastwritetime -lt (get-date).adddays(-2) -and -not $_.psiscontainer} |% {remove-item $_.fullname -force}
get-childitem c:\path -recurse | where {$_.lastwritetime -lt (get-date).adddays(-2) -and -not $_.psiscontainer} |% {remove-item $_.fullname -force}
get-childitem \\desk77\c$\eCASBackup -recurse | where {$_.lastwritetime -lt (get-date).adddays(-2) -and -not $_.psiscontainer} |% {remove-item $_.fullname -force}
Send-MailMessage -To email.com `
-From email.com `
-Subject "old back deleted" `
-Body "eCASBackup old back deleted" `
-SmtpServer domain
Avatar of Bryan Butler
Bryan Butler
Flag of United States of America image

What do you mean when you say "Check latest backup" ?  Is there a folder with a datestamp?  Or do you want to check the log?  Same issues with "copy the day before".  How do we know if the latest backup is available?  And where the "day bofore" is located?
Avatar of tfontanilla
tfontanilla

ASKER

i wanted to to use this:
It basically sort the folder by the lastwritetime.

ls -path $source -fi $filter | sort-object LastWriteTime | select-object -last 1 | copy-item -dest $dest
This what i just created, but it's not working.

$source = "C:\path"
$filter = "*.arc"
$dest = "C:\path"
$logProgress = 'c:\path\copyLog.txt'

get-childitem C:\path -recurse | where {$_.lastwritetime -lt (get-date).adddays(-5) -and -not $_.psiscontainer} |% {remove-item $_.fullname -force}
##########################################################################################################################################################################

ls -path $source -fi $filter | sort-object LastWriteTime | select-object -last 1 | copy-item -dest $dest  | foreach {
copy $_.fullname $destination -errorAction silentlyContinue
if($? -eq $false){echo "$source did not copy ok to $destination" | out-file -append $logProgress}
else
{echo "$source copied OK to $destination" | out-file -append $logProgress}
}

########################################################################################################
get-childitem \\C$\path -recurse | where {$_.lastwritetime -lt (get-date).adddays(-5) -and -not $_.psiscontainer} |% {remove-item $_.fullname -force}
Send-MailMessage -To email.com `
-From email.com `
-Subject "Backup" `
-attachment c:\logs\copyLog.txt
-Body "The Bakup is finished Backup!" `
-SmtpServer domain.com
This line:
ls -path $source -fi $filter | sort-object LastWriteTime | select-object -last 1 | copy-item -dest $dest  | foreach ....

copies the item, then the foreach loop copies it too.  I think it will work if you take it out and make it:

ls -path $source -fi $filter | sort-object LastWriteTime | select-object -last 1 | foreach....
Thank again for youe post.
it works on successful copying, but the failed copy is not working. I tried to create a false foder to generate error copying, but not working.
i would like to time or date stamp on the log and overwrite the current log, so i dont have any redundant.
Try adding "-ErrorVariable err" to your copy command, then when checking, do:

if($err){

To get the log, add this to the end of your copy: 2>&1 $(get-date -f yyyy-MM-dd)-Logfile.txt

copy $_.fullname $destination -errorAction silentlyContinue -ErrorVariable err 2>&1 $(get-date -f yyyy-MM-dd)-Logfile.txt


Thank you for your quick response.

This is the current script. I was able to get the date on the log.
The problem is that the error is not working. I will try what you wrote.

$source = 'C:\"VeraSMART Data"\Archives'
$destination = '\\desk77\c$\eCASBackup\eCASBak'
$filter = '*.arc'
$date = (get-date)
$logProgress = 'c:\crmz3\logs\backupLog.txt'
get-childitem C:\"VeraSMART Data"\Archives -recurse | where {$_.lastwritetime -lt (get-date).adddays(-3) -and -not $_.psiscontainer} |% {remove-item $_.fullname -force}
ls -path $source -fi $filter | sort-object LastWriteTime | select-object -last 1 |  foreach {
copy $_.fullname $destination -errorAction silentlyContinue
if($? -eq $false){echo "$date $source failed backup to $destination" | out-file -append $logProgress}
else
{echo "$date $source Backup is successfully copied to $destination" | out-file -append $logProgress}
}
copy $_.fullname $destination -errorAction silentlyContinue -ErrorVariable err 2>&1 $(get-date -f yyyy-MM-dd)-Logfile.txt

did not work.
What did it do?  You will have to check the variable afterwards.  Such as:

if($err){echo "$source did not copy ok to $destination" | out-file -append $logProgress}
else ....

Also, try just "2>":

copy $_.fullname $destination -errorAction silentlyContinue -ErrorVariable err 2> $(get-date -f yyyy-MM-dd)-Logfile.txt


This the error message:

Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.

PS c:\> $source = "C:\Data\Archives"
PS c:\> $filter = "*.arc"
PS c:\> $date = (get-date)
PS c:\> $destination = "\\desk77\c$\Backup\eCASBak"
PS c:\> $logProgress = 'c:\crmz3\logs\backupLog.txt'
PS c:\> ls -path $source -fi $filter | sort-object LastWriteTime | select-object -last 1 | foreach {
>> copy $_.fullname $destination -errorAction silentlyContinue -ErrorVariable err 2> $(get-date -f yyyy-MM-dd)-Logfile.t
xt
>> if($? -eq $false){echo "$date $source failed backup to $destination" | out-file -append $logProgress}
>> else
>> {echo "$date $source Backup is successfully copied to $destination" | out-file -append $logProgress}
>> }
>>
Copy-Item : A parameter cannot be found that matches parameter name 'Logfile'.
At line:2 char:116
+ copy $_.fullname $destination -errorAction silentlyContinue -ErrorVariable err 2> $(get-date -f yyyy-MM-dd)-Logfile <
<<< .txt
    + CategoryInfo          : InvalidArgument: (:) [Copy-Item], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.CopyItemCommand

Ooops, put quotes around it:

"$(get-date -f yyyy-MM-dd)-Logfile.txt"
Thanks! it work. Now all i need is to output into the log the amount of data copied.

$source = "C:\Data\Archives"
$filter = "*.arc"
$date = (get-date)
$destination = "\\desk77\c$\Backup\eCASBak"
$logProgress = 'c:\crmz3\logs\backupLog.txt'
ls -path $source -fi $filter | sort-object LastWriteTime | select-object -last 1 | foreach {
copy $_.fullname $destination -errorAction silentlyContinue -ErrorVariable err 2> "$(get-date -f yyyy-MM-dd)-Logfile.txt
if($? -eq $false){echo "$date $source failed backup to $destination" | out-file -append $logProgress}
else
{echo "$date $source Backup is successfully copied to $destination" | out-file -append $logProgress}
}
Amount as in size or number of files?

$source = "C:\Data\Archives"
$filter = "*.arc"
$date = (get-date)
$destination = "\\desk77\c$\Backup\eCASBak"
$logProgress = 'c:\crmz3\logs\backupLog.txt'
$size = 0
$num = 0
ls -path $source -fi $filter | sort-object LastWriteTime | select-object -last 1 | foreach {
  $fileobj = $_
  copy $fileobj.fullname $destination -errorAction silentlyContinue -ErrorVariable err 2> "$(get-date -f yyyy-MM-dd)-Logfile.txt
  if($? -eq $false){echo "$date $source failed backup to $destination" | out-file -append $logProgress}
  else
  {
    $size += $fileobj.Length
    $num += 1
    echo "$date $source Backup is successfully copied to $destination" | out-file -append $logProgress
  }
}
$num
$size
Sorry for the confusion. What i meant to say Amount as in size
That would be the $size then.  You can remove the $num counter.  

echo "Total size of files is:  $size" | out-file -append $logProgress
Thanks for quick response.

I tried the script, and it looks like it getting stock. I just see ">>" sign not doing  anything.

Here it is.
$source = "C:\Data\Archives"
$filter = "*.arc"
$date = (get-date)
$destination = "\\desk77\c$\Backup\eCASBak"
$logProgress = 'c:\crmz3\logs\backupLog.txt'
$size = 0
$num = 0
ls -path $source -fi $filter | sort-object LastWriteTime | select-object -last 1 | foreach {
  $fileobj = $_
  copy $fileobj.fullname $destination -errorAction silentlyContinue -ErrorVariable err 2> "$(get-date -f yyyy-MM-dd)-Logfile.txt
  if($? -eq $false){echo "$date $source failed backup to $destination" | out-file -append $logProgress}
  else
  {
    $size += $fileobj.Length
    $num += 1
    echo "$date $source Backup is successfully copied to $destination" | out-file -append $logProgress
      }
}
$size
echo "Total size of files is:  $size" | out-file -append $logProgress
>>
>>
>>
ASKER CERTIFIED SOLUTION
Avatar of Bryan Butler
Bryan Butler
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks again.

The good new is that i'm getting the error message on the logs folder. The bad news is I'm getting this error message. i check the path it is the correct one. My script is below.

Copy-Item : The network path was not found.
At line:3 char:5
+ copy <<<<  $fileobj.fullname $destination -recurse -errorAction silentlyContinue -ErrorVariable err 2> "$(get-date -f
 yyyy-MM-dd)-Logfile.txt"
    + CategoryInfo          : NotSpecified: (:) [Copy-Item], IOException
    + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.CopyItemCommand

My script.

$source = "C:\VeraSMART Data\Archives"
$filter = "*.arc"
$date = (get-date)
$destination = "\\desk77\c$\Backup\eCASBak"
$logProgress = 'c:\crmz3\logs\backupLog.txt'
$size = 0
$num = 0
ls -path $source -fi $filter | sort-object LastWriteTime | select-object -last 1 | foreach {
$fileobj = $_
copy $fileobj.fullname $destination -recurse -errorAction silentlyContinue -ErrorVariable err 2> "$(get-date -f yyyy-MM-dd)-Logfile.txt"
if($? -eq $false){echo "$date $source failed backup to $destination" | out-file -append $logProgress}
else
{
$size += $fileobj.Length
$num += 1
echo "$date $source Backup is successfully copied to $destination" | out-file -append $logProgress}
$size
echo "Total size of files is:  $size" | out-file -append $logProgress}
Well, I'm not sure what's up.  You say you checked the path...were you checking "\\desk77\c$\Backup\eCASBak"?  Also, you changed from ""C:\VeraSMART Data\Archives" to "C:\Data\Archives"...does the new one exist?  You said things worked before so there must be some small change that messed it up.  
I made some modification on the script, now it work perfectly. thank you so much for all your efforts on this task. I have another question posted, maybe you can help :)

Title:
Crentralize Update for Adobe Reader 9.4.5 without admin rights
I've requested that this question be closed as follows:

Accepted answer: 0 points for tfontanilla's comment http:/Q_27236709.html#36335180

for the following reason:

Genius!
Did you mean to close this without points?  
I'll see what I can do with the other question.
I didi provide you with all the points. Sorry for the confusion.