troubleshooting Question

Powershell Scripting Assistance : Copy file with error correction

Avatar of ITguy565
ITguy565Flag for United States of America asked on
PowershellScripting Languages
5 Comments1 Solution106 ViewsLast Modified:
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}		
	}
}


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.
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros