We help IT Professionals succeed at work.

Powershell Scripting Assistance : Copy file with error correction

101 Views
Last Modified: 2020-09-17
Experts, Looking for additional PowerShell scripting assistance

Function copy-File1 {
Param(
	[Parameter(Mandatory=$true)]
	$FileName
)
	#Drive Locations
	$tempDir = "$($locations.tempdir)"
	$WorkingDir = "$($locations.working_Dir)"
    $tdrivedir = "$($locations.tdrivedir)"
	[string]$filenamenew = "$(&$filenames.cwf_FileNameNew)"
    [string]$Destination = "$(&$filenames.cwf_Destination)"
    [string]$WorkFileloc = "$(&$filenames.cwf_WorkFileLoc)"
	
	#filename Trim
	$FileName = $FileName.Trim()
	If ([string]::IsNullOrEmpty($FileName)) {
		Throw 'Argument FileName is empty!'
	}
	$filePath = Join-Path -Path $tempDir -ChildPath $FileName
	#Identify which File is being Processed
	Write-Host -ForegroundColor Green "Processing File ""$($filePath)"" ..." -NoNewline
	#Test File Destination Path to see if File Exists.. If it Exists Remove it
	If (Test-Path -Path $filePath) {
		Write-Host " $($filePath) Already Exists : Removing File From Temp Location" -NoNewline
		#Error Handling for Remove-Item Command
		Try {
			$itemtemp = Get-Item -Path $filePath -ErrorAction Stop
 			Remove-Item $itemtemp -ErrorAction Stop
			Write-Host -ForegroundColor Green " Successfully Removed File $FileName" -NoNewline
		
		#Add Error Handing for Copy Command
			Write-Host -ForegroundColor Green "Setting $($workingdir) location"
			Set-Location $WorkingDir;
			if (Test-Path -Path $workingdir){
				Try {
					Write-Host -foregroundcolor green "Path Successfully set to $($workingdir)"
					Write-Host -ForegroundColor Green "Copying "$($locations.tempdir)\$filename" to $Destination"
					copy-item -path "$($locations.tempdir)\$filename" -Destination $Destination
					if (Test-Path $filename) {
						Write-Host -foregroundcolor Green "The file $filename was successfully Copied"}

				} Catch {
							Write-Host "Error Message: $($_.Exception.Message)" -ForegroundColor Red;
							Write-Host -foregroundcolor red "$filename was not Copied Successfully"
				}
			
			}else {
					Write-Host "Couldn't set the proper location : script will Terminate";
					pause;
					end}			
		} Catch {
			Write-Host "Error Message: $($_.Exception.Message)" -ForegroundColor Red -NoNewline
		}
	} Else {
		Write-Host " $($filePath) does not exist." -NoNewline
			Write-Host -ForegroundColor Green "Setting $($workingdir) location"
			Set-Location $WorkingDir;
			if (Test-Path -Path $workingdir){
				Try {
					Write-Host -foregroundcolor green "Path Successfully set to $($workingdir)"
					Write-Host -ForegroundColor Green "Copying "$($locations.tempdir)\$filename" to $Destination"
					copy-item -path "$($locations.tempdir)\$filename" -Destination $Destination
					if (Test-Path $filename) {
						Write-Host -foregroundcolor Green "The file $filename was successfully Copied"}

				} Catch {
							Write-Host "Error Message: $($_.Exception.Message)" -ForegroundColor Red;
							Write-Host -foregroundcolor red "$filename was not Copied Successfully"
				}
			
			}else {
					Write-Host "Couldn't set the proper location : script will Terminate";
					pause;
					end}		
	}
}

Open in new window



The code above is a first attempt at a error handling copy file script. While the code appears to be executing correctly, it is not copying the file requested into the proper location as it appears to successfully do.

Thanks in advance for you assistance with this.

Can anyone shead some light on a more efficient way to accomplish this Properly.
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
CERTIFIED EXPERT

Author

Commented:
oBdA, Thanks for the response.

Here are the definitions for what I am using in the script.  :

$locations = ""|Select Working_Dir, tempDir, tdrivedir, local_Working
    $locations.Working_Dir = {\\server1\c$\WORKING_DIR}
    $locations.local_Working = {(get-childitem C:\users\ -Recurse |? {($_.name -like "working_dir") -and ($_.Attributes -like "Directory*")}).fullname}
    $locations.tempDir = {\\server1\c$\WORKING_DIR\Temp}
    $locations.tdrivedir = {\\server2\c$\tempT}

Open in new window


When I try and execute the code above, it is returning with :

Processing file '' ...Action failed:
Error Message: Cannot bind argument to parameter 'Path' because it is null.
Script will terminate
Press Enter to continue...
CERTIFIED EXPERT

Author

Commented:
Sorry for my delay in responding, we are finishing preparations for a hurricane at the moment.
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
You can take your time, I'm still in Kansas ...

$locations.Working_Dir = {\\server1\c$\WORKING_DIR}
...

Open in new window

You're defining a scriptblock here, that the function will then just turn into its string representation. That's overly complicated and hard to understand (because the scriptblock by itself will just fail), with no benefit.
I'd recommend to just use a regular string, because that's what you need and expect anyway.

$locations.local_Working = {(get-childitem C:\users\ -Recurse |? {($_.name -like "working_dir") -and ($_.Attributes -like "Directory*")}).fullname}

Open in new window

This is the only case where the scriptblock would actually work as such (so the $filenames variables are defined like that as well, I guess?).
But it would only make sense if in the time between the scriptblock's definition in the main script and the resolution in the function, the target folder you're looking for might have changed. If this folder is supposed to stay the same during the whole runtime of the script, you're better off with retrieving the folder at the script start and saving the path as a regular string.
And just in case: note that this might return more than one folder.

In other words:
Either
Replace lines 7-12 in my script above with the originals:
	$tempDir = "$($locations.tempdir)"
	$WorkingDir = "$($locations.working_Dir)"
	$tdrivedir = "$($locations.tdrivedir)"
	$filenamenew = "$(&$filenames.cwf_FileNameNew)"
	$Destination = "$(&$filenames.cwf_Destination)"
	$WorkFileloc = "$(&$filenames.cwf_WorkFileLoc)"

Open in new window


Or leave the script as is, and set the variables you're passing to regular strings instead of scriptblocks.
CERTIFIED EXPERT

Author

Commented:
Thanks again oBdA