robocopy is giving error with compiled script

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 6 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: robocopy is giving error with compiled script

Post by jvierra »

You are trying to copy to or from a location that is not available. "CategoryInfo : ResourceUnavailable: "
User avatar
ashishk
Posts: 19
Last visit: Mon Apr 10, 2017 12:08 am

Re: robocopy is giving error with compiled script

Post by ashishk »

Fortunately both the location or directory are available.
User avatar
ashishk
Posts: 19
Last visit: Mon Apr 10, 2017 12:08 am

Re: robocopy is giving error with compiled script

Post by ashishk »

Hello

Has anyone found the solution?

I am still stuck on same issue, I tested this in PS 2.0/4.0/5.0, everywhere it is same issue.

Any other approach to resolve this issue?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: robocopy is giving error with compiled script

Post by jvierra »

Something in your variables is wrong. We cannot see your system. You will need to carefully inspect your variables and test them independently.
User avatar
ashishk
Posts: 19
Last visit: Mon Apr 10, 2017 12:08 am

Re: robocopy is giving error with compiled script

Post by ashishk »

Hi jvierra

Following code suggested by and it seems to be perfect, there is nothing wrong in the variable but I am still getting error when I run it as compiled exe, however file is getting copy.

Secondly I direct run as script instead of compiled exe, I don't get any error.

$logLocal = "c:\users\$Env:USERNAME\AppData\Local\MyLogs"
$centralizedLocation = 'c:\appLogs'
$filename = "$Env:USERNAME_AdminActivity_$logDate.txt"
# check target file
$targetFilePath = join-Path $logLocal $filename
if (Test-Path $targetFilePath) {
robocopy $logLocal $centralizedLocation $filename
} else {
Write-Host 'File not found'
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: robocopy is giving error with compiled script

Post by jvierra »

You code makes no sense. It is testing if a file exists in the target locations before it is copied.

I suspect you are doing something that is not seeable in what you have posted.
User avatar
ashishk
Posts: 19
Last visit: Mon Apr 10, 2017 12:08 am

Re: robocopy is giving error with compiled script

Post by ashishk »

Hi

My original code which I posted in my first post was, I was getting same error here as well

$logLocal = "c:\users\$Env:USERNAME\AppData\Local\MyLogs\"
$centralizedLocation = "c:\appLogs\"
$copyLog=$logLocal +$Env:USERNAME+"_"+ "AdminActivity" +"_"+$logDate +".txt"
$filename = Split-Path $copyLog -leaf

robocopy $logLocal $centralizedLocation $filename

Then Davinl suggested code in a different manner, which I posted in my last email.

Anyway I posted my initial code for your reference, if you have some idea pls let me know.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: robocopy is giving error with compiled script

Post by jvierra »

I can run that code with no issues.

This:

  1. $filename = 'test.txt'
  2. $logLocal = "$env:USERPROFILE\AppData\Local\MyLogs"
  3. $centralizedLocation = 'c:\appLogs'
  4. robocopy $logLocal $centralizedLocation $filename
  5. Write-Host done
  6. Read-Host 'Enter to exit'
As an EXE has no issues. RoboCopy is silly for copying one file but it works with no issues. You have some other problem that is not shown in what you have posted.

Write you code the way I have above and just insert the correct file name and test.
This topic is 6 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked