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.