powershell - IF statement + send-mailmessage confused -


quick question; want make log of bad stuff happening servers, that's not question.

the log should deleted upon send, , if there no log @ time script being run should send different message, without attachment.

here's script far:

        <# 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 system exception"  }  { if ($file -eq ){     (send-mailmessage -erroraction silentlycontinue -from "server         <server@company.com>" -to "it dept. <itdept@company.com>" -subject "log of day." -body             "good morning,      fortuneatly, nothing bad 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 day." -body     "good morning,          here daily report." -attachments $file -priority high -dno onsuccess,     onfailure  -smtpserver smtp.company.com -credential ($mailcred) -usessl)         } } 

what doing wrong here?

some parts of code not make sense. let's review those.

$file = c:/path/errorfile.log ... if ($file -eq ){ 

the first part assignment. in order find out wether errorfile.log exists, use test-path cmdlet so,

$file = test-path 'c:/path/errorfile.log' 

always use quotes around file name. if got spaces within path, unquoted path doesn't work.

the second part actual test.

if ($file){ ... # file on disk, send alert , delete } else { ... # no file found, send ok  } 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -