我正在编写一个脚本来测试同步文件夹中的旧文件,如果文件超过2小时,则发送文本消息(这意味着文件无法更新)。我没有包括“发短信”部分,因为它工作正常。
当我运行我的脚本时,我得到三个日期:许多文件的时间。为什么我为“最后”写入时间获得三个值?而且,我如何仅查看最近的“最后”写入时间?
这是我的剧本......
$Store = “ALI”, “COR”, “ELD”, “GRY”, “HML”, “JB”, “SWN”, “LBT”, “LGC”, “MAU”, “MID”, “MPW”, “RKE”, “SPC”, “SSP”, “TWL”, “WRD”
foreach ($element in $Store)
{
$FPath = "E:\ServerFolders\SData\$element\$element-Cmp"
$FPath = $FPath.Replace("-", "_")
if(Get-Childitem $FPath | Where-Object {$_.LastWriteTime -lt (get-date).addHours(-2) })
{
$FName = Get-ChildItem $FPath -name -Filter "*.7Z"
$FTime = (Get-ChildItem $FPath).LastWriteTime
$FName
$FTime
}}
这是我的输出......
TX029.7Z
Wednesday, September 13, 2017 11:44:54 AM
Monday, November 13, 2017 8:01:34 PM
Monday, November 13, 2017 7:45:45 PM
TX059.7Z
Tuesday, August 22, 2017 7:56:18 PM
Monday, November 13, 2017 8:03:56 PM
Monday, November 13, 2017 8:41:05 PM
TX013.7Z
Monday, November 13, 2017 8:01:33 PM
Monday, November 13, 2017 7:33:20 PM
Friday, November 10, 2017 2:38:20 PM
TX050.7z
Monday, November 13, 2017 8:02:17 PM
Monday, November 13, 2017 9:46:19 AM
Tuesday, October 31, 2017 7:46:01 AM
非常感谢帮助!!
我运行了Stephen建议的代码。对于我的许多文件,我仍然为LastWriteTime获取多个值。
这是17/24/17上午8:57的结果输出,当时所有文件在大约上午8:46左右显示时间戳。
PS C:\Windows\system32> $Store = “ALI”, “COR”, “ELD”, “GRY”, “HML”, “JB”, “SWN”, “LBT”, “LGC”, “MAU”, “MID”, “MPW”, “RKE”, “SPC”, “SSP”, “TWL”, “WRD”
foreach ($element in $Store)
{
$FPath = "E:\ServerFolders\SData\$element\$element-Cmp"
$FPath = $FPath.Replace("-", "_")
#this if statement does not return a true / false value
#PowerShell is using whether an object is returned by Get-ChildItem
# or not to try and determine a boolean value
if(Get-Childitem $FPath | Where-Object {$_.LastWriteTime -lt (get-date).addHours(-2) })
{
#at this point all we know is that some file exists with a write time
#of more than 2 hours but it could be any file
#You then get the name but only of all *.7z files in the path
#but there's no guarantee that this is the file you need
$FName = Get-ChildItem $FPath -name -Filter "*.7Z"
#This line gets the lastwritetime for all files in the folder
#regardless of the lastwritetime of the file
$FTime = (Get-ChildItem $FPath).LastWriteTime
$FName
$FTime
}}
TX025.7Z
Thursday, November 23, 2017 11:04:20 AM
Friday, November 24, 2017 8:46:54 AM
TX029.7Z
Wednesday, September 13, 2017 11:44:54 AM
Thursday, November 23, 2017 11:01:26 AM
Friday, November 24, 2017 8:45:45 AM
TX049.7Z
Thursday, November 23, 2017 11:03:18 AM
Wednesday, November 22, 2017 5:46:02 PM
GA007.7Z
Thursday, November 23, 2017 7:45:38 PM
Thursday, November 23, 2017 11:02:20 AM
GA004.7Z
Friday, November 24, 2017 8:47:01 AM
Thursday, November 23, 2017 11:30:53 AM
GA003.7Z
Friday, November 24, 2017 8:47:10 AM
Thursday, November 23, 2017 10:30:48 AM
MO001.7Z
Thursday, November 23, 2017 11:04:45 AM
Thursday, November 23, 2017 7:47:08 PM
TX059.7Z
Tuesday, August 22, 2017 7:56:18 PM
Thursday, November 23, 2017 11:03:48 AM
Friday, November 24, 2017 8:41:02 AM
AR001.7Z
Thursday, November 23, 2017 7:46:02 PM
Thursday, November 23, 2017 11:05:08 AM
TX013.7Z
Thursday, November 23, 2017 11:01:25 AM
Friday, November 24, 2017 8:34:11 AM
TX050.7z
Thursday, November 23, 2017 11:02:03 AM
Friday, November 24, 2017 8:46:19 AM
TX024.7Z
Thursday, November 23, 2017 11:03:48 AM
Thursday, November 23, 2017 8:40:36 PM
GA010.7Z
Friday, November 24, 2017 8:45:24 AM
Thursday, November 23, 2017 11:02:39 AM
TX002.7Z
Thursday, November 23, 2017 11:02:03 AM
Friday, November 24, 2017 8:43:45 AM
TX035.7Z
Thursday, November 23, 2017 11:03:17 AM
Friday, November 24, 2017 8:51:39 AM
PS C:\Windows\system32>
答案 0 :(得分:1)
您的过程过于复杂。您只需添加一个选择对象即可获取所需的2个属性。
$Store = “ALI”, “COR”, “ELD”, “GRY”, “HML”, “JB”, “SWN”, “LBT”, “LGC”, “MAU”, “MID”, “MPW”, “RKE”, “SPC”, “SSP”, “TWL”, “WRD”
foreach ($element in $Store)
{
$FPath = "E:\ServerFolders\SData\$element\$element-Cmp"
$FPath = $FPath.Replace("-", "_")
#Filter for *.7z, with a lastwritetime older than 2 hours
#Grab only the name and lastwritetime
Get-Childitem $FPath -Filter "*.7z" |
Where-Object {$_.LastWriteTime -lt (get-date).addHours(-2) } |
Select-Object Name, LastWriteTime
}
编辑:解释原始过程的逻辑
$Store = “ALI”, “COR”, “ELD”, “GRY”, “HML”, “JB”, “SWN”, “LBT”, “LGC”, “MAU”, “MID”, “MPW”, “RKE”, “SPC”, “SSP”, “TWL”, “WRD”
foreach ($element in $Store)
{
$FPath = "E:\ServerFolders\SData\$element\$element-Cmp"
$FPath = $FPath.Replace("-", "_")
#this if statement does not return a true / false value
#PowerShell is using whether an object is returned by Get-ChildItem
# or not to try and determine a boolean value
if(Get-Childitem $FPath | Where-Object {$_.LastWriteTime -lt (get-date).addHours(-2) })
{
#at this point all we know is that some file exists with a write time
#of more than 2 hours but it could be any file
#You then get the name but only of all *.7z files in the path
#but there's no guarantee that this is the file you need
$FName = Get-ChildItem $FPath -name -Filter "*.7Z"
#This line gets the lastwritetime for all files in the folder
#regardless of the lastwritetime of the file
$FTime = (Get-ChildItem $FPath).LastWriteTime
$FName
$FTime
}}
如果没有看到原始文件结构,很难确切地说明你正在使用什么,但上面应该解释为什么你在脚本中获得了多个lastwritetime值。