File Check Script

Welcome Forums General PowerShell Q&A File Check Script

Viewing 3 reply threads
  • Author
    Posts
    • #224166
      Participant
      Topics: 2
      Replies: 1
      Points: 24
      Rank: Member

      Im still fairly new to using powershell and scripting, I have a script that grabs a file out of a folder and emails it to whoever needs it daily.

      What im wondering is, Can I make a separate .txt file that has all the emails inside and it sends it to all of them?

      also, how would I use the script to basically do this:

      if file with name “test” created with todays date is there, send email saying successful

      if file with name “test” ย is not there with todays date, send email saying failed.

      this is what I have currently-

      #Request model for daily email

      $file = “E:\test.xlsx”

      $smtpServer = “123.45.67.890”

      $msg = new-object Net.Mail.MailMessage
      $att = new-object Net.Mail.Attachment($file)
      $smtp = new-object Net.Mail.SmtpClient($smtpServer)

      $msg.From = “xxx@xxx.com”
      $msg.To.Add(“xxxx@xxxx.com”)
      $msg.Subject = “xxxx”
      $msg.Body = “Automated E-mail”
      $msg.Attachments.Add($att)

      $smtp.Send($msg)

    • #224178
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP

      Bradley, welcome to Powershell.org. Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! Youโ€™ll be Glad You Did!.

      When you post code or error messages or sample data or console output format it as code, please.
      In the “Text” view you can use the code tags “PRE“, in the “Visual” view you can use the format template “Preformatted“. You can go back edit your post and fix the formatting – you don’t have to create a new one.
      Thanks in advance.

      A simple version could be something like this:

      $Path = 'E:\test.xlsx'
      $File = Get-Item -Path $Path
      $today = (Get-Date).Date
      
      $SendMailMessageSplat = @{
              from         = 'xxx@xxx.com'
              to           = 'xxxx@xxxx.com'
              subject      = $subject
              body         = 'Automated E-mail'
              smtpserver   = '123.45.67.890'
          }
      
      If($File.LastWriteTime.Date -eq $today){
          $subject = 'Successful'
          $SendMailMessageSplat.Add('attachements', $Path)
      }
      else {
          $subject = 'Failed'
      }
      
      Send-MailMessage @SendMailMessageSplat
      • #224292
        Participant
        Topics: 2
        Replies: 1
        Points: 24
        Rank: Member

        This was super helpful, I was able to easily implement my idea with the help.

        now for the second part, hopefully you see this im unsure how notifications work on this site but if I wanted to email all emails that were in a .txt file would I set it up like this

        $bcclist = Get-content 'E:\list.txt

        and then for the to would I just do

        to = '$bcclist'

        would that send it to all of them? the txt file has the listed like this:

        xxxx@email

        xxxx@email

        xxxx@email

         

    • #224181
      Senior Moderator
      Topics: 8
      Replies: 1215
      Points: 4,334
      Helping Hand
      Rank: Community Hero

      How are you planning to execute the script, via taskscheduler ?

      You can always use Get-ChildItem which will have the Datecreated property and use that property to compare against the current date to decide wither to send success or failure message. And to pick the addresses from a txt file(one recipient in a line), you would use Get-Content cmdlet then pass it to th -To parameter of Send-MailMessage cmdlet.

      You don’t need to use Net.Mail.MailMessage class, instead use Send-MailMessage cmdlet. The detailed help for above cmdlets below

      Send-MailMessage – https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/send-mailmessage?view=powershell-5.1

      Get-ChildItem – https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem?view=powershell-5.1

      Get-Content – https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-content?view=powershell-5.1

      • This reply was modified 3 weeks, 5 days ago by kvprasoon. Reason: post correction
    • #224409
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP

      Please read my first answer again … completely … not just the code suggestion i posted. ๐Ÿ˜‰

      This was super helpful, I was able to easily implement my idea with the help.

      Great. I hope you don’t just use the code. I hope you try to understand what exactly it does.

      now for the second part, hopefully you see this im unsure how notifications work on this site but if I wanted to email all emails that were in a .txt file would I set it up like this

      Instead of an answer with a piece of code ready to use I’d like to encourage you to do your own research and try to solve your problem yourself. You might have already heard about the method “Trial and Error“. ๐Ÿ˜‰ Plesae always read the help for the cmdlets you’re about to use including the examples to learn how to use them. And if you got stuck with your code you can come back and we will try to help you. ๐Ÿ˜‰

Viewing 3 reply threads
  • You must be logged in to reply to this topic.