快速提问; 我想记录发生在服务器上的不良内容,但这不是问题。
发送时应删除日志,如果脚本运行时没有日志,则应发送不同的消息,不附带附件。
到目前为止,这是脚本:
<#
Set-ExecutionPolicy unrestricted -Force
iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))
cinst BetterCredentials
#>
Import-Module BetterCredentials
$Mailcred = Get-Credential -user user@gmail.com -pass 12345password
Try{
$File = C:/path/errorfile.log
}
Catch [System.Management.Automation.PSArgumentException]
{
"invalid object"
}
Catch [system.exception]
{
"caught a system exception"
}
Finally
{
If ($File -eq ){
then (send-mailmessage -ErrorAction SilentlyContinue -from "Server <Server@company.com>" -to "IT Dept. <Itdept@company.com>" -subject "Log of the day." -body "Good Morning,
Fortuneatly, nothing bad to report today!" -priority High -dno onSuccess, onFailure -smtpServer smtp.company.com -credential ($Mailcred) -usessl)
else (send-mailmessage -ErrorAction SilentlyContinue -from "Servers <Server@company.com>" -to "IT Dept. <ITDept@company.com>" -subject "Log of the day." -body "Good Morning,
Here is your daily report." -Attachments $File -priority High -dno onSuccess, onFailure -smtpServer smtp.company.com -credential ($Mailcred) -usessl)
}
}
我在这里做错了什么?
答案 0 :(得分:1)
代码的某些部分没有任何意义。让我们回顾一下。
$File = C:/path/errorfile.log
...
If ($File -eq ){
第一部分是关于作业。为了找出存在的errorfile.log
,请使用Test-Path
cmdlet,如下所示,
$File = test-path 'C:/path/errorfile.log'
始终在文件名周围使用引号。如果路径中有空格,则不带引号的路径不起作用。
第二部分是实际测试。
If ($File){
... # file was on disk, send alert and delete it
} else {
... # no file found, send ok
}