2015-04-02 52 views
3

我是新來的PowerShell腳本,我不知道爲什麼我的腳本複製 所有文件,似乎並沒有檢查日期,然後複製無論如何,所有文件 。我正在嘗試做幾天和幾分鐘,但我不太確定 如何做到這一點。任何幫助將是偉大的!Powershell腳本複製文件基於日期modifed從遠程位置檢查最新的文件

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 
    } 

} 

回答

2

如果您想使用小時數和分鐘數而不是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". 
+0

非常感謝您的幫助! – 2015-04-03 12:50:35

0

自從我在PowerShell中做了任何事情以來已經有一段時間了。但是,您可能想嘗試將您正在比較的值回顯出來,以查看它們真正返回的內容。

回聲 「LastWrite:」 & $ file.LastWriteTime

回聲 「複製後:」 &($ Curr_date).adddays($ MAX_DAYS)

這將有助於確定你正在試圖比較什麼。 HopeThis至少有一點。

相關問題