繁体   English   中英

PowerShell脚本文件修改时间> 10h,如果未找到任何内容,则返回一个值

[英]PowerShell script file modify time>10h and return a value if nothing is found

我正在尝试编写一个脚本/一个衬纸,它将在一个特定的文件夹中查找10个小时前已修改过的文件,如果没有文件,我需要它来打印一些值或字符串。

Get-ChildItem -Path C:\blaa\*.* | where {$_.Lastwritetime -lt (date).addhours(-10)}) | Format-table Name,LastWriteTime -HideTableHeaders"

有了这种衬垫,当文件的修改时间超过10小时时,我就会得到想要的结果,但是如果没有结果,我还需要它来打印值/字符串,以便我可以对其进行适当地监视。 这样做的原因是利用脚本/一个衬纸进行监视。

如果未找到任何内容,则这些cmdlet Get-ChildItem和where子句将返回null。 您将不得不单独考虑。 我还要提醒您使用Format-Table进行输出,除非您只是将其用于屏幕阅读。 如果您想要“单线”,您可以这样做。 如果需要,所有PowerShell代码都可以是一个内衬。

$results = Get-ChildItem -Path C:\blaa\*.* | where {$_.Lastwritetime -lt (date).addhours(-10)} | Select Name,LastWriteTime; if($results){$results}else{"No files found matching criteria"}

您的代码中添加了一个括号,可能是复制工件,我不得不删除了它。 编码正确,看起来像这样

$results = Get-ChildItem -Path "C:\blaa\*.*" | 
    Where-Object {$_.Lastwritetime -lt (date).addhours(-10)} | 
    Select Name,LastWriteTime

if($results){
    $results
}else{
    "No files found matching criteria"
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM