Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File Exists Issue

Status
Not open for further replies.

Program Jim

Systems Engineer
Nov 21, 2018
3
GB
Hi,

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
 
Is that SMTP port correct? I thought port ranges only went to 65535?



Just my $.02

"What the captain doesn't realize is that we've secretly replaced his Dilithium Crystals with new Folger's Crystals."

--Greg
 
Gbaughma,

I have altered all important details to fake values ie. smtp port user creds and passwords & smtp server.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top