How handle errors with copy-item - powershell

I have a backup script that copies several directories of files to a backup location. Unfortunately, not all of the files in the folder are accessible. What the history is, is that they were archived, and what's left is a filename with a grey x on it. When I try to copy it, I get the following message:
Copy-Item : Access to the path 'E:\Backup\Loc\DO\zOLD_Under Review for NOT USED_keep for now\2006-06\N.doc' is denied.
At C:\Users\me\Documents\powershellFiles\Backup.ps1:13 char:4
+ Copy-Item -Path $SourcePath -Destination $DestinationPath -Force - ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (N.doc:FileInfo) [Copy-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : CopyDirectoryInfoItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.CopyItemCommand
Yes, it's complaining at the To Location, not the From Location. I have opened up the directory/files so it's not read only, but still get this.
Also, getting through these copy errors takes a really long time. If I could avoid trying to copy these files in the first place, it would be much quicker. There's probably 200 of these files that are archived.
However, I'm copying the folder, not the filenames individually. There are ones that aren't archived in that folder. There isn't a plan to clean them up. I'm trying to identify when an error occurs, but it's only hitting my breakpoint if $error.Exception -ne $null statement after it writes the errors to screen and takes forever failing (see comment).
Any idea how I can either filter out the ones that are archived, or grab them and check them against an array or list so I don't get an error message? I haven't figured out how to find them as they happen since it's copying the entire folder.
I was looking at error checking during copy-item but I don't see how to apply that to my issue.
This is the copy method:
function CopyFileToFolderUNC($SourcePath, $DestinationPath){
if(-Not (Test-Path $SourcePath))
{
$global:ErrorStrings.Add("Exception: No such path, $SourcePath;; ")
write-output "++ Error: An error occured during copy operation. No such path, $SourcePath ++"
}
Copy-Item -Path $SourcePath -Destination $DestinationPath -Force -Recurse -errorVariable errors
foreach($error in $errors)
{
if ($error.Exception -ne $null)
{
$global:ErrorStrings.Add("Exception: $($error.Exception);; ")
write-output "++ Error: An error occured during copy operation. Exception: $($error.Exception) ++" #this breakpoint is hit after giving errors on screen and taking a long time/failing to copy files it can't reach
}
write-output "Error: An error occured during copy operation. Exception: $($error.Exception)"
}
}
This is my latest try based on what was suggested by #theo, but it's trying to copy files I hadn't tested attributes for a file I can copy, just the dir above it:
function CopyFileToFolderUNC($SourcePath, $DestinationPath, $exclude){
if(-Not (Test-Path $SourcePath )) #-PathType Container
{
$global:ErrorStrings.Add("Exception: No such path, $SourcePath;; ")
write-output "++ Error: An error occured during copy operation. No such path, $SourcePath ++"
}
#$tempFileName = [System.IO.Path]::GetFileName($SourcePath)
#$tempDestFileNamePath = "$DestinationPath\$tempFileName"
Get-ChildItem -Path $SourcePath -Recurse -Force | ForEach {$_} {
#test if maybe we are dealing with an off-line file here
#or use the enum name 'Offline'
# or use the numeric value: 4096
#$oldAttribs = $null
$attr = $_.Attributes.value__
write-output "++ $_ $attr ++"
if (($_.Attributes -eq [System.IO.FileAttributes]::Offline) -or ($_.Attributes.value__ -eq "4096")) {
$_.Attributes=[System.IO.FileAttributes]::Normal
#$oldAttribs = $_.Attributes
#make it a 'normal' file with only the Archive bit set
#$_.Attributes = [System.IO.FileAttributes]::Archive
#log that the file was an issue and copy the other ones
$global:ErrorStrings.Add("Found offline file in backup dir, $_. Logging info and not copying this file. Offline. Please investigate.;; ")
write-output "++ Error: An error occured during copy operation. No such path or file, Offline $_ ++"
} #if
elseif(($_.Attributes -eq [System.IO.Fileattributes]::Archive) -or ($_.Attributes.value__ -eq "32")) {
$global:ErrorStrings.Add("Found offline file in backup dir, $_. Logging info and not copying this file. Archive. Please investigate.;; ")
write-output "++ Error: An error occured during copy operation. No such path or file, Archive $_ ++"
} #elseif
elseif(($_.Attributes -eq [System.IO.Fileattributes]::SparseFile) -or ($_.Attributes.value__ -eq "512")) {
$global:ErrorStrings.Add("Found offline file in backup dir, $_. Logging info and not copying this file. SparseFile. Please investigate.;; ")
write-output "++ Error: An error occured during copy operation. No such path or file, SparseFile $_ ++"
} #elseif
elseif(($_.Attributes -eq [System.IO.Fileattributes]::ReparsePoint) -or ($_.Attributes.value__ -eq "1024")) {
$global:ErrorStrings.Add("Found offline file in backup dir, $_. Logging info and not copying this file. ReparsePoint. Please investigate.;; ")
write-output "++ Error: An error occured during copy operation. No such path or file, ReparsePoint $_ ++"
} #elseif
else {
#the file is not or no longer off-line, so proceed with the copy
$_ | Copy-Item -Destination $DestinationPath -Force -Recurse -ErrorVariable errors
foreach($error in $errors)
{
if ($error.Exception -ne $null)
{
$global:ErrorStrings.Add("Exception: $($error.Exception);; ")
write-output "++ Error: An error occured during copy operation. Exception: $($error.Exception) ++"
}
write-output "Error: An error occured during copy operation. Exception: $($error.Exception)"
}
} #else
#if ($oldAttribs) {
# $_.Attributes = $oldAttribs
#}
} #Get-ChildItem
For example, I'm testing a dir at \\drive\folder\Forms\C Forms\ and it says it's a good attribute "16", but there's a file in that dir that the copy is trying to copy over to my dest dir and I'm seeing it has this for attributes: file.pdf Archive, SparseFile, ReparsePoint, Offline. But I'm not testing that file, the last thing I tested attributes for was the dir it's in.

To me it looks like the files you describe are Off-line files.
In your function you can test if that is the case using something like this:
function CopyFileToFolderUNC($SourcePath, $DestinationPath){
if(-Not (Test-Path $SourcePath)) {
$global:ErrorStrings.Add("Exception: No such path, $SourcePath;; ")
Write-Output "++ Error: An error occured during copy operation. No such path, $SourcePath ++"
}
# test if maybe we are dealing with an off-line file here
if ((Get-Item -Path $SourcePath).Attributes -band 4096) { # or use the enum name 'Offline'
# or use .Net:
# [System.IO.File]::GetAttributes($SourcePath) -band 4096
Write-Output "Did not copy: File '$SourcePath' is currently off-line. "
}
else {
# the file is not off-line, so proceed with the copy
Copy-Item -Path $SourcePath -Destination $DestinationPath -Force -Recurse -errorVariable errors
foreach($error in $errors) {
if ($error.Exception) {
$global:ErrorStrings.Add("Exception: $($error.Exception);; ")
Write-Output "++ Error: An error occured during copy operation. Exception: $($error.Exception) ++" #this breakpoint is hit after giving errors on screen and taking a long time/failing to copy files it can't reach
}
Write-Output "Error: An error occured during copy operation. Exception: $($error.Exception)"
}
}
}
EDIT
From your comment I understand the function was not meant for a single file, but for all files found in a directory named $SourcePath.
In that case, here's an updated function that should do the trick:
function CopyFileToFolderUNC($SourcePath, $DestinationPath){
if(-Not (Test-Path $SourcePath -PathType Container)) {
$global:ErrorStrings.Add("Exception: No such path '$SourcePath'")
Write-Output "++ Error: An error occured during copy operation. No such path '$SourcePath' ++"
}
Get-ChildItem -Path $SourcePath -File | ForEach-Object {
# test if maybe we are dealing with an off-line file here
# or use the enum name 'Offline'
# or use the numeric value: 4096
if ($_.Attributes -band [System.IO.FileAttributes]::Offline) {
Write-Output "Did not copy: File '$($_.FullName)' is currently off-line."
}
else {
# the file is not off-line, so proceed with the copy
$_ | Copy-Item -Destination $DestinationPath -Force -Recurse -ErrorVariable errors
foreach($error in $errors) {
if ($error.Exception) {
$global:ErrorStrings.Add("Exception: $($error.Exception);; ")
Write-Output "++ Error: An error occured during copy operation. Exception: $($error.Exception) ++"
}
Write-Output "Error: An error occured during copy operation. Exception: $($error.Exception)"
}
}
}
}
If by dealing with the files you mean you still want to copy them, use this instead:
function CopyFileToFolderUNC($SourcePath, $DestinationPath){
if(-Not (Test-Path $SourcePath -PathType Container)) {
$global:ErrorStrings.Add("Exception: No such path '$SourcePath'")
Write-Output "++ Error: An error occured during copy operation. No such path '$SourcePath' ++"
}
Get-ChildItem -Path $SourcePath -File | ForEach-Object {
# test if maybe we are dealing with an off-line file here
# or use the enum name 'Offline'
# or use the numeric value: 4096
$oldAttribs = $null
if ($_.Attributes -band [System.IO.FileAttributes]::Offline) {
$oldAttribs = $_.Attributes
# make it a 'normal' file with only the Archive bit set
$_.Attributes = [System.IO.FileAttributes]::Archive
}
# the file is not or no longer off-line, so proceed with the copy
$_ | Copy-Item -Destination $DestinationPath -Force -Recurse -ErrorVariable errors
foreach($error in $errors) {
if ($error.Exception) {
$global:ErrorStrings.Add("Exception: $($error.Exception);; ")
Write-Output "++ Error: An error occured during copy operation. Exception: $($error.Exception) ++"
}
Write-Output "Error: An error occured during copy operation. Exception: $($error.Exception)"
}
# if you want the attributes in the original file to be restored, use:
if ($oldAttribs) {
$_.Attributes = $oldAttribs
}
}
}
See FileAttributes Enum for all possible attribute values.
Hope that helps

Related

Error on Move-Item moving home area to file server

This is the last section of my script that I use to transfer student leavers areas to an archive file server. The script runs and does everything as expected but still throws an error out saying
Move-Item : Cannot find path '\\domain\students$\E-J$\MH201507' because it does not exist. The script does find this path and moves the home area so I'm not sure why I get this error. Or is there any way to fix this or is it easier to hide the error somehow?
The CSV document contains a list of the sam account name and contains their home area location on the domain so it knows where to copy the path over from.
Any help would be massively appreicaited! Kind Regards
#Declaring the .csv file that contains a list of all leavers home directories.
$HomeDirectoryList = Import-CSV "C:\Scripts\Leavers\HomeDirectoryExport.csv"
$Username = $HomeDirectoryList.samAccountName
$HomeDirectory = $HomeDirectoryList.HomeDirectory
$Archive = "\\myfileserver.ac.uk\D$\21-22 Leavers"
ForEach ($Username in $HomeDirectoryList)
{
Move-Item -Path $HomeDirectory -Destination $Archive
}
Sample data with which the error occurs:
samAccountName HomeDirectory
WB214589 \\domain\students$\A-D$\WB214589
MH201507 \\domain\students$\E-J$\MH201507
You are setting the variable $HomeDirectory outside the loop, so that will contain an array of home directory paths.
Then you use variable $Username to iterate the data from the CSV file, but inside that loop you never use it.
Try:
$HomeDirectoryList = Import-CSV 'C:\Scripts\Leavers\HomeDirectoryExport.csv'
$Archive = '\\myfileserver.ac.uk\D$\21-22 Leavers'
foreach ($student in $HomeDirectoryList) {
Write-Host "Moving HomeDirectory folder for student '$($student.samAccountName)'"
Move-Item -Path $student.HomeDirectory -Destination $Archive -Force
}
If you need to catch errors happening, change the loop to:
foreach ($student in $HomeDirectoryList) {
Write-Host "Moving HomeDirectory folder for student '$($student.samAccountName)'"
try {
Move-Item -Path $student.HomeDirectory -Destination $Archive -Force -ErrorAction Stop
}
catch {
Write-Warning "Error moving homedirectory '$($student.HomeDirectory)':`r`n$($_.Exception.Message)"
}
}

Handle Directory Exists Exception

I am new to PowerShell. I have a piece of code which checks whether a folder "ilalog" exists or not. When I run this script first time, it is checking for the folder "ilalog",if it does not exist,it is creating. When I run the script second time. I receive the error below:
An item with the specified name D:\Temp\ilalog already exists
"FullyQualifiedErrorId :
DirectoryExist,Microsoft.PowerShell.Commands.NewItemCommand".
how to handle this exception
I have tried using try and Catch block
$rgflder="ilalog"
[bool]$checkrg=Test-Path D:\Gdump\$rgfolder -PathType Any
if ( $checkrg -eq $False)
{
try{
New-Item -Path "D:\Gdump" -Name "$rgflder" -ItemType "directory"
}
catch [System.IO.IOException]
{
if ($_.CategoryInfo.Category -eq $resExistErr) {Write-host "Dir Exist"}
}
}
else
{
Write-Output "Directory Exists"
}
If you want to continue processing your script while taking action based on an error type, a simple way is to just examine the $error variable. Using Trap may be an option as well.
$error.clear()
New-Item -Path "D:\Gdump" -Name "$rgflder" -ItemType "directory"
if ($error[0].Exception.GetType().Fullname -eq 'System.IO.IOException') {
"Dir Exists"
}
else {
"Dir was created"
}
If you want to use try-catch, you need to treat your non-terminating error as a terminating error to activate the catch block. You can do this with -ErrorAction Stop.
try {
New-Item -Path "D:\Gdump" -Name "$rgflder" -ItemType "directory" -ErrorAction Stop
}
catch [System.IO.IOException]
{
"Exception caught!"
}
Alternatively, you can manage this within your session by setting $ErrorActionPreference = 'Stop', which will apply to all commands within that session.
Keep in mind that the value passed to -ErrorAction overrides the setting in $ErrorActionPreference. Also, error action settings have no affect on terminating errors. So you cannot expect to set -ErrorAction Continue and have code continue processing on a terminating error.
Many commands return objects that may or may not be terminating errors. You will find more success explicitly, specifying when you want to throw a terminating error.
You can read more about error action preference at About Common Parameters. I actually like Everything About Exceptions for exception handling in PowerShell.
Why not simply do like below?
$rgflder="ilalog"
# combine the path and foldername
$rgPath = Join-Path -Path 'D:\Gdump' -ChildPath $rgflder
# test if the folder already exists
if (Test-Path $rgPath -PathType Container) {
Write-Output "Directory Exists"
}
else {
# if not, create the new folder
try {
$null = New-Item -Path $rgPath -ItemType Directory -ErrorAction Stop
Write-Output "Created directory '$rgPath'"
}
catch {
# something terrible happened..
Write-Error "Error creating folder '$rgPath': $($_.Exception.Message)"
}
}

Renaming folders from a list

I have a text file list of 56 folders that includes the full paths to a set of folders. I need to rename the folder at the end of the path.
Example:
Original: \this folder\needs to\move
New: \this folder\needs to\move.moved
I am totally new at powershell and trying to learn. I thought this might be a good way to start. Any help would be greatly appreciated.
# Get the content of the list
# in this case, a text file with no heading, and one path per line
$listContent = Get-Content $ENV:USERPROFILE\Desktop\list.txt
# Loop over each child folder and rename it
foreach($line in $listContent)
{
# check if the current path is valid
$pathTest = Test-Path -Path $line
if($pathTest -eq $True)
{
Write-Output "`nOld path: $($line)"
$newName = $line + ".moved"
Write-Output "New name: $newName"
try
{
# on success, write out message
Rename-Item -Path $line -NewName $newName -Force
# split the string from the file and get the data after the last \ for readability
Write-Output "`nSuccessfully changed directory name $($line.split('\')[-1]) to $newName"
}
catch
{
# on error, throw first error in the Error array
throw $Error[0]
}
}
else {
Write-Output "$($line) is not a valid path"
}
}
Write-Output "`nEnd of script!"

Array handling in powershell

Context :
I have a folder and have some files inside it. I am running a PowerShell script from Jenkins to delete the list of files selected from Jenkins and copy the fresh file from the source. I am trying to delete the files all at an time and copy the list of files like Ctrl+A and copy and paste. I have the script but it is doing individual deletion and copy-paste.
foreach ($database_filename in $database_files) {
Remove-Item -Path $auditFile_Directory -Include $database_filename* -Recurse -Force
Remove-Item -Path $logFile_Directory -Include $database_filename* -Recurse -Force
if ($?) {
log "Deleting the old files complete."
}
try {
log "File sets for $database_filename copying...."
$primary_File = "$database_filename$primaryfile_extn"
$audit_Files = "$database_filename$auditfile_extn"
$log_Files = "$database_filename$logfile_extn"
Copy-Item -Path $sourceFile_Directory$primary_File -Destination $auditFile_Directory
Copy-Item -Path $sourceFile_Directory$audit_Files -Destination $auditFile_Directory
Copy-Item -Path $sourceFile_Directory$log_Files -Destination $logFile_Directory
if ($?) {
log "A fresh golden copy of the db files created."
} else {
Write-Error "Failed! error creating the golden copy, please check the log files"
}
}
catch [System.Net.WebException], [System.IO.IOException]
{
Write-Error "Failed! Unable to copy the SQL files"
}
}
}
You need to split the code into two loops then, the first loop will delete all the files and the second loop will copy all the files.

Perform file verification check with Copy-Item

The following script creates a folder named the specified date on the servers in servers.txt, then copies the folder from which the script is run to that folder.
For example, it creates the folder "3-22-15 (night)" on SERVER1, SERVER2, etc., then copies "Directory Containing the Script" to "3-22-15 (night)".
$CurrentLocation = get-location
$DeploymentDate = "3-22-15 (night)"
Foreach($server in get-content "servers.txt"){
New-Item -ErrorAction SilentlyContinue -ItemType directory -Path \\$server\C$\Deployments\$DeploymentDate
copy-item -Path $CurrentLocation -Destination \\$server\C$\Deployments\$DeploymentDate\ -ErrorAction SilentlyContinue -recurse
}
How do I modify the script to include file verification for each file that is copied to \\$server\C$\Deployments\$DeploymentDate\?
I would like it to output an error message with the file that does not pass the verification check, but to continue copying.
I was going to try something like this:
function SafeCopy ($SourcePath,$DestinationPath,$SourceFileName) {
#MD5 Check Function
function Check-MD5 ($FilePath) {
$md5=New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$hash=[System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($FilePath)))
Return $hash
} # EndOf function Check-MD5
$MD5Source=Check-MD5 $SourcePath
Copy-Item $SourcePath\$SourceFileName $DestinationPath
$MD5Destination=Check-MD5 $DestinationPath
if (Test-Path $DestinationPath) {
if ($MD5Destination -match $MD5Source) {
Write-Host "`nThe file `"$SourcePath`" has been copied in `"$DestinationPath`" successfully.`n" -ForegroundColor DarkGreen
} else {
Write-Host "`nThe file `"$SourcePath`" has been copied in `"$DestinationPath`" but the CRC check failed!`n" -ForegroundColor DarkRed
}
} else {
Write-Host "`nThe file `"$SourcePath`" has not been copied in `"$DestinationPath`"!`n" -ForegroundColor DarkRed
}
} # EndOf function SafeCopy
But I'm not sure how to implement it.
I am not a talented scripter nor do I play one on the radio but I've put this together and it seems to work. As mentioned, the larger the filesize being compared, the slower it goes.
Param
(
[parameter(Mandatory=$true,Position=1,ValueFromPipeLine=$true)][string]$source,
[parameter(Mandatory=$true,Position=2,ValueFromPipeLine=$true)][string]$dest
)
$countsource = #(Get-ChildItem -Path $source -Directory)
$countdest = #(Get-ChildItem -Path $dest -Directory)
IF($countsource = $countdest){
"$source equals $dest"
}
Else{
"$source does not match $dest"
}
Sample usage and output. TODO: Add folder item count compare, warn or break if the folders do not have the same number of items (subfolders and files).
So copy the folder then run Folder-Compare as a function once the copy is finished.
PS C:\Scripts> .\Folder-Compare.ps1 -source C:\Scripts\temp -dest C:\aaa
C:\Scripts\temp does not match C:\aaa