Solved

Copy Files with powerscript and log failed or success

Posted on 2011-02-17
12
4,421 Views
Last Modified: 2012-05-11
I have a simple power shell script that copy files from folder to another folder on a server.

How can I make it log success or failed copied files to a log file? I don't want to use robocopy.


copy-item "C:\Folder1\*" "\\Server\Folder1" -Force -Recurse

Open in new window

0
Comment
Question by:eMarketer75
  • 4
  • 4
  • 2
  • +1
12 Comments
 
LVL 16

Expert Comment

by:Bryan Butler
Comment Utility
I don't believe this is possible from the one command.  There is nothing that tell you success or failure, except if it fails it will throw an exception and stop.  One thing that might work is to use the "-PassThru" option which returns an object representing each copied item.  If the object for the item is there, then it has copied.  The only way to verify it would be to write another step that uses hash value comparison.
0
 

Author Comment

by:eMarketer75
Comment Utility
I'm new to powershell. Can you give me an example or a link to reference?
0
 
LVL 16

Expert Comment

by:Bryan Butler
Comment Utility
If you add the parameter, you should get back an object for each file:

copy-item "C:\Folder1\*" "\\Server\Folder1" -Force -Recurse -passthru

This post might help:  
http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_25027300.html

Still, if it bombs, it's not going to return anything, just throw and exception and stop.
0
 
LVL 5

Expert Comment

by:daveTechSearch
Comment Utility
Just tried this on a couple of my systems and seems to work well
$source = 'C:\folder1\*.*'
$destination = '\\server\Folder1\'
$logProgress = 'c:\temp\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}
}

Open in new window

0
 
LVL 16

Expert Comment

by:Bryan Butler
Comment Utility
Nice.  Seems like you would need the "passthru" for this to work, but I guess not.  Did you test it failing?
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 5

Expert Comment

by:daveTechSearch
Comment Utility
minor change to script to account for the recursion
$source = 'C:\folder1\'
$destination = '\\server\Folder1\'
$logProgress = 'c:\temp\copyLog.txt'

get-childitem $source -recurse | foreach {
copy $_.fullname $destination -Force -Recurse -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}
}

Open in new window

0
 
LVL 5

Accepted Solution

by:
daveTechSearch earned 500 total points
Comment Utility
@ developedtester.... yes... to test a failed copy i used a path that my regular ID does not have access to.

also, cleaned up the output to log a little (so each entry per file is on one line)
$source = 'C:\folder1\'
$destination = '\\server\Folder1\'
$logProgress = 'c:\temp\copyLog.txt'

get-childitem $source -recurse | foreach {
copy $_.fullname $destination -Force -Recurse -errorAction silentlyContinue
if($? -eq $false){echo "$($_.fullname) did not copy ok to $destination" | out-file -append $logProgress} 
else 
{write-output "$($_.fullname) copied OK to $destination" | out-file -append $logProgress}
}

Open in new window

0
 
LVL 16

Expert Comment

by:Bryan Butler
Comment Utility
Very nice Dave.  Then it does return $true/$false for each copy.  
0
 

Author Comment

by:eMarketer75
Comment Utility
Wow!! Thank you. I will test that script out.
0
 
LVL 5

Expert Comment

by:daveTechSearch
Comment Utility
Just following up... did this work for you?
0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
This question has been classified as abandoned and is being closed as part of the Cleanup Program. See my comment at the end of the question for more details.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now