我需要更改每 30 分鐘運行一次的腳本。它目前所做的是檢查檔案是否存在,如果它在腳本運行時當前位于該位置,它將回傳檔案路徑供我們調查為什么它尚未移出。
我們無法在主要匯入位置運行它,因為檔案不斷出現,這將導致永遠每 30 分鐘發送一封電子郵件。
能否請您幫忙在專案上添加上次修改日期的檢查,因此只有超過30分鐘的那些,每30分鐘運行一次檢查時,才會回傳?
function folder_check {
param (
[Parameter(Mandatory=$true)] [string]$folder
)
if((Get-ChildItem -Attributes !Directory !System, !Directory !System Compressed $folder -force | Select-Object -First 1 | Measure-Object).Count -gt 0) {
Write-Host "Found"
return 1;
}
Write-Host "Not found"
return 0;
}
foreach ($folder in $folders) {
Write-Host "Checking $folder"
if (folder_check $folder) {
$found_folder = 1;
$lst = (gci -Attributes !Directory !System, !Directory !System Compressed -Path "$folder" -Force | Out-String -Width 80)
$body = ($body $lst);
}
}
uj5u.com熱心網友回復:
Get-ChildItem與 switch 結合-File只回傳檔案,而不是目錄,因此不需要屬性!Directory。您將 cmdlet 與 switch 結合使用-Force,因此它還會回傳用戶無法訪問的專案,例如隱藏檔案或系統檔案。忽略該引數,將處理屬性!System..
此外,我不認為需要一個函式來迭代檔案并回傳一個整數,1或者0讓主腳本做幾乎完全相同的事情。
洗掉該功能folder_check并將代碼調整為:
$refDate = (Get-Date).AddMinutes(-30)
$body = foreach ($folder in $folders) {
Write-Host "Checking $folder"
# gather a list of files older than 30 minutes ago
$lst = @(Get-ChildItem -Path $folder -File | Where-Object { $_.LastWriteTime -lt $refDate })
if ($lst.Count) {
# output the full path and filenames we have in array $lst
$lst.FullName
}
}
# now, $body will contain an array of full path and filenames to process further
$body
您稍后沒有告訴我們您想用該串列做什么,但從您的評論中我可以看出您想通過電子郵件發送找到的檔案。
然后簡單地做:
if (@($body).Count) {
send_email
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/381805.html
標籤:电源外壳
