【发布时间】:2020-04-29 19:24:28
【问题描述】:
我正在尝试创建一个 PowerShell 脚本,该脚本将每天早上运行一次(通过任务计划程序)并提醒某人(通过直接发送(我知道如何执行此操作,稍后将实现所述功能)) t 在前一天的每个小时内创建,以便不再需要手动执行此操作或出现问题时。文件名,如 $FilePattern 所示,都包含它们的创建日期以及时间。显然,我的 Foreach 循环中的逻辑是有缺陷的,因为 $Time 将始终以 $Hours 为单位,但我一直在摸不着头脑,无法弄清楚我应该如何去做。我以为我可以比较两个数组,但我现在不太确定。我还遇到了看起来很有希望的 Compare-Object,但我无法完全掌握它。我为混乱道歉。我目前的代码如下。
[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."
}
}
【问题讨论】:
-
您想将哪些信息传递给该脚本?文件名中不包含小时数吗?
标签: arrays powershell compareobject