I am trying to copy about 10 folders, each of which contains a ~ 3KB .txt file to a deleted file with a delay of several seconds. I am using Powershells Copy-Item as follows:
try { Copy-Item -Path $source -Destination $destination -Recurse -ErrorAction Stop } catch { Write-Error $_.Exception.ToString() }
The user on which the script is running has read, write, and execute permissions on the shared file server and on the local source.
At first launch, the destination folder is empty. Everything is working fine. At the second start, files and folders already exist. Therefore, before running the code above, I first run the test using Test-Path , and if there is a delete in the folder using Remove-Item , like this:
try { if(Test-Path -Path $path -ErrorAction Stop) { Remove-Item -Recurse -Path $path -ErrorAction Stop } } catch { Write-Error $_.Exception.ToString() }
No one is editing these files. However, when I run the script a dozen times, from time to time, for some reason I donβt understand, I suddenly get UnauthorizedAccessException errors for some of the folders when copying. Exact error:
System.UnauthorizedAccessException: access denied ---> System.ComponentModel.Win32Exception: access is denied in Microsoft.PowerShell.Commands.FileSystemProvider.NativeDirectoryExists (String path) in System.Management.Automation.SessionStateInternal.IsItemContainer (pathmdletter providerCmdletter CmdletProviderContext
note: I get these errors AFTER deleting old files on the remote file server.
masi
source share