Copy Function with attributes

tonelm54
tonelm54 used Ask the Experts™
on
I've been trying to write a simple function to copy a file and the set the attributes as needed:-
function tCopy($filSource, $filDestination) {
    Copy-Item $filSource, $filDestination

    $tmpSource = Get-Item $filSource
    $tmpDestination = Get-Item $filDestination

    $tmpDestination.CreationTime = $tmpSource.CreationTime
    $tmpDestination.LastWriteTime = $tmpSource.LastWriteTime
    $tmpDestination.LastAccessTime = $tmpSource.LastAccessTime
}

Open in new window


Which could be used by:-
tCopy C:\New\Application.txt, C:\New\cpApplication.txt

Open in new window


But I just get:-

Copy-Item : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Path'. Specified method is not
supported.
At line:2 char:15
+     Copy-Item $filSource, $filDestination
+               ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Copy-Item], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.CopyItemCommand
 
Get-Item : Cannot find path 'C:\New\cpApplication.txt' because it does not exist.
At line:4 char:18
+     $tmpSource = Get-Item $filSource
+                  ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\New\cpApplication.txt:String) [Get-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand
 
Get-Item : Cannot bind argument to parameter 'Path' because it is null.
At line:5 char:32
+     $tmpDestination = Get-Item $filDestination
+                                ~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Get-Item], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetItemCommand
 
The property 'CreationTime' cannot be found on this object. Verify that the property exists and can be set.
At line:7 char:5
+     $tmpDestination.CreationTime = $tmpSource.CreationTime
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound
 
The property 'LastWriteTime' cannot be found on this object. Verify that the property exists and can be set.
At line:8 char:5
+     $tmpDestination.LastWriteTime = $tmpSource.LastWriteTime
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound
 
The property 'LastAccessTime' cannot be found on this object. Verify that the property exists and can be set.
At line:9 char:5
+     $tmpDestination.LastAccessTime = $tmpSource.LastAccessTime
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

Any idea what Im doing wrong?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
You're calling it wrong.
You separated the arguments with a comma, so as far as PS is concerned, you're passing an array to the first positional argument.
tCopy C:\New\Application.txt C:\New\cpApplication.txt

Open in new window

But it's best to get used to using named arguments:
tCopy -filSource C:\New\Application.txt -filDestination C:\New\cpApplication.txt

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial