首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >PowerShell -每小时创建一个文件吗?

PowerShell -每小时创建一个文件吗?
EN

Stack Overflow用户
提问于 2020-04-29 19:24:28
回答 2查看 344关注 0票数 2

我正在尝试创建一个PowerShell脚本,该脚本将每天早上运行一次(通过任务调度程序),并通知某人(通过直接发送(我知道如何做到这一点,并将在以后实现上述功能),如果没有在前一天的每一个小时创建一个文件,那么就不必再手动地或在出现问题时这样做了。文件名(如$FilePattern中所示)都包含创建它们的日期以及时间。显然,Foreach循环中的逻辑是有缺陷的,因为$Time总是在$Hours中,但是我一直在抓我的头,还没有弄清楚我该怎么做。我想我可以比较两个数组,但我现在不太确定。我还遇到了看似有希望的比较对象,但我无法完全理解它。我为这一团糟道歉。我的当前代码如下所示。

代码语言:javascript
复制
[CmdletBinding()]

param(

    [Parameter(Mandatory=$false)]
    [ValidateScript({ Test-Path $_ -PathType Container })]
    [string]
    $Path = "C:\Users\<username>\Desktop\Archive",
    $Year = [DateTime]::Today.AddDays(-1).Year,
    $Month = [DateTime]::Today.AddDays(-1).Month,
    $Day = ([DateTime]::Today.AddDays(-1).Day),
    $strMonth = ([String]$Month).PadLeft(2,'0'),
    $strDay = ([String]$Day).PadLeft(2,'0'),
    $Date = "$Year" + "$strMonth" + "$strDay",
    $FilePattern = @("850_" + $Date + "*.txt"),
    $Files = (Get-ChildItem -Path $Path -Include $FilePattern -Recurse | 
        Select-Object -ExpandProperty CreationTime | Get-Date -f "yyyyMMdd"),
    $Time = (Get-ChildItem -Path $Path -Include $FilePattern -Recurse |
        Select-Object -ExpandProperty CreationTime | Get-Date -f "hh"),
    $Hours = @(0..23)

)

Foreach ($File in $Files) {

    #if (-Not (Test-Path "$Path\$FilePattern")) {
    #    Write-Host "Warning: The file path doesn't exist!"
    #} else {
    #    Write-Host "The file path exists..."
    #}

    if ($Hours -notin $Time) {
        Write-Host "There's no file present for $Time o'clock for $Date."
    } else {
        Write-Host "There's at least one file per hour for $Date."
    }

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-05-03 07:07:37

下面是一个更短、更易读的实现:

代码语言:javascript
复制
$Path = "C:\Users\<username>\Desktop\Archive\*"
$Date = (Get-Date).AddDays(-1).ToString('yyyyMMdd')
$Hours = Get-ChildItem -Path $Path -Include "850_$($Date)*.txt" | ForEach-Object {(Get-Date $_.CreationTime).Hour} | Sort-Object | Get-Unique
for ($Hour = 0; $Hour -lt 24; $Hour++) {
    if ($Hour -in $Hours) {
        Write-Host "There's at least one file for $Hour o'clock for $Date." -ForegroundColor Green
    } else {
        Write-Host "There's no file present for $Hour o'clock for $Date." -ForegroundColor Red
    }
}

所以我改变了什么:

  • 我删除了params块,因为我认为您只是使用它来初始化变量,但是它的设计是为了定义要传递给函数的参数。看起来你不想把任何东西传递给那个脚本。纠正我,如果我错了。
  • 我在路径中添加了一个星号(*),以使-Include参数工作(参见文档)。我认为你添加-Recurse是为了让它正常工作。仅当您的日志位于子目录中时才使用-Recurse
  • 我添加了一种更易读、更容易理解的方法来创建所需的时间戳。
  • 我提取了小时,您正在寻找的是数字,这样您就可以将它们与数字进行比较,而不必创建一个由两位数组成的字符串化小时数组。
  • 我在输出中添加了颜色,以获得更快的概述(如果缺少任何文件)。
票数 1
EN

Stack Overflow用户

发布于 2020-05-01 20:11:14

我仍然需要重构和改变一些东西,但这似乎是可行的。

代码语言:javascript
复制
[CmdletBinding()]

param(

    $Path = "C:\Users\<username>\Desktop\Archive",
    $Year = [DateTime]::Today.AddDays(-1).Year,
    $Month = [DateTime]::Today.AddDays(-1).Month,
    $strMonth = ([String]$Month).PadLeft(2,'0'),
    $Day = ([DateTime]::Today.AddDays(-1).Day),
    $strDay = ([String]$Day).PadLeft(2,'0'),
    $Date = "$Year" + "$strMonth" + "$strDay",
    $Hours = @(
        "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11",
        "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"
    ),
    $strHours = $Hours.ToString().PadLeft(2,'0'),
    $FilePattern = @("850_" + $Date + "*.txt"),
    $Files = (Get-ChildItem -Path $Path -Include $FilePattern -Recurse | 
        Select-Object -ExpandProperty CreationTime | Get-Date -f "HH")

)

ForEach ($Hour in $Hours) {

    if ($Hour -notin $Files) {

        Write-Host "There's no file present for $Hour o'clock for $Date."

    } else {

        Write-Host "There's at least one file for $Hour o'clock for $Date."

    }

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61510029

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档