[英]No email and housekeep when add remove-item
脚本是关于如果找不到路径,则将 email 和 output 发送到 FileA。 如果能够找到路径并删除任何超过 30 天的日志和 output 到 FileA。 如果在该行添加 Get-ChildItem,下面的脚本将不会做任何家务 + output 到 txt 文件并将 email 发送给用户。 如果我撤销该行的 Get-ChildItem,它将向用户发送 email。
我想要的是:
####Declare file path to be housekeep $housekeepLog = "D:\logs-Copy*" << valid path $housekeepLog2 = "D:\logs-Copy2*" << not valid path,特意测试发送email
####Housekeep any log older than 30days
>when add 2 sentence below, cannot send out email and no output to text file. when remove this, will send email and output to text file.
Get-ChildItem –Path $housekeepLog -Recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-30))} | Remove-Item
Get-ChildItem –Path $housekeepLog2 -Recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-30))} | Remove-Item
####Output file
$outputHousekeep = "D:\Housekeep.txt"
$outputHousekeep2 = "D:\Housekeep2.txt"
$timestampDateTime = (Get-Date).ToString('yyyy-MM-dd HH:mm:ss')
#### housekeepLog
$EmailSplatHousekeep = @{
To = "myemail@mydomain.com"
CC = "myemail@mydomain.com"
SmtpServer = "smptserver@mydomain.com"
From = "noreply@mydomain.com"
Priority = "High"
BodyAsHtml = $true
Subject = "Folder of Log File Does't Exist"
}
if (!(Test-Path $housekeepLog))
{
$EmailSplatHousekeep.body =
"
1. do something
"
Send-MailMessage @EmailSplatHousekeep
Write-Output "$timestampDateTime The folder $housekeepLog doesn't exist! Check the folder path!" | Out-file $outputHousekeep -Append
}
else
{
####Input to outputfile
Write-Output "$timestampDateTime Done housekeeping in $housekeepLog ." | Out-file $outputHousekeep -Append
}
#### housekeepLog2
$EmailSplatHousekeep2 = @{
To = "myemail@mydomain.com"
CC = "myemail@mydomain.com"
SmtpServer = "smptserver@mydomain.com"
From = "noreply@mydomain.com"
Priority = "High"
BodyAsHtml = $true
Subject = "Folder of Log File Does't Exist"
}
if (!(Test-Path $housekeepLog2))
{
$EmailSplatHousekeep2.body =
"
1. do something
"
Send-MailMessage @EmailSplatHousekeep2
Write-Output "$timestampDateTime The folder $housekeepLog2 doesn't exist! Check the folder path!" | Out-file $outputHousekeep2 -Append
}
else
{
####Input to outputfile
Write-Output "$timestampDateTime Done housekeeping in $housekeepLog2 ." | Out-file $outputHousekeep2 -Append
}
如果我正确理解了这个问题,您可以使用一系列housekeepingLog
路径组合它并循环它们,这将为您节省大量重复代码。
您的代码的主要问题是您立即删除了旧日志,因此之后无法告诉您删除了什么..
像这样的东西:
# Declare file path to be housekeep
# first path in array is valid, second path is not
$housekeepLog = "D:\logs-Copy", "D:\logs-Copy2"
$timestamp = (Get-Date).ToString('yyyy-MM-dd HH:mm:ss')
foreach ($path in $housekeepLog) {
if (Test-Path $path -PathType Container) {
# the path exists, get logfiles older than 30 days
$refDate = (Get-Date).AddDays(-30).Date # set time to 00:00:00 (aka midnight)
# assuming the log files have the '.txt' extension.. If different, then change that in the Filter
$oldLogs = Get-ChildItem –Path $path -Filter '*.txt' -File -Recurse |
Where-Object {$_.LastWriteTime -lt $refDate}
if ($oldLogs) {
# save the full path and names of the old logfiles that are being deleted
$currentFolder = Split-Path -Path $path -Leaf
$outputFile = "D:\Housekeep-$currentFolder.txt"
Add-Content -Path $outputFile -Value $oldLogs.FullName
Add-Content -Path $outputFile -Value "$timestamp - Done housekeeping in $path.`r`n"
# remove the old log files
$oldLogs | Remove-Item -Confirm:$false
}
else {
Write-Host "No log files older than 30 days found in $path"
}
}
else {
Write-Warning "The folder '$path' does not exist.`r`nCheck the folder path!"
# the path is not found, send email
$emailProperties = @{
To = "myemail@mydomain.com"
CC = "myemail@mydomain.com"
Subject = "Folder '$path' does not exist"
SmtpServer = "smptserver@mydomain.com"
From = "noreply@mydomain.com"
Priority = "High"
Body = "The folder $path does not exist.<br>Check the folder path!"
BodyAsHtml = $true
}
Send-MailMessage @emailProperties
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.