0

I use The following powershell code to copy file from a remote machine on the same network to the actual machine where this powershell is executed. The firewall is disabled. The $UpdateSourceFolder is a shared folder where all users have access. The problem is that copy-item never returns me exception but the file are not copied from one location to another.

Try {
    Copy-item "$UpdateSourceFolder\*" "$UpdateDestinationFolder" -Force -Recurse -ErrorAction Stop
    Add-Content $LogFilePath "SUCCESSFULLY COPIED FILE"
}
catch {
    $errorMessage = $_.Exception.Message 
    Add-Content $LogFilePath $errorMessage
}

Is there something I can do to force Powershell to give me the exception?

6
  • 4
    The absence of errors suggests that wildcard pattern "$UpdateSourceFolder\*" doesn't match anything, which simply makes the Copy-Item call a quiet no-op. See if Get-ChildItem -LiteralPath $UpdateSourceFolder -Force | Copy-Item -Destination $UpdateDestinationFolder -Force -Recurse -ErrorAction Stop results in errors. Commented Mar 25 at 14:56
  • 2
    Try using the Copy-Item -Verbose ... switch. Commented Mar 26 at 6:59
  • 1
    Thank you for your suggestion. Unfortunately none of that worked. I add also that I discovered that It depens on the user I use to run this powershell. If I run it using an administrator user it works. Otherwise, If I use service account like System or Network Service the file are not copied and no exception is logged Commented Mar 26 at 7:25
  • 1
    One potential explanation is that these special accounts are not permitted to access the target file share and that they are perhaps also not permitted to write to the log file, meaning that the attempt to record the exceptions that do occur fails. Commented Mar 26 at 12:05
  • 1
    @aSpagno The responses to this Reddit thread will likely help you along, based on your comment: reddit.com/r/PowerShell/comments/1gakkts/… Commented Mar 28 at 18:43

1 Answer 1

0

For some reason, no errors are generated when using wild cards in the path of Copy-Item (as @mklement0 states in the comments). Using the Filter param instead should bypass this behavior.

Copy-Item "$UpdateSourceFolder\" -Filter * "$UpdateDestinationFolder" `
  -Recurse -ErrorAction Stop

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.