比较mkv的创建时间

时间:2017-10-12 17:53:34

标签: windows powershell windows-server windows-server-2016

我的任务是创建一个脚本,检查我们设置的办公相机是否已停止将其Feed上传到相机"共享位于我们的Windows 2016存储服务器上。如果NEWEST .mkv与当前时间(获取日期)相比超过一个小时,那么"问题"相机需要手动重启。 (无需编写该部分的脚本。)

这是我的导演到目前为止所写的内容:

#Variable Definitions start here

$numhours = 1

Get-ChildItem "d:\Shares\Cameras" | Foreach {    

$folderToLookAt =  ($_.FullName + "\*.mkv")
$result = Get-ChildItem -Recurse $folderToLookAt | Sort-Object CreationTime -Descending


echo $result[0].FullName
echo $result[0].CreationTime

}

第一个变量确实还没有被使用,但是我有点愚蠢到接下来要做什么。以上内容将成功返回最新的.mkvs

的全名和创建时间

关于下一部分的建议?

5 个答案:

答案 0 :(得分:1)

反转逻辑 - 而不是搜索所有文件,排序,查找最新文件,并检查日期,反之亦然。

查找自截止以来创建的文件,如果找不到,则发出警报:

$cutOffTime = [datetime]::Now.AddHours(-1)

Get-ChildItem "d:\Shares\Cameras" | Foreach {    

    $folderToLookAt =  ($_.FullName + "\*.mkv")
    $result = Get-ChildItem -Recurse $folderToLookAt | Where-Object { $_.CreationTime -gt $cuttoffTime }

    if (-not $result)
    {
        "$($_.Name) has no files since the cutoff time"
    }
}

答案 1 :(得分:0)

您必须将CreationTime日期与(Get-Date).AddHours(-1)进行比较。 AddHours方法允许您向DateTime添加小时数,但也可以减去。{/ p>

您可以使用以下示例:

$Path = 'd:\Shares\Cameras'
$CreationTime = Get-ChildItem -Path $Path -Filter *.mkv | 
    Sort-Object -Property CreationTime -Descending | 
    Select-Object -First 1 -ExpandProperty CreationTime
if ($CreationTime -lt (Get-Date).AddHours(-1)) {
    # your action here (restart, send mail, write output, ...)
}

它还可以优化您的代码。 ;)

答案 2 :(得分:0)

var Xray = require('x-ray');
var x = Xray();

x('http:// abc  .com', {
  title: '.acalog-core>h4@text',
 courseid : '.acalog-core li',

 and so on...
})(function(err, obj) {
    // do some lite filtering in obj to strip of the HTML
    // write To json.. obj contains all the scrapped content as key ,value pair
})

答案 3 :(得分:0)

试试这个:

Get-ChildItem "c:\temp" -Filter *.mkv -File | sort CreationTime -Descending | 
    select -First 1 | where CreationTime -lt (Get-Date).AddHours(-1) |
        %{Write-Host "Alert !!" -ForegroundColor Red}

答案 4 :(得分:0)

我假设您的路径如下:

D:\Shares\Cameras\Camera1\file1.mkv
D:\Shares\Cameras\Camera1\file2.mkv
D:\Shares\Cameras\Camera2\file1.mkv
D:\Shares\Cameras\Camera2\file2.mkv
D:\Shares\Cameras\Camera3\file1.mkv
.
.
.

如果是这样,我会做这样的事情:

# The path to your files
$CameraShareRoot = 'D:\Shares\Cameras';

# Number of Hours
$NumberOfHours = 1;

# Date and time of significance.  It's $NumberOfHours in the past.
$MinFileAge = (Get-Date).AddHours( - $NumberOfHours);

# Get all the folders at the camera share root
Get-ChildItem -Path $CameraShareRoot -Directory | ForEach-Object {
    # Get the most recently created file in each folder
    $_ | Get-ChildItem -Recurse -Filter '*.mkv' -File | Sort-Object -Property CreationTime -Descending | Select-Object -First 1
} | Where-Object {
    # Remove any files that were created after our datetime
    $_.CreationTime -lt $MinFileAge;
} | Select-Object -Property FullName, CreationTime

这将只输出陈旧相机的完整文件名和创建时间。

当结果包含任何文件时,您可以执行以下操作以向自己发送电子邮件:

# The path to your files
$CameraShareRoot = 'D:\Shares\Cameras';

# Number of Hours
$NumberOfHours = 1;

# Date and time of significance.  It's $NumberOfHours in the past.
$MinFileAge = (Get-Date).AddHours( - $NumberOfHours);

# Get all the folders at the camera share root, save the results to $StaleCameraFiles
$StaleCameraFiles = Get-ChildItem -Path $CameraShareRoot -Directory | ForEach-Object {
    # Get the most recently created file in each folder
    $_ | Get-ChildItem -Recurse -Filter '*.mkv' -File | Sort-Object -Property CreationTime -Descending | Select-Object -First 1;
} | Where-Object {
    # Remove any files that were created after our datetime
    $_.CreationTime -lt $MinFileAge;
}

# If there are any stale camera files
if ($StaleCameraFiles) {
    # Send an email
    $MailMessage = @{
        SmtpServer = 'mail.example.com';
        To = 'youremail@example.com';
        From = 'youremail@example.com';
        Subject = 'Stale Camera Files';
        Body = $StaleCameraFiles | Select-Object -Property FullName, CreationTime | ConvertTo-Html -Fragment | Out-String;
        BodyAsHtml = $true;
    }
    Send-MailMessage @MailMessage;
}

通常,您会希望使用LastWriteTime代替CreationTime,因为后者可以通过文件移动或复制进行更新,但也许这就是您想要的内容。