troubleshooting Question

Copy Function with attributes

Avatar of tonelm54
tonelm54 asked on
Powershell
1 Comment1 Solution191 ViewsLast Modified:
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
}

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

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?
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 1 Comment.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros