35

我对 PowerShell 很陌生,我希望能获得一些帮助,创建一个告诉我文件修改日期的脚本。

我希望我对 PowerShell 有更多的了解,因为我觉得我问了很多(我这周的所有空闲时间都将致力于更好地学习 PowerShell)。指出我在哪里学习如何做到这一点的方向也会非常有帮助。

这是完整的纲要。我需要每天运行一份报告,检查 90 家不同商店的计算机列表,以确保执行了特定备份。修改日期应告知是否已执行备份,并将设置为前一个日期。

如果修改日期是昨天,则不需要输出。如果不是昨天,我希望在 PowerShell 窗口中输出输出,或者输出到文本文件中,以更容易者为准。

我还必须检查 90 个商店中每个商店的文件夹不超过 7 天,输出标准相同。我对每家商店的想法都是这样的

对于商店 1:

Check file date for \\server\store\computer\c:\folder\"newest modified date in folder"
if date equals yesterday
   then do nothing
if date does not equal yesterday
   then output "Test did not backup"

check folder modified date for \\server\sample\store\backupfolder
if date equals <7 days old
   then do nothign
if date equals >7 days old
   then output "test did not backup"

很抱歉没有证明我的研究成果,我对 Powershell 很陌生,而且我在截止日期前完成这项工作。从昨天开始,我已经学会了如何使用这个脚本来做我需要做的所有事情。感谢@Keith 让我走上了正确的道路。我最终使用以下代码来实现我的目标,即只输出结果为假的位置。

$a = Get-ChildItem \\server\XXX\Received_Orders\*.* | Where{$_.LastWriteTime -ge (Get-Date).AddDays(-7)}
if ($a = (Get-ChildItem \\server\XXX\Received_Orders\*.* | Where{$_.LastWriteTime -ge (Get-Date).AddDays(-7)}))
{
}
Else
{
    'STORE XXX HAS NOT RECEIVED ANY ORDERS IN THE PAST 7 DAYS'
}


$b = Get-ChildItem \\COMP NAME\Folder\*.* | Where{$_.LastWriteTime -ge (Get-Date).AddDays(-1)}
if ($b = (Get-ChildItem \\COMP NAME\TFolder\*.* | Where{$_.LastWriteTime -ge (Get-Date).AddDays(-1)}))
{
}
Else
{
    'STORE XXX DID NOT RUN ITS BACKUP LAST NIGHT'
}
4

5 回答 5

67

如果您运行 Get-Item 或 Get-ChildItem 命令,这些命令将输出包含此信息的System.IO.FileInfoSystem.IO.DirectoryInfo对象,例如:

Get-Item c:\folder | Format-List  

或者您可以像这样直接访问该属性:

Get-Item c:\folder | Foreach {$_.LastWriteTime}

要开始根据上次写入时间过滤文件夹和文件,您可以执行以下操作:

Get-ChildItem c:\folder | Where{$_.LastWriteTime -gt (Get-Date).AddDays(-7)}
于 2013-11-04T18:22:20.753 回答
24

要获取单个文件的修改日期,请尝试:

$lastModifiedDate = (Get-Item "C:\foo.tmp").LastWriteTime

与另一个比较:

$dateA= $lastModifiedDate 
$dateB= (Get-Item "C:\other.tmp").LastWriteTime

if ($dateA -ge $dateB) {
  Write-Host("C:\foo.tmp was modified at the same time or after C:\other.tmp")
} else {
  Write-Host("C:\foo.tmp was modified before C:\other.tmp")
}
于 2017-07-31T22:36:55.267 回答
0

这对我有用:

$a = Get-ChildItem \\server\XXX\Received_Orders\*.* | Where{$_.LastWriteTime -ge (Get-Date).AddDays(-7)}
if ($a = (Get-ChildItem \\server\XXX\Received_Orders\*.* | Where{$_.LastWriteTime -gt (Get-Date).AddDays(-7)}  
#Im using the -gt switch instead of -ge
{}
Else
{
'STORE XXX HAS NOT RECEIVED ANY ORDERS IN THE PAST 7 DAYS'
}


$b = Get-ChildItem \\COMP NAME\Folder\*.* | Where{$_.LastWriteTime -ge (Get-Date).AddDays(-1)}
if ($b = (Get-ChildItem \\COMP NAME\TFolder\*.* | Where{$_.LastWriteTime -gt (Get-Date).AddDays(-1)))}
{}
Else
{
'STORE XXX DID NOT RUN ITS BACKUP LAST NIGHT'
}
于 2016-08-30T17:11:47.733 回答
0

你可以试试dirTimesJS.batfileTimesJS.bat

例子:

C:\>dirTimesJS.bat %windir%

directory timestamps for C:\Windows :

Modified : 2020-11-22 22:12:55
Modified - milliseconds passed : 1604607175000
Modified day of the week : 4

Created : 2019-12-11 11:03:44
Created - milliseconds passed : 1575709424000
Created day of the week : 6

Accessed : 2020-11-16 16:39:22
Accessed - milliseconds passed : 1605019162000
Accessed day of the week : 2

C:\>fileTimesJS.bat %windir%\notepad.exe

file timestamps for C:\Windows\notepad.exe :

Modified : 2020-09-08 08:33:31
Modified - milliseconds passed : 1599629611000
Modified day of the week : 3

Created : 2020-09-08 08:33:31
Created - milliseconds passed : 1599629611000
Created day of the week : 3

Accessed : 2020-11-23 23:59:22
Accessed - milliseconds passed : 1604613562000
Accessed day of the week : 4
于 2020-11-10T15:30:06.813 回答
-1

用于查找过去 2 天修改的所有文档库文件的 PowerShell 代码。

$web = Get-SPWeb -Identity http://siteName:9090/ 
        $list = $web.GetList("http://siteName:9090/Style Library/")
        $folderquery =  New-Object Microsoft.SharePoint.SPQuery  
        $foldercamlQuery =  
        '<Where>   <Eq> 
                <FieldRef Name="ContentType" />  <Value Type="text">Folder</Value> 
            </Eq> </Where>' 
        $folderquery.Query = $foldercamlQuery 
        $folders = $list.GetItems($folderquery) 
        foreach($folderItem in $folders) 
        { 
            $folder = $folderItem.Folder
            if($folder.ItemCount -gt 0){ 
            Write-Host " find Item count " $folder.ItemCount
                $oldest = $null
                $files = $folder.Files

                $date = (Get-Date).AddDays(-2).ToString(“MM/dd/yyyy”)
                foreach ($file in $files){ 
                    if($file.Item["Modified"]-Ge $date)
                    {
                        Write-Host "Last 2 days modified folder name:"   $folder   " File Name: "  $file.Item["Name"]   " Date of midified: "  $file.Item["Modified"] 
                    } 
                } 
            } 
            else
             { 
                Write-Warning "$folder['Name'] is empty" 
            } 
        }
于 2015-02-24T07:10:03.570 回答