Announcement

Collapse
No announcement yet.

Script to email printing status

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Script to email printing status

    I need help with a script which I can schedule to run every 10 mins and check the status on the print spooler services and send out email to admins and start the print spooler if it was off. I am trying the below script I found online but it is not working. What I am doing wrong, when I try to execute the script.

    Moderator edit: Wrapped [Code]-tags around the code below
    Code:
    strComputer = "."
    
    strService = "Print Spooler"
    Set objEmail = CreateObject("CDO.Message")
    
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery("Select * from Win32_Service where DisplayName = '" & Service & "'")
    
    For Each objItem in colItems
    If objItem.status <>"OK" or objItem.state <>"Running" then
       objEmail.From = "Email Address"
       objEmail.To = "Email Address"
       objEmail.Subject = "Print Spooler Service" 
       objEmail.Textbody = objItem.DisplayName & " on: " & strComputer _
          '" State: " & objItem.State & VbCrLf
       objEmail.Configuration.Fields.Item _
          ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
       objEmail.Configuration.Fields.Item _
          ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
          "smtp" 
       objEmail.Configuration.Fields.Item _
          ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
       objEmail.Configuration.Fields.Update
       objEmail.Send
    End If
    Next
    Last edited by Rems; 13th August 2009, 10:08.

  • #2
    Re: Script to email printing status

    What is the issue -Sending email or -Checking the service ? please test both features separately in separates scripts.
    Can you provide more information about the OSs and the messaging infrastructure.


    What credentials are configured to execute the sheduled task?
    - It that account allowed to start the print spooler service (although this is not in the sample you showed).
    - It that account allowed to authenticate to the SMTP server?
    - It that account allowed to send-as "Email Address"?
    Does the mailbox for "Email Address" exist?

    TCP port 25 could be blokked on the computer running the script.

    Check if the server name "SMTP" is added in DNS as Alias for the mailserver.domail.local? Otherwise, try using the full dns name of the mail server in your script.

    You might want to review the part: "Sending a text email using authentication against a remote SMTP server" in this article http://www.paulsadowski.com/WSH/cdo.htm


    \Rems

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: Script to email printing status

      Doesn't IIS SMTP service on the server also need to be installed for this to work?

      Comment


      • #4
        Re: Script to email printing status

        Both issues not able to start services if they are stopped and not sending out emails.
        I am using administartor accout. I tried the script on windows server 2000, 2003 and XP pc.

        I did not shcedule it yet, I am just trying manually.

        TCP port 25 could be blokked on the computer running the script.
        Port 25 is opened and added to relay list on exhange server.

        Check if the server name "SMTP" is added in DNS as Alias for the mailserver.domail.local? Otherwise, try using the full dns name of the mail server in your script.
        I did use FQDN for SMTP address, I just did not paste in the thread.

        Comment


        • #5
          Re: Script to email printing status

          If you have IIS SMTP Service installed on the server you are sending from, you just need this:

          Code:
          strComputer = "."
          
          strService = "Print Spooler"
          Set objEmail = CreateObject("CDO.Message")
          
          Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
          Set colItems = objWMIService.ExecQuery("Select * from Win32_Service where DisplayName = '" & Service & "'")
          
          For Each objItem in colItems
          If objItem.status <>"OK" or objItem.state <>"Running" then
             With objEmail
                .From = "Email Address"
                .To = "Email Address"
                .Subject = "Print Spooler Service" 
                .Textbody = objItem.DisplayName & " on: " & strComputer _
                   '" State: " & objItem.State & VbCrLf
                .Send
             End With
          End If
          Next

          Comment


          • #6
            Re: Script to email printing status

            I am able to send out simple text email but the one with printspooler status in not working.

            Below script not working. I tried with print spooler off and on.
            strComputer = "."
            strService = "Print Spooler"
            Set objEmail = CreateObject("CDO.Message")
            Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
            Set colItems = objWMIService.ExecQuery("Select * from Win32_Service where DisplayName = '" & Service & "'")
            For Each objItem in colItems
            If objItem.status <>"OK" or objItem.state <>"Running" then
            objEmail.From = "[email protected]"
            objEmail.To = "[email protected]"
            objEmail.Subject = "Print Spooler Service"
            objEmail.Textbody = objItem.DisplayName & " on: " & strComputer _
            '" State: " & objItem.State & VbCrLf
            objEmail.Configuration.Fields.Item _
            ("http://schemas.microsoft.com/cdo/con...tion/sendusing") = 2
            objEmail.Configuration.Fields.Item _
            ("http://schemas.microsoft.com/cdo/con...ion/smtpserver") = _
            "smtp.test.com"
            objEmail.Configuration.Fields.Item _
            ("http://schemas.microsoft.com/cdo/con...smtpserverport") = 25
            objEmail.Configuration.Fields.Update
            objEmail.Send
            End If
            Next

            Comment


            • #7
              Re: Script to email printing status

              I got the belwo script to work. what changes I need to make to start the spooler services when it is stopped.
              strComputer = "."
              strService = "Print Spooler"
              Set objEmail = CreateObject("CDO.Message")
              Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
              Set colItems = objWMIService.ExecQuery("Select * from Win32_Service where DisplayName = '" & strService & "'")
              For Each objItem in colItems
              If objItem.status <>"OK" or objItem.state <>"Running" then
              objEmail.From = "email"
              objEmail.To = "email"
              objEmail.Subject = "Print Spooler Service"
              objEmail.Textbody = objItem.DisplayName & " on: " & strComputer & " State: " & objItem.State & VbCrLf
              objEmail.Configuration.Fields.Item _
              ("http://schemas.microsoft.com/cdo/con...tion/sendusing") = 2
              objEmail.Configuration.Fields.Item _
              ("http://schemas.microsoft.com/cdo/con...ion/smtpserver") = "smtp"
              objEmail.Configuration.Fields.Item _
              ("http://schemas.microsoft.com/cdo/con...smtpserverport") = 25
              objEmail.Configuration.Fields.Update
              objEmail.Send
              End If
              Next

              Comment


              • #8
                Re: Script to email printing status

                You had the wrong string name in the query:

                EDIT** Updated with starting the service if stopped (and making it look a little nicer )

                Code:
                strComputer = "."
                
                strService = "Print Spooler"
                
                Set objEmail = CreateObject("CDO.Message")
                Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
                Set colItems = objWMIService.ExecQuery("Select * from Win32_Service where DisplayName = '" strService & "'")
                
                For Each objItem in colItems
                If objItem.status <>"OK" or objItem.state <>"Running" then
                   With objEmail
                      .From = "Email Address"
                      .To = "Email Address"
                      .Subject = "Print Spooler Service" 
                      .Textbody = objItem.DisplayName & " on: " & strComputer '" State: " & objItem.State & VbCrLf
                         With .Configuration.Fields
                            .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
                            .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp" 
                            .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
                            .Update
                         End With
                      .Send
                   End With
                objItem.StartService()
                End If
                
                Next
                Last edited by ekrengel; 13th August 2009, 18:39.

                Comment


                • #9
                  Re: Script to email printing status

                  Thanks everyone script is now working fine.

                  Comment


                  • #10
                    Re: Script to email printing status

                    My suggestion is to check the service, if it fails restart it.
                    Pause 10 seconds and check again. If the service is still off send the email.

                    The only problem I have with your script now is you will not know for another 10 minutes if the service is still not working, and then did it fix it?

                    You can simply put the program into a function and call it. If it fails sleep and and call it again.. 2 strikes and an email is sent and you know if needs to be looked at right now!

                    Hobie

                    Comment

                    Working...
                    X