×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

File Exists Issue

File Exists Issue

File Exists Issue

(OP)
Hi,

I have created this code:

CODE --> PowerShell

#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 --> PowerShell

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

RE: File Exists Issue

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

RE: File Exists Issue

(OP)
Gbaughma,

I have altered all important details to fake values ie. smtp port user creds and passwords & smtp server.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close