Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 400
  • Last Modified:

Powershell copy script

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
rakkad
Asked:
rakkad
  • 4
  • 2
  • 2
1 Solution
 
becraigCommented:
$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
 
rakkadAuthor Commented:
Can you explain how the code works and what is $filed variable ?

Thanks for your speedy response
0
 
becraigCommented:
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
QlemoC++ DeveloperCommented:
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
 
becraigCommented:
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
 
QlemoC++ DeveloperCommented:
@becraig:  " \\cslwinapp10\bacs"  - still wrong!
0
 
becraigCommented:
@ 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
 
rakkadAuthor Commented:
Excellent
0

Featured Post

Industry Leaders: 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!

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now