问题描述
我是新的PowerShell脚本,我不知道为什么我的脚本复制所有文件,似乎没有检查日期,然后复制所有的文件
无论如何。我也试图在几分钟内做,但我不太确定
如何做到这一点。任何帮助都会很棒!
请看我下面的脚本。
$ RemotePath =\\eb-pc\E $ \testlocation\ * .txt
$ LocalPath =C:\testlocation
$ Max_days =-1
#Max_mins =-5
$ Curr_date = get-date
#检查日期,然后将文件从RemotePath复制到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
}
}
如果要使用小时和分钟,而不是AddDays,只需使用.AddMinutes(),.AddHours()或.AddSeconds()方法。
对于什么值得,我做了一个小的修改,向脚本中添加了一个Else {Scriptblock}来回显未被复制的文件。编写代码将仅复制过去24小时内写入的文件。
$ RemotePath =t:\
$ LocalPath =C:\temp
$ Max_days =-1
#Max_mins =-5
$ Curr_date = get-date
#检查日期,然后将文件从RemotePath复制到LocalPath
Foreach($ file in(Get-ChildItem $ RemotePath))
{
if($ file.LastWriteTime -gt($ Curr_date).adddays($ Max_days))
{
复制项-PATH $ file.fullname -Destination $ LocalPath -WhatIf
#Move-Item -Path $ file.fullname -Destination $ LocalPath
}
ELSE
{不复制$文件
}
}
>如果:执行操作复制文件在目标项:T :\file.htm目的地:C:\temp\file.ht
m。
不复制ListOfSacredVMs.txt
不复制newUser_01.png
不复制newUser_015.png
不复制newUser_02.png
不复制newUser_03.png
什么如果:执行操作复制文件目标项:T:\values.csv目标:C:\temp\values.csv。
I am new to powershell scripting and i cant figure out why my script copies all files and doesn't seem to check the date and then copies all the files anyway. I was trying to do by days and minutes too, but I am not quite sure on how to do that. any help would be great!
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
}
}
If you want to use Hours and minutes, instead of AddDays, just use the .AddMinutes(), .AddHours(), or .AddSeconds() methods instead.
For what it's worth, I made a small modifcation, adding an Else{Scriptblock} to the script to echo out the files which aren't being copied. As written your code will only copy files written in the last 24 hours.
$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脚本根据日期修改来复制文件,以从远程位置检查最新的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!