Solved

Powershell copy script

Posted on 2014-10-22
8
380 Views
Last Modified: 2014-11-09
Hi

Can someone help me, I am trying to write a Powershell script that will do the following:-

1. Check certain files e.g bacs1.txt, bacs2.txt, bacs3.txt exist in a file location \\cslwinapp06\bacs, check that three files exist if not report, log error 'files don't exist'
2. if these three files exist in \\cslwinapp06\bacs copy these files to \\cslwinapp10\bacs
3. Check that these files have been copied to \\cslwinapp10\bacs and report a log to confirm 'files copied'

Thanks
0
Comment
Question by:rakkad
  • 4
  • 2
  • 2
8 Comments
 
LVL 29

Accepted Solution

by:
becraig earned 500 total points
ID: 40397940
$filer = gci \\cslwinapp06\bacs | ?{ $_.name -like " bacs*.txt" }
if ($filer.count -ne 3) { write-host "The expected number of files does not exist... exiting !!" -fore RED }
else
{
	$filer | % {
		$filed = $_.fullname
		$x = Copy-Item -Path $_.fullname -Destination " \\cslwinapp10\bacs" -PassThru -ErrorAction silentlyContinue
		if ($x) { "Copy of $filed successful" | out-file c:\somelog.log -append }
		else { "Copy failure" }
		
	}
	
	

Open in new window


Edited to include definition of $filed variable
0
 

Author Comment

by:rakkad
ID: 40397992
Can you explain how the code works and what is $filed variable ?

Thanks for your speedy response
0
 
LVL 29

Expert Comment

by:becraig
ID: 40398015
first we check the folder for any files matching bacs<anything>.txt
$filer = gci \\cslwinapp06\bacs | ?{ $_.name -like " bacs*.txt" }

Then we  count how many files we found that match bacs*.txt (since you indicate you expect ONLY 3 we exit is more or less than 3 files are found matching the naming convention
if ($filer.count -ne 3) { write-host "The expected number of files does not exist... exiting !!" -fore RED }

If ONLY 3 files are found matching the naming convention as you requested we copy them to the destination
else
{
We start by piping each file found into a for each loop
     $filer | % {
Then we create a variable name for the current file ($filed) so we have it for future use if needed
           $filed = $_.fullname
We now call copy item to copy from source to destination and use passthru so the value from copy is available as well as silently continue in the event we get an error
           $x = Copy-Item -Path $_.fullname -Destination " \\cslwinapp10\bacs" -PassThru -ErrorAction silentlyContinue

Then if we get a value for $x we know the copy suceeded and we log it to a log file as requested
           if ($x) { "Copy of $filed successful" | out-file c:\somelog.log -append }
However if the copy fails we just write to screen that it failed
           else { "Copy failure" }
            
      }
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 40398183
This could and should be improved. In particular I would expect to see the file which did not make it over to the destination. And there is leading space in the destination path which should be not there.
$logfile = 'C:\Temp\EE\Logfile.txt'
$filer = gci \\cslwinapp06\bacs\bacs[123].txt
if ($filer.count -ne 3) {
  Out-File $logfile -Append -Input "The expected number of files does not exist... exiting !!"
} else {
  $x = $filer | Copy-Item -Destination \\cslwinapp10\bacs -PassThru -EA silentlyContinue
  if ($x.Count -eq 3) {
     Write-Host "all files copied successfully"
  } else {
     $missing = 1..3 | ? { ($fiiler | select -Expand Name) -notcontains "bacs$_.txt" }
     Out-File $logfile -Append -Input "Error copying $missing"
}

Open in new window

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 29

Expert Comment

by:becraig
ID: 40398199
I agree while the same could be accomplished by just utilizing the variable already added:
As for the overall script I can see a lot of ways to do what I think I would want it to so, however that would not be answering the "Specific" request posed in the question.


$filer = gci \\cslwinapp06\bacs | ?{ $_.name -like " bacs*.txt" }
if ($filer.count -ne 3) { write-host "The expected number of files does not exist... exiting !!" -fore RED }
else
{
	$filer | % {
		$filed = $_.fullname
		$x = Copy-Item -Path $_.fullname -Destination " \\cslwinapp10\bacs" -PassThru -EA silentlyContinue
		if ($x) { "Copy of $filed successful" | out-file c:\somelog.log -append }
		else { "Copy failure for $filed" | out-file c:\somefaillog.log -append  }
		
	}

Open in new window


I logged in both instances since it was what was requested.
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 40398225
@becraig:  " \\cslwinapp10\bacs"  - still wrong!
0
 
LVL 29

Expert Comment

by:becraig
ID: 40398230
@ Qlemo perfectly correct !
whitespace now  removed lol

$filer = gci \\cslwinapp06\bacs | ?{ $_.name -like " bacs*.txt" }
if ($filer.count -ne 3) { write-host "The expected number of files does not exist... exiting !!" -fore RED }
else
{
	$filer | % {
		$filed = $_.fullname
		$x = Copy-Item -Path $_.fullname -Destination "\\cslwinapp10\bacs" -PassThru -EA silentlyContinue
		if ($x) { "Copy of $filed successful" | out-file c:\somelog.log -append }
		else { "Copy failure for $filed" | out-file c:\somefaillog.log -append  }
		
	}

Open in new window

0
 

Author Closing Comment

by:rakkad
ID: 40430959
Excellent
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
A brief introduction to what I consider to be the best editor for PowerShell.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now