Question

Have a strange issue crop up last week and can't find a solution. I have a modified version of this script: http://technet.microsoft.com/en-us/magazine/2009.07.heyscriptingguy.aspx running my log backups across 500 servers. Last week, 6 servers in the EMEA region decided that they didn't want to allow the files to be copied over.

The script processes one script a time and is failing with the copy-item cmdlet. The file does exist on the remote server. Take a look at the output below:

PS C:\> BackUpAndClearEventLogsDebug.ps1 -computers bud1s001 -LogsArchive "\\srv1s001\d$\Log_Backups\Test"
+ Processing bud1s001
This is the Backupeventlogs function and we're about to copy \\bud1s001\c$\Windows\temp\bud1s001\Application.evt
This is the Copyeventlogs function and we're about to copy \\bud1s001\c$\Windows\temp\bud1s001\Application.evt
Copy-Item : Cannot find path '\\bud1s001\c$\Windows\temp\bud1s001\Application.evt' because it does not exist.
At C:\Sched\LOG_BACKUPS\BackUpAndClearEventLogsDebug.ps1:132 char:2
+     Copy-Item -path $path -dest "$LogsArchive\$folder"
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : ObjectNotFound: (\\bud1s001\c$\W...Application.evt:String)     [Copy-Item], ItemNotFoundException
   + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand

PS C:\> ls \\bud1s001\c$\Windows\temp\bud1s001\Application.evt
Directory: \\bud1s001\c$\Windows\temp\bud1s001
Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         2/17/2014  10:17 AM    5242836 Application.evt

If I set the variables in an interactive shell I can run the copy-item cmd with no problems. If I use the same arguments but not variables I can copy the files. It is only when run within the script that the failure happens.

I've tried running under different accounts, all of which are administrators. I changed the -path argument to -LiteralPath with no change. I've added output to make sure that the object isn't getting its value changed and everything looks good. The servers that are failing could have different language settings since they are not in the US, but the variable looks good. Only having issues with Windows 2003 servers, not issues with 2008 servers.

I'm out of ideas, open to suggestions.

Here is the exact function from the script with calling argument

Copy-EventLogsToArchive -path $path -Folder $Folder

Function Copy-EventLogsToArchive($path, $folder)
{
Write-Host " "
write-host "This is the Copyeventlogs function and we're about to copy $path"
Copy-Item -path $path -dest "$LogsArchive\$folder"
} # end Copy-EventLogsToArchive

No correct solution

OTHER TIPS

Have you tried using Test-Path to verify the existence of the target file path, before calling Copy-Item?

Function Copy-EventLogsToArchive
{
    [CmdletBinding()]
    param ( [string] $Path, [string] $folder)

    Write-Host -Object "`nThis is the Copyeventlogs function and we're about to copy $path";
    if (Test-Path -Path $Path) {
        Copy-Item -Path $Path -Destination "$LogsArchive\$folder";
    }
} # end Copy-EventLogsToArchive

Copy-EventLogsToArchive -Path $Path -Folder $Folder;

The behavior you are describing is rather odd. How about bypassing the Copy-Item cmdlet, and going straight to the .NET Framework to copy the file?

[System.IO.File]::Copy($Path, "$LogsArchive\$Folder");

Side note: You should generally put your function calls after the function definition in PowerShell. If you make a change to the function definition, but your function call occurs before the definition, then your old function definition will be called when you execute the script.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top