Powershell脚本根据修改日期复制文件以检查来自远程位置的最新文件
Powershell Script to copy files based on date modifed to check newest file from a remote location
我是 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 "Copy After : " & ($Curr_date).adddays($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".
我是 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 "Copy After : " & ($Curr_date).adddays($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".