Send email if a copy-item cmdlet fails
i've got script send email, , know how copy files, don't seem able email on failure. failure example might network share isn't available. know i'm missing fundamental here, first real shot @ using powershell i'm struggling bit.
currently i'm using try {
copy-item source destination
}
catch {
there problem copy job: $_
$emailfrom = ""
$emailto = ""
$subject = ""
$body = ""
$smtpserver = ""
$smtp = new-object net.mail.smtpclient($smtpserver)
$smtp.send($emailfrom, $emailto, $subject, $body)
}
try/catch triggers catch block if encounters terminating error. can use -ea parameter force errors terminating, catch block invoked.
try {
copy-item source destination -ea stop
}
catch {
there problem copy job: $_
$emailfrom = ""
$emailto = ""
$subject = ""
$body = ""
$smtpserver = ""
$smtp = new-object net.mail.smtpclient($smtpserver)
$smtp.send($emailfrom, $emailto, $subject, $body)
}
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
Windows Server > Windows PowerShell
Comments
Post a Comment