Solved

Copy File and Log

Posted on 2013-01-16
11
276 Views
Last Modified: 2013-02-05
I want to create a script that will copy one file from one fixed location on my computer to a fixed location on a share drive. The file will exist on the share drive, so I will overwrite it.

Once the copy has finished, I want to write to the event log the status. The copy could fail if the source file is locked by another applicaion/user, so I need to detect failure and record either success or failure to the event log.

I do not need any kind of recovery if it fails.
0
Comment
Question by:dbbishop
[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
  • 6
  • 4
11 Comments
 
LVL 15

Author Comment

by:dbbishop
ID: 38783137
A little change. I need to copy all of the files in a specific folder to a destination and log the result. If any one file cannot be copied, the action is considered a failure, although I do not need to log the specific file that couldn't be copied, if that can be easily obtaned, it would be nice.
0
 
LVL 7

Expert Comment

by:karunamoorthy
ID: 38783164
you need some programming effort for eg. vb
0
 
LVL 15

Author Comment

by:dbbishop
ID: 38783774
I want to do it in PowerShell.
0
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.

 
LVL 15

Author Comment

by:dbbishop
ID: 38783966
I've been playing around a bit and I've come up with this code for doing the copy. However, it appears the full directory structure needs to be in place for files to copy properly. Although I can create the structure before-hand, I am concerned if a folder gets added in the source path, how to make sure it, and its contents gets copied to the destination path. Any ideas?

$sourceDir = 'E:\sourcepath'
$targetDir = 'g:\destinationpath'

get-childitem -path $sourceDir -recurse |
    foreach { Copy-Item $_.FullName -destination $targetDir -recurse -force }
0
 
LVL 40

Expert Comment

by:Subsun
ID: 38784028
Try this script.. The script will copy C:\Dumps\shell.txt to the share \\Server\Users\Test and log Event ID 20399 with status.
 
$Src = "C:\Dumps\shell.txt"
$Dest = "\\Server\Users\Test"
$eventLogID = 20399
$logFilePrefix = "FileCopyStat"
	
function Write-Event{
		$msg = $args[0]
		$source = $args[1]
		$type = $args[2]
		$eventid = $args[3]
		if(![System.Diagnostics.EventLog]::SourceExists($source))
			{
				[System.Diagnostics.EventLog]::CreateEventSource($source,'Application')
			}
		$log = New-Object System.Diagnostics.EventLog 
		$log.set_log("Application") 
		$log.set_source($source)
		$log.WriteEntry($msg,$type,$eventid)
}

Try 
	{
		Copy-Item $Src $Dest -ErrorAction Stop
		write-event $("The file $($Src) was successfully copied to $($Dest)") $logFilePrefix "Information" $eventLogID
	}
catch [System.Exception]
	{
		write-event $_.Exception.Message $logFilePrefix "Error" $eventLogID
	}

Open in new window

0
 
LVL 40

Expert Comment

by:Subsun
ID: 38784719
I am concerned if a folder gets added in the source path, how to make sure it, and its contents gets copied to the destination path. Any ideas?

If you are copying a directory then you can use -recurse parameter with Copy-Item which creates a destination folder structure. When the source is a file then destination directory should be there.. There are workarounds..

You can add a If condition to test whether the destination folder exist or not..
$Dest= "C:\test\Folder"
if (!(Test-Path -path $Dest)) {New-Item $Dest -Type Directory}
Copy-Item $Src $Dest

Open in new window

Or try..
$Dest= "C:\test\Folder\file.txt"
New-Item -ItemType File -Path $Dest -Force
Copy-Item $source $Dest -Force

Open in new window

0
 
LVL 15

Author Comment

by:dbbishop
ID: 38785135
subsun: I'd like to do this on a foreach loop, as I would not necessarily know, at the time of execution, the folder name(s) and there couldbe more than one. In other words, a 'parent' subfolder could have been added and a complete hierarchy of subfolders under it. I need to make sure all of the folders in the source path exist in the destination or are created.

I am playing around with this some on my own but work s rather overwhelming right now and my time available for research is limited. Normally, I'd likely go out and figure out how to do this all on my own.
0
 
LVL 40

Expert Comment

by:Subsun
ID: 38785168
Do you want an event for each file & folder which is copied?
0
 
LVL 15

Author Comment

by:dbbishop
ID: 38794238
No, just for the full process. If it fails, then I know at least one file did not copy. Actually, if not to much trouble, if all files copy, then a single succeed event written, else a fail event for each file that did not copy, along with the filename. But if too much trouble, then a single pass/fail event.
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 38794737
Try this..
$Src = "C:\Documents"
$Dest = "\\Server\Users\Test"
$eventLogID = 20399
$logFilePrefix = "FileCopyStat"
function Write-Event{
		$msg = $args[0]
		$source = $args[1]
		$type = $args[2]
		$eventid = $args[3]
		if(![System.Diagnostics.EventLog]::SourceExists($source))
			{
				[System.Diagnostics.EventLog]::CreateEventSource($source,'Application')
			}
		$log = New-Object System.Diagnostics.EventLog 
		$log.set_log("Application") 
		$log.set_source($source)
		$log.WriteEntry($msg,$type,$eventid)
}
$error.Clear()
Copy-Item $Src $Dest -Recurse -Force -ErrorAction SilentlyContinue
if ($error.Count -eq 0){
write-event ("The files at "+$Src+" was successfully copied to "+$Dest) $logFilePrefix "Information" $eventLogID
}
Else{
$Error | % {write-event $_ $logFilePrefix "Error" $eventLogID}
}

Open in new window

0
 
LVL 15

Author Closing Comment

by:dbbishop
ID: 38857161
Sorry it has taken so long to respond. This was very low on the priority list of tasks to accomplish, and I've been working 12-hour days on high priority stuff. I have not tested this, but in looking it over, I believe it will work. If not, it should only require minor tweaks. Thank you.
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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…

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