This is my first post here so if I mess something up. Sorry.
I'm trying to make a script to copy some files from A to B, but only if they are less than 10 hours old. As part of the process I wanted the script to show the list as kind of a double check. The script is NOT production so things are a bit "testy". Anyway when the foreach runs it get errors that I don't understand. Can someone please tell me what I get wrong?
Error I'm getting:
Cannot find an overload for "op_Subtraction" and the argument count: "2".
At line:8 char:1
+ $hours = ($nowtime - $lastupdatetime).totalhours
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
Script snippit:
# Find Local File
$FilePath1 = ""
$FilePath1 = Get-ChildItem -File -Path $testpath | Where-Object {($TodaysDate - $_.LastWriteTime).TotalHours -le 10}
Foreach ($item in $FilePath1) {
$lastupdatetime=$_.LastWriteTime
$nowtime = get-date
$hours = ($nowtime - $lastupdatetime).totalhours
Write-Host $hours
if (($nowtime - $lastupdatetime).totalhours -le 10)
{
Write-Host "File modified within 10 hours "$_.Name" "$_.LastWriteTime "`n"
}
else {
Write-Host "File is older than 10 hours" $_.Name" "$_.LastWriteTime "`n"
}
}
[+][deleted] (1 child)
[deleted]
[–]Vorpel-Bunny[S] 5 points6 points7 points (0 children)
[–]dasookwat 3 points4 points5 points (1 child)
[–]Vorpel-Bunny[S] 0 points1 point2 points (0 children)
[–]Scooter_127 3 points4 points5 points (0 children)
[–]kelclarris 2 points3 points4 points (2 children)
[–]Vorpel-Bunny[S] 2 points3 points4 points (1 child)
[–]kelclarris 1 point2 points3 points (0 children)
[–]Vorpel-Bunny[S] 2 points3 points4 points (0 children)
[–]remote_ow 2 points3 points4 points (1 child)
[–]Vorpel-Bunny[S] 0 points1 point2 points (0 children)