email captured results of "if" "else"
$date = get-date -format yyyymmdd new-item c:\dnsbk\dnsbackup$date -itemtype directory $source = "c:\windows\system32\dns\*" $destination = "c:\dnsbk\dnsbackup$date" $copy = copy-item -recurse $source $destination -passthru #if ($copy) {"copy successfull"} #else {"copy failure"} if(-not $?) {$fail = write-warning "copy failed"} else {$succ = write-host "success"} send-mailmessage -from "admin@test.ac.uk" -to "admin2@test.ac.uk" -subject $successsubject -smtpserver 192.168.0.1 -body {$succ or $fail}
its simple thing trying (i sure guys anyway) trying email results of copy-item. want know if copy-item successful or failed! (it doesn't work @ moment, sure can see)
thanks in advance
-metho
can clarify isn't working or errors get?
there problems code:
1. path passed new-item used twice in code - might best store in variable.
2. not why used -passthru switch copy-item - it's not needed here.
3. don't use if/else - use try/catch instead. attempt copy , if terminating errors returned, catch block executed.
4. successsubject variable null there no subject in email.
5. scriptblock passed body of email not quite right. first, there no reason have 2 different variables success or failure. use 1 called result. also, scriptblock not know word 'or' , funky results.
try this:
$date = get-date -format yyyymmdd
$path = "c:\dnsbk\dnsbackup$date" new-item $path -itemtype directory $source = "c:\windows\system32\dns\*" $destination = $path try { copy-item -recurse $source $destination $result = 'success!' } catch { $result = 'failure!' } send-mailmessage -from "admin@test.ac.uk" -to "admin2@test.ac.uk" -subject copyresult -smtpserver 192.168.0.1 -body $result
Windows Server > Windows PowerShell
Comments
Post a Comment