Feb 06 2022 01:26 PM
Hi, I was just wondering if there is a way to copy a .txt file to a specific destination and then send that copy via email on a loop that runs every hour or so? I've tried to do it but have had no luck.
Feb 07 2022 01:20 AM
Feb 07 2022 04:10 AM
Feb 07 2022 04:34 AM
You could try something like
while ($true) {
#Copying file to other location
Copy-Item -Path c:\temp\filename.txt -Destination d:\backup -Force:$true
#Sending mail with the file
Send-MailMessage -SmtpServer smtp.domain.local -port 25 -Subject "Emailing the file" -Body "See attached file" -Attachments d:\backup\filename.txt -To "Email address removed" -From "Email address removed"
#Wait for 1 hour
Start-Sleep -Seconds 3600
}
Feb 08 2022 01:20 AM
Feb 08 2022 08:06 AM - edited Feb 08 2022 08:16 AM
It works with one loop but whenever it loops again it doesn't copy the file again. Instead I get an error stating that the Copy-Item cannot access the d:\backup file because its being used by another process. This only happens if I use the email script.
Feb 08 2022 08:20 AM
Feb 08 2022 08:26 AM
Feb 08 2022 08:54 AM
Solution
Some error handling and checking 🙂
function Test-FileLock {
param (
[parameter(Mandatory = $true)][string]$Path
)
$oFile = New-Object System.IO.FileInfo $Path
if ((Test-Path -Path $Path) -eq $false) {
return $false
}
try {
$oStream = $oFile.Open([System.IO.FileMode]::Open, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None)
if ($oStream) {
$oStream.Close()
}
$false
}
catch {
# file is locked by a process.
return $true
}
}
while ($true) {
#Copying file to other location but checking if it's in use first
if ((Test-Filelock -path d:\backup\filename.txt) -eq $False) {
Copy-Item -Path c:\temp\filename.txt -Destination d:\backup -Force:$true
#Sending mail with the file
Send-MailMessage -SmtpServer smtp.domain.local -port 25 -Subject "Emailing the file" -Body "See attached file" -Attachments d:\backup\filename.txt -To "Email address removed" -From "Email address removed"
}
#Wait for 1 hour
Start-Sleep -Seconds 3600
}
Feb 08 2022 09:23 AM - edited Feb 08 2022 09:50 AM
That works great! Thank you so much for all your help!!
Feb 08 2022 10:03 AM
Feb 08 2022 08:54 AM
Solution
Some error handling and checking 🙂
function Test-FileLock {
param (
[parameter(Mandatory = $true)][string]$Path
)
$oFile = New-Object System.IO.FileInfo $Path
if ((Test-Path -Path $Path) -eq $false) {
return $false
}
try {
$oStream = $oFile.Open([System.IO.FileMode]::Open, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None)
if ($oStream) {
$oStream.Close()
}
$false
}
catch {
# file is locked by a process.
return $true
}
}
while ($true) {
#Copying file to other location but checking if it's in use first
if ((Test-Filelock -path d:\backup\filename.txt) -eq $False) {
Copy-Item -Path c:\temp\filename.txt -Destination d:\backup -Force:$true
#Sending mail with the file
Send-MailMessage -SmtpServer smtp.domain.local -port 25 -Subject "Emailing the file" -Body "See attached file" -Attachments d:\backup\filename.txt -To "Email address removed" -From "Email address removed"
}
#Wait for 1 hour
Start-Sleep -Seconds 3600
}