ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Powershell output in Email is Possible

    Scheduled Pinned Locked Moved IT Discussion
    powershellsend-mailmessagewindowsscripting
    21 Posts 5 Posters 3.6k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Mike DavisM
      Mike Davis
      last edited by

      What would be calling the script? As Scott says, just call the .ps1 and forget about using a .bat

      1 Reply Last reply Reply Quote 1
      • ObsolesceO
        Obsolesce
        last edited by Obsolesce

        I do that in a few places. A .bat file that calls a .ps1. And within that .ps1, I use this to send an email:

        # Gets date:
        $emailDate = Get-Date
        
        # -------------------- Specify Email Variables Below ------------------------- #
        
        # Specifies who the notification email goes to:
        $emailTo = "[email protected]"
        
        # Specifies who the notification email comes from:
        $emailFrom = "Server Alert <[email protected]>"
        
        # Specifies the Subject of the notification email:
        $emailSubject = "Your email subject"
        
        # Specifies the Body of the notification email:
        $emailBody = "Body content in HTML format... how about an $emailDate.<br><br>Attached is a copy of the attachment.<br><br>More text:<br><br><font face='tahoma' size='5'>$servList</font>"
        
        # Specifies the attachment to include in the notification email:
        $emailAttachment = "$dir\$latestBUErrorLog"
        
        # Specifies the name of the SMTP Server to use:
        $emailSMTPServer = "smtpServer"
        
        # Specifies the SMTP Server Port to use:
        $emailSMTPServerPort = "25"
        
        # ------------------- End Email Variables ------------------------------------ #
        
        Send-MailMessage -To "$emailTo" -From "$emailFrom" -Subject "$emailSubject" -Body "$emailBody" -BodyAsHtml -Attachments "$emailAttachment" -SmtpServer "$emailSMTPServer" -Port "$emailSMTPServerPort"
        

        And of course, you'll need to define those variables in the script before that line.

        Edit: I'm including the variable definitions so Jared doesn't yell at me.

        LakshmanaL 1 Reply Last reply Reply Quote 2
        • LakshmanaL
          Lakshmana @Obsolesce
          last edited by

          @tim_g This script in Powershell is good.How to call this from .Bat ?
          I am in start command but the powershell closes automatically! Any suggestion here

          scottalanmillerS 1 Reply Last reply Reply Quote 0
          • LakshmanaL
            Lakshmana
            last edited by

            I need .bat because i will schedule this job in Windows Scheduler

            scottalanmillerS 1 Reply Last reply Reply Quote 0
            • NerdyDadN
              NerdyDad
              last edited by

              If you are using Task Scheduler, then you call powershell.exe and run the ps1 script as an option of the command. Just make sure you run it as an administrator.

              1 Reply Last reply Reply Quote 2
              • scottalanmillerS
                scottalanmiller @Lakshmana
                last edited by

                @lakshmana said in Powershell output in Email is Possible:

                I need .bat because i will schedule this job in Windows Scheduler

                You don't need BAT for that.

                1 Reply Last reply Reply Quote 0
                • scottalanmillerS
                  scottalanmiller @Lakshmana
                  last edited by

                  @lakshmana said in Powershell output in Email is Possible:

                  @tim_g This script in Powershell is good.How to call this from .Bat ?
                  I am in start command but the powershell closes automatically! Any suggestion here

                  Batch files are "deprecated", don't use an old approach to do new things.

                  1 Reply Last reply Reply Quote 0
                  • LakshmanaL
                    Lakshmana
                    last edited by

                    Then need to save this file and how to run this powershell command automatically ?any suggeations?

                    scottalanmillerS NerdyDadN 2 Replies Last reply Reply Quote 0
                    • scottalanmillerS
                      scottalanmiller @Lakshmana
                      last edited by

                      @lakshmana said in Powershell output in Email is Possible:

                      Then need to save this file and how to run this powershell command automatically ?any suggeations?

                      I'm confused. What's the question? Just use PowerShell instead of BATCH. This is the purpose of PowerShell.

                      LakshmanaL 1 Reply Last reply Reply Quote 0
                      • LakshmanaL
                        Lakshmana @scottalanmiller
                        last edited by

                        @scottalanmiller How to automate this powershell according some time interval in the machine ?

                        scottalanmillerS 1 Reply Last reply Reply Quote 0
                        • scottalanmillerS
                          scottalanmiller @Lakshmana
                          last edited by

                          @lakshmana said in Powershell output in Email is Possible:

                          @scottalanmiller How to automate this powershell according some time interval in the machine ?

                          With the Task Scheduler.

                          https://technet.microsoft.com/en-us/library/cc748993(v=ws.11).aspx

                          1 Reply Last reply Reply Quote 2
                          • LakshmanaL
                            Lakshmana
                            last edited by

                            Ok Thanks

                            1 Reply Last reply Reply Quote 0
                            • NerdyDadN
                              NerdyDad @Lakshmana
                              last edited by

                              @nerdydad said in Powershell output in Email is Possible:

                              If you are using Task Scheduler, then you call powershell.exe and run the ps1 script as an option of the command. Just make sure you run it as an administrator.

                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

                              @lakshmana said in Powershell output in Email is Possible:

                              Then need to save this file and how to run this powershell command automatically ?any suggeations?

                              1 Reply Last reply Reply Quote 0
                              • ObsolesceO
                                Obsolesce
                                last edited by

                                Here is a script I made to set up a scheduled task to either launch a PowerShell script, or a batch file.

                                Comment out the $action line that you do NOT want to use. The below script will create a scheduled task that launches a PowerShell script called psScript.ps1.

                                Note that you must execute these lines in an elevated PowerShell window.

                                #Requires -RunAsAdministrator
                                
                                $action = New-ScheduledTaskAction -Execute 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe' -Argument "-ExecutionPolicy bypass -NonInteractive -NoLogo -NoProfile -File '\\server\path\to\psScript.ps1'"
                                # $action = New-ScheduledTaskAction -Execute '\\server\path\to\batchFile.bat'
                                $trigger = New-ScheduledTaskTrigger -AtLogon
                                $principal = New-ScheduledTaskPrincipal -GroupId "BUILTIN\Administrators" -RunLevel Highest
                                $settings = New-ScheduledTaskSettingsSet
                                $task = New-ScheduledTask -Action $action -Trigger $trigger -Principal $principal -Settings $settings
                                Register-ScheduledTask -TaskName "kickoff" -InputObject $task
                                

                                If you choose to launch the PowerShell script via a batch file, here's what you need to have in your .bat file:

                                Powershell.exe -executionpolicy bypass -File "\\server\path\to\psScript.ps1"
                                
                                1 Reply Last reply Reply Quote 0
                                • 1
                                • 2
                                • 1 / 2
                                • First post
                                  Last post