0

快速提问;我想记录服务器上发生的坏事,但这不是问题。

日志应该在发送时被删除,如果在脚本运行时没有日志,它应该发送不同的消息,不带附件。

这是到目前为止的脚本:

        <#
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)
        }
}

我在这里做错了什么?

4

1 回答 1

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 
}
于 2014-06-09T12:40:26.490 回答