windows powershell mail error

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of 84rusty . 84rusty . 2 months, 3 weeks ago.

  • Author
    Posts
  • #53805
    Profile photo of venkatesh SQL
    venkatesh SQL
    Participant

    Hi,
    We have a requirement where files in a particular directory get processed the corresponding business users need to be intimated via email. All the Directories and Sub-directories have email associated and all this information is saved in a comma separated file.

    $From = "myemail@domain.com"
    $To = "businessuser@domain.com"
    $Cc = "stakeholder@domain.com"
    $Subject = "File process intimation"
    $Body = "File processed"
    $SMTPServer = "smtp.domain.com"
    $SMTPPort = "1739"

    $flatFilePath = "C:\Temp\Email.csv"
    $flatFile = Import-Csv -Path $flatFilePath -Header Dir,Email
    $emailID = @{}
    foreach ($record in $flatFile) { $emailID[$record.Dir]=$record.Email }

    # organization folder
    $orgFolder = Get-ChildItem -Path "C:\Temp\" -recurse -exclude *.*

    # Send email
    foreach ($dir in $orgFolder) {
    $processedfiles = $null
    $emailTo = $null

    Get-ChildItem -Path $dir.FullName -Recurse -include *.txt,*.xls,*.csv,*.xlsx | ? { $_.FullName } |
    Foreach-Object {
    [string]$internalOrg = [System.IO.DirectoryInfo]$_.Directory.Name
    $processedfiles += [string][System.IO.Path]::GetFileName($_.FullName) + " | ";
    }

    $emailTo = $emailID.Get_Item($dir.Name) -split ";"

    if ($processedfiles) { Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject -Body "$Body $processedfiles" -SmtpServer $SMTPServer -port $SMTPPort }

    }

    This is the ps script to send email. This works okay but when I have a file to process in "C:\Temp\Accounts\GL" and no included file in "C:\Temp\Accounts" it still sends out 2 different email to accounts@domain.com & accountsgl@domain.com.

    Data in comma separated file:

    Dir Email
    Accounts accounts@domain.com
    GL accountsgl@domain.com
    AR accountsar@domain.com
    AP accountsap@domain.com
    Sales sales@domain.com
    SalesOrder salesorder@domain.com

    Thank you.

  • #53835
    Profile photo of 84rusty .
    84rusty .
    Participant

    Does this work for you?

    	
    	$From = "myemail@domain.com"
    	$To = "businessuser@domain.com"
    	$Cc = "stakeholder@domain.com"
    	$Subject = "File process intimation"
    	$Body = "File processed"
    	$SMTPServer = "smtp.domain.com"
    	$SMTPPort = "1739"
    
    	$flatFilePath = "C:\temp\Email.csv"
    	$flatFile = Import-Csv -Path $flatFilePath -Header Dir,Email
    
    	$emailID = @{}
    	foreach ($record in $flatFile) { 
    		$emailID[$record.Dir]=$record.Email
    	}
    
    	# organization folder
    	$orgFolder = Get-ChildItem -Path c:\temp -recurse -exclude *.*
    
    	# Send email
    	foreach ($dir in $orgFolder) {
    
    		$processedfiles = $null
    		$emailTo = $null
    
    
    		Get-ChildItem -Path "$($dir.FullName)\*.*" -include *.txt,*.xls,*.csv,*.xlsx | ? { $_.FullName"} | Foreach-Object {
    
    			[string]$internalOrg = [System.IO.DirectoryInfo]$_.Directory.Name
    			
    			$processedfiles += [string][System.IO.Path]::GetFileName($_.FullName) + " | "
    
    		}
    
    		$emailTo = $emailID.Get_Item($dir.Name) -split ";"
    
    		if ($processedfiles) { 
    			Send-MailMessage -From $From -to $emailTo -Cc $Cc -Subject $Subject -Body "$Body $processedfiles" -SmtpServer $SMTPServer -port $SMTPPort 
    		}
    	}
    
    • This reply was modified 2 months, 3 weeks ago by Profile photo of 84rusty . 84rusty ..
    • This reply was modified 2 months, 3 weeks ago by Profile photo of 84rusty . 84rusty ..
  • #53902
    Profile photo of venkatesh SQL
    venkatesh SQL
    Participant

    Hi, For some reason file process duplication happens. My folder structure is "C:\Temp\SalesOrder\20160809\cc.txt" when its looking for all the files that need to be processed as soon as the recursion gets to folder "C:\Temp\SalesOrder" & "C:\Temp\SalesOrder\20160809" its processing cc.txt file. How do I rewrite the gci to not to traverse subfolders and only process files "C:\Temp\SalesOrder\20160809"

    Problem area:
    Get-ChildItem -Path $dir.FullName -Recurse -Include *.txt,*.xls,*.csv,*.xlsx | ? { $_.FullName } |
Foreach-Object {
[string]$internalOrg = [System.IO.DirectoryInfo]$_.Directory.Name
$processedfiles += [string][System.IO.Path]::GetFileName($_.FullName) + " | ";
}

    I want to process files only when the file that meets the criteria exists. For some reason even though I have just 1 file to process in all the folders put together it does process more than once and cannot figure out why? I have no hidden files either.

    Thank you.

    • #53952
      Profile photo of 84rusty .
      84rusty .
      Participant

      Hi venkatesh,

      Check out the code I supplied in my previous post, it is your code just with the fix applied.

      Specifically you mention the gci section as a problem, so remove the -Recurse, and add *.* onto the -path so it picks up the files, like so:
      Get-ChildItem -Path "$($dir.FullName)\*.*" -include *.txt,*.xls,*.csv,*.xlsx

      You don't need to recurse on this call as you recursed earlier ($orgFolder = Get-ChildItem -Path c:\temp -recurse -exclude *.*)

      • This reply was modified 2 months, 3 weeks ago by Profile photo of 84rusty . 84rusty ..

You must be logged in to reply to this topic.