Solved

Powershell copy script

Posted on 2014-10-22
8
384 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 69

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

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

785 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