Powershell File Copy Error Handling
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring powershell error handling copy-item developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask
Powershell Error Handling Best Practices
Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join powershell error handling function them; it only takes a minute: Sign up powershell error checking during file copy with recursion up vote 2 down vote favorite I have a program that copies folders and files recursively. example: Copy-Item -path "$folderA" -destination "$folderB" -recurse
Powershell Error Handling Try Catch
Sometimes the files do not copy. Is there a way to "step inside the recursion" or a better way to do it, so I can enable some kind of error checking during the process rather than after wards. Possibly even do a Test-Path and prompt for a recopy? powershell recursion error-handling copy-item share|improve this question asked Sep 28 '10 at 2:05 alphadev 40421120 stej - didn't realize that was something to do with answered questions - went powershell error handling silentlycontinue back and updated them - thx –alphadev Sep 29 '10 at 2:54 add a comment| 1 Answer 1 active oldest votes up vote 2 down vote accepted You can. For example the following code snippet will actually copy and check each file for possible errors. You can also put your custom code at the beginning to check for some prerequisites: get-childItem $source -filter *.* | foreach-object { # here you can put your pre-copy tests... copy-item $_.FullName -destination $target -errorAction SilentlyContinue -errorVariable errors foreach($error in $errors) { if ($error.Exception -ne $null) { write-host -foregroundColor Red "Exception: $($error.Exception)" } write-host -foregroundColor Red "Error: An error occured during copy operation." } } share|improve this answer answered Sep 28 '10 at 4:02 David Pokluda 6,52441926 add a comment| Your Answer draft saved draft discarded Sign up or log in Sign up using Google Sign up using Facebook Sign up using Email and Password Post as a guest Name Email Post as a guest Name Email discard By posting your answer, you agree to the privacy policy and terms of service. Not the answer you're looking for? Browse other questions tagged powershell recursion error-handling copy-item or ask your own question. asked 6 years ago viewed 8966 times active 6 years ago Related 1Powershell: How to copy a file from TFS to a different destination8Get the list of files that are getting copied in powershell1Powershell Copy-Item - Access Denied0H
Lima 2 years, 2 months ago. Author Posts August 3, 2014 at 12:41 am
Powershell Error Handling The Rpc Server Is Unavailable
#17731 Wallace LimaParticipant Hello guys Thanks to you I was
Powershell Error Handling Non-terminating
able to resolve many issues in these cases of copy (one-to-one, one-to-many, with all kinds powershell error handling trap of exceptions that I needed). But still can not find a simple way to log success or failure example: $a = Get-Content c:\temp\servers.txt foreach ($i http://stackoverflow.com/questions/3809199/powershell-error-checking-during-file-copy-with-recursion in $a) { Copy-Item "C:\temp\Source\*" -Recurse -Destination "\\$i\c$\destination\" -force -EA SilentlyContinue } Works great, but I do not know how to extract a log if some of these servers is offline, if any file on the target can not be overwritten, etc. sorry for misspelling thank you very much August https://powershell.org/forums/topic/log-for-copy-item/ 3, 2014 at 1:27 am #17732 Istvan SzarkaParticipant You can put any error messages generated by a cmdlet into a variable with the ErrorVariable common parameter. Then, you can save the error in a file by piping the variable into Out-File. PS C:\Windows\system32> Get-Service kkkkkk -ErrorVariable e Get-Service : Cannot find any service with service name 'kkkkkk'. At line:1 char:1 + Get-Service kkkkkk -ErrorVariable e + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (kkkkkk:String) [Get-Service], ServiceCommandException + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand PS C:\Windows\system32> $e Get-Service : Cannot find any service with service name 'kkkkkk'. At line:1 char:1 + Get-Service kkkkkk -ErrorVariable e + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (kkkkkk:String) [Get-Service], ServiceCommandException + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand PS C:\Windows\system32> $e | Out-File c:\error.txt You can append other errors to the error varible like this: Get-Service bbbbbbbbb -ErrorVariable +e You can also have a look at the help file: about_CommonParameters I hope it h
last updated by Graham Beer 1 year, 6 months ago. Author Posts April 8, 2015 at 3:49 am #24064 Graham BeerParticipant Hi, I've written a copy script https://powershell.org/forums/topic/copy-script-right-usage/ which i am going to use in an SCCM 2012 R2 task sequence. Would like to know if i have used things in the correct way as part of my learning process..Thanks ! #Set Locations #$StartLocation = "%windir%\temp" #$Destination = "C:\ProgramData\Microsoft\User Account Pictures" #Remove unrequired file(s) [CmdletBinding(SupportsShouldProcess=$true)] Param ( ) Process { #Set Variables $ErrorAction = 'Stop' $StartLocation = "%windir%\temp" $Destination = "C:\ProgramData\Microsoft\User Account Pictures" error handling Try { $remove = get-childitem "$Destination" -Include *.dat -Recurse | Remove-item } Catch { } If (!$remove) { Write-Verbose -Message "An error occurred whilst attempting to remove $remove" Break } #Copy Avatar files to location Try { Get-ChildItem -path $StartLocation -Recurse -include "*.bmp","*.png" | Foreach-Object { Copy-Item -path $_ -Destination $Destination } } Catch { Write-Verbose -Message "Failed to copy files to $Destination" Break } powershell error handling } April 8, 2015 at 6:47 am #24072 Don JonesKeymaster Ah... well, did you have any more specific questions? I mean, if it works, that's 90% of the battle! Usually, you can pipe directly from Get-ChildItem to Copy-Item, which would probably run a tiny bit faster than using ForEach object. I don't think your Catch block will ever run, because Copy-Item normally doesn't throw a terminating error. You'd need to add [b]-ErrorAction Stop[/b] to have it generate a catchable, terminating exception. That's true for your earlier Try/Catch, too - you might review "The Big Book of PowerShell Error Handling" (free ebook from our Resources menu) to learn more about that. Oh, I see - you set $ErrorAction to 'Stop.' That won't work. It's $ErrorActionPreference, and setting it globally is considered a poor practice in most situations. Certainly where you're not catching it globally. Also, I'm not sure your intention, but as-is a single file copy failure will abort all remaining files, once your Try/Catch is working with -ErrorAction. If you were to get the files and then enumerate them, you could have it fail one file and continue trying others. Again, I'm not su