Datetime Powershell脚本,用于根据修改的日期复制文件,以从远程位置检查最新文件

Datetime Powershell脚本,用于根据修改的日期复制文件,以从远程位置检查最新文件,datetime,powershell,copy,Datetime,Powershell,Copy,我不熟悉powershell脚本,我不明白为什么我的脚本会复制 所有文件,似乎没有检查日期,然后复制所有文件 无论如何我也试着按天和分钟来做,但我不太确定 关于如何做到这一点。任何帮助都会很好 see my script below. $RemotePath = "\\eb-pc\E$\testlocation\*.txt" $LocalPath = "C:\testlocation" $Max_days = "-1" #Max_mins = "-5" $Curr_date = get-dat

我不熟悉powershell脚本,我不明白为什么我的脚本会复制 所有文件,似乎没有检查日期,然后复制所有文件 无论如何我也试着按天和分钟来做,但我不太确定 关于如何做到这一点。任何帮助都会很好

see my script below.

$RemotePath = "\\eb-pc\E$\testlocation\*.txt"
$LocalPath = "C:\testlocation"
$Max_days = "-1"
#Max_mins = "-5"
$Curr_date = get-date

#Checking date and then copying file from RemotePath to LocalPath
Foreach($file in (Get-ChildItem $RemotePath))
{
    if($file.LastWriteTime -gt ($Curr_date).adddays($Max_days))
    {
        Copy-Item -Path $file.fullname -Destination $LocalPath
        #Move-Item -Path $file.fullname -Destination $LocalPath
    }

}

我已经有一段时间没有在PowerShell做任何事情了。但是,您可能希望尝试回显正在比较的值,以查看它们真正返回的值

Echo“LastWrite:&$file.LastWriteTime

Echo“复制时间:&($Curr_date)。添加天数($Max_days)

这将有助于确定要比较的内容。
希望至少有一点限制。

如果要使用小时和分钟而不是AddDays,只需使用.AddMinutes()、.AddHours()或.AddSeconds()方法即可

值得一提的是,我做了一个小小的修改,在脚本中添加了一个Else{Scriptblock},以回显未被复制的文件。编写时,您的代码将只复制在过去24小时内编写的文件

$RemotePath = "t:\"
$LocalPath = "C:\temp"
$Max_days = "-1"
#Max_mins = "-5"
$Curr_date = get-date

#Checking date and then copying file from RemotePath to LocalPath
Foreach($file in (Get-ChildItem $RemotePath))
{
    if($file.LastWriteTime -gt ($Curr_date).adddays($Max_days))
    {

        Copy-Item -Path $file.fullname -Destination $LocalPath -WhatIf
        #Move-Item -Path $file.fullname -Destination $LocalPath
    }
    ELSE
    {"not copying $file"
    }

}

>What if: Performing the operation "Copy File" on target "Item: T:\file.htm Destination: C:\temp\file.ht
m".
not copying ListOfSacredVMs.txt
not copying newUser_01.png
not copying newUser_015.png
not copying newUser_02.png
not copying newUser_03.png
What if: Performing the operation "Copy File" on target "Item: T:\values.csv Destination: C:\temp\values.csv".

非常感谢,这很有帮助!