Solved

Copy Files with powerscript and log failed or success

Posted on 2011-02-17
12
5,607 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
Does Powershell have you tied up in knots?

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 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

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

717 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