robocopy is giving error with compiled script

Ask your Windows 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.
jvierra
Posts: 14335
Meble kuchenne na zamówienie - na wymiar - Wrocław
Joined: Tue May 22, 2007 9:57 am
Contact:

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
Joined: Mon Feb 13, 2017 1:09 pm

Re: robocopy is giving error with compiled script

Post by ashishk »

Fortunately both the location or directory are available.

User avatar
ashishk
Posts: 19
Joined: Mon Feb 13, 2017 1:09 pm

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: 14335
Joined: Tue May 22, 2007 9:57 am
Contact:

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
Joined: Mon Feb 13, 2017 1:09 pm

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: 14335
Joined: Tue May 22, 2007 9:57 am
Contact:

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
Joined: Mon Feb 13, 2017 1:09 pm

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: 14335
Joined: Tue May 22, 2007 9:57 am
Contact:

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.  
  3. $logLocal = "$env:USERPROFILE\AppData\Local\MyLogs"
  4.  
  5. $centralizedLocation = 'c:\appLogs'
  6.  
  7. robocopy $logLocal $centralizedLocation $filename
  8.  
  9. Write-Host done
  10.  
  11. 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.

Locked