我正在尝试创建一个PowerShell脚本,该脚本将每天早上运行一次(通过任务调度程序),并通知某人(通过直接发送(我知道如何做到这一点,并将在以后实现上述功能),如果没有在前一天的每一个小时创建一个文件,那么就不必再手动地或在出现问题时这样做了。文件名(如$FilePattern中所示)都包含创建它们的日期以及时间。显然,Foreach循环中的逻辑是有缺陷的,因为$Time总是在$Hours中,但是我一直在抓我的头,还没有弄清楚我该怎么做。我想我可以比较两个数组,但我现在不太确定。我还遇到了看似有希望的比较对象,但我无法完全理解它。我为这一团糟道歉。我的当前代码如下所示。
[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."
}
}发布于 2020-05-03 07:07:37
下面是一个更短、更易读的实现:
$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。发布于 2020-05-01 20:11:14
我仍然需要重构和改变一些东西,但这似乎是可行的。
[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."
}
}https://stackoverflow.com/questions/61510029
复制相似问题