Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Copy Files with powerscript and log failed or success

Posted on 2011-02-17
12
Medium Priority
?
6,285 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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

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

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

Are your AD admin tools letting you down?

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

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…
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
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 …
Loops Section Overview
Suggested Courses

610 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