Program Jim
Systems Engineer
Hi,
I have created this code:
But if the file does exist i dont get an email i believe the code is closing after file removal also once run if i try to rerun from ISE or Powershell prompt the error advises the file is open in another process.
I think its something really simple to solve all i want it to do is.
1) If file exists
2) then delete & then run $log = get-eventlog Application -After (Get-Date).adddays(-1) | where {$_.InstanceId -eq 916} | Out-File C:\BackupLog\BackupFailure.txt & email
3) If file doesnt exist run $log = get-eventlog Application -After (Get-Date).adddays(-1) | where {$_.InstanceId -eq 916} | Out-File C:\BackupLog\BackupFailure.txt & email.
Thanks James
I have created this code:
Code:
#Varibles
$compname = $env:computername
$SourceFile = "C:\Backuplog\BackupFailure.txt"
#If source file does exists delete text file
if (Test-Path $SourceFile) {
Remove-Item $SourceFile
} else {
# Check for event in event log & write last event to file
$log = get-eventlog Application -After (Get-Date).adddays(-1) | where {$_.InstanceId -eq 916} | Out-File C:\BackupLog\BackupFailure.txt
# Sender and Recipient Info
$MailFrom = "frank@peter.com"
$MailTo = "jimmy@bob.com"
# Sender Credentials
$Username = "username"
$Password = "password"
# Server Info
$SmtpServer = "smtpserver"
$SmtpPort = "252525"
# Message stuff
$MessageSubject = "Backup Failure For %Customer%"
$Message = New-Object System.Net.Mail.MailMessage $MailFrom,$MailTo
$Message.IsBodyHTML = $true
$Message.Subject = $MessageSubject
$Message.Attachments.Add("C:\BackupLog\BackupFailure.txt")
$Message.Body = "Backup failure on device:- $compname for their backup."
$Message.Body = "Please read the attached text file for the error & invesitgate on the server."
# Construct the SMTP client object, credentials, and send
$Smtp = New-Object Net.Mail.SmtpClient($SmtpServer,$SmtpPort)
$Smtp.EnableSsl = $true
$Smtp.Credentials = New-Object System.Net.NetworkCredential($Username,$Password)
$Smtp.Send($Message)
But if the file does exist i dont get an email i believe the code is closing after file removal also once run if i try to rerun from ISE or Powershell prompt the error advises the file is open in another process.
Code:
Remove-Item : Cannot remove item C:\DDS\Backuplog\BackupFailure.txt: The process cannot access the file 'C:\DDS\Backuplog\BackupFailure.txt' because it is being used by
another process.
At C:\BackupLog\Backup-Failure.ps1:9 char:1
+ Remove-Item $SourceFile
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (C:\Backuplog\BackupFailure.txt:FileInfo) [Remove-Item], IOException
+ FullyQualifiedErrorId : RemoveFileSystemItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand
I think its something really simple to solve all i want it to do is.
1) If file exists
2) then delete & then run $log = get-eventlog Application -After (Get-Date).adddays(-1) | where {$_.InstanceId -eq 916} | Out-File C:\BackupLog\BackupFailure.txt & email
3) If file doesnt exist run $log = get-eventlog Application -After (Get-Date).adddays(-1) | where {$_.InstanceId -eq 916} | Out-File C:\BackupLog\BackupFailure.txt & email.
Thanks James