Solved

Copy Files with powerscript and log failed or success

Posted on 2011-02-17
12
5,380 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +1
12 Comments
 
LVL 16

Expert Comment

by:Bryan Butler
ID: 34922985
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
ID: 34926720
I'm new to powershell. Can you give me an example or a link to reference?
0
 
LVL 16

Expert Comment

by:Bryan Butler
ID: 34947109
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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 5

Expert Comment

by:daveTechSearch
ID: 34947306
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
ID: 34947339
Nice.  Seems like you would need the "passthru" for this to work, but I guess not.  Did you test it failing?
0
 
LVL 5

Expert Comment

by:daveTechSearch
ID: 34947365
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
ID: 34947554
@ 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
ID: 34955363
Very nice Dave.  Then it does return $true/$false for each copy.  
0
 

Author Comment

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

Expert Comment

by:daveTechSearch
ID: 35070687
Just following up... did this work for you?
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 35399105
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A safe way to clean winsxs folder from your windows server 2008 R2 editions
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

734 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