When I run my script there is a piece after the email ... I used a comment to show line
that always gets a file in USE or in use by another process. I want to after each file I process move it
to an archive folder so I don't reprocess if it was successful. I would also like to rename the file before I move to archive to avoid duplicates(just in case)I have code commented out, but would like to get that installed along with
figuring out why I get the file in use error. I'm creating an email piece and have another script that does the faxing.
Thanks.
Script.
Function Write-Log($LogFile,$LogText){
If($LogFile -match '_fax'){
$LogEntry=@"
$($LogText)"@
}else{
$LogEntry=@"
$((Get-Date).ToString("dd-MM-yyyy HH:mm:ss")) > $($LogText)"@
}
$LogEntry
$LogEntry | Add-Content $LogFile # I want this to be Shippernbr.txt
}
$writelogdir = 'c:\test'
$rootfolder='c:\test'
$files=Get-ChildItem "$rootfolder\*.csv"
$def = "ggg@dcm.com"
ForEach($file in $files){
Write-Log "$writelogdir\files-processed.log" "Found files $file"
Import-CSV $file -delimiter '|' | ForEach-Object{
$attachment="$rootfolder\pdf\$($_.Shipper).pdf"
$shipper = ($_.Shipper)
$sub = 'Autosend for #' + ' ' + $shipper
If($_.email) {
Write-Log "$writelogdir\$($_.Shipper)_email.log" "Attachment is $($attachment)"
$email=$_.Email
Write-Log "$writelogdir\$($_.Shipper)_email.log" "Email address is $($email)"
$emailCC=$_.EmailCC
Write-Log "$writelogdir\$($_.Shipper)_email.log" "Email CC address is $($emailCC)"
$subject=$_.Shipper
Write-Log "$writelogdir\$($_.Shipper)_email.log" "Autosend for # $($subject)"
$from=$_.ReturnEmail
Write-Log "$writelogdir\$($_.Shipper)_email.log" "From Address $($from)"
}
Else
{
Write-Log "$writelogdir\files-processed.log" "No Email Address found in File--> $file"
}
# Write a Fax Doc file if FaxNbr Not Blank
If($_.FaxNbr) {
Write-Log "$writelogdir\$($_.Shipper)_fax.log" "<adddoc:$attachment>"
$Faxnbr = $_.Faxnbr
Write-Log "$writelogdir\$($_.Shipper)_fax.log" "<TOFAXNUM:$($_.FaxNbr)>"
$Attn = $_.Attn
Write-Log "$writelogdir\$($_.Shipper)_fax.log" "<TONAME:$($_.Attn)>"
Write-Log "$writelogdir\$($_.Shipper)_fax.log" "<TOCOMPANY: PACK:$($file)>"
Write-Log "$writelogdir\$($_.Shipper)_fax.log" "<FROMNAME: AUTO FAX>"
}
Else
{
Write-Log "$writelogdir\files-processed.log" "FaxNbr Not found in File--> $file"
}
# Check if Return Email has valid data
If($_.ReturnEmail){
$from=$_.ReturnEmail
}
Else
{
$from=$def
Write-Log "$writelogdir\$($_.Shipper)_email.log" "From Address Blank Using Default $($from)"
}
# Send Email
#Send-MailMessage -from $from -To $email -Cc $emailCC -Subject $sub -attachments $attachment -SmtpServer 'local' -ErrorAction Continue
if($?){
Write-Log "$writelogdir\$($_.Shipper)_email.log" "Email Sent Successfully"
Move-Item $file -Destination "$rootfolder\archive\" #File in USE Message
Write-Log "$writelogdir\$($_.Shipper)_email.log" "Moved file $($file) to $rootfolder\archive\"
}else{
Write-Log "$writelogdir\$($_.Shipper)_email.log" "Email Send failed: $($error[0])";break
}
gci "$writelogdir\*.csv" | % { rename-item –ath $_.Fullname –ewname ( $_.basename + (get-date -format ' dd-MM-yyyy') + $_.extension) -WhatIf
}
}
}