Solved

Copy Files with powerscript and log failed or success

Posted on 2011-02-17
12
4,863 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
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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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 69

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

809 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