Announcement

Collapse
No announcement yet.

Required VBscript for service status and Disk space

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

  • Required VBscript for service status and Disk space

    Hi All,

    I am looking for a script(vbscript n i have got no experience in vbscript ) that can do the following task.

    We have a set of servers for which we need to check the disk space and service status daily. Which takes almost 1 hour or more on daily basis. Doing so make me look fool. So i need a vbscript so that i can run it against a list of servers so to get the status.

    Here is how things are setup

    I need a script that can do the following.

    Read list of the server from a file (text file or any file that can be update)

    Then write to the file following data

    Name of the server
    write to the file the drive letter or name if free disk space is less than 10% on a drive
    Write to the file the service name if the status of the service is stopped and startup type is automatic.

    The data can be write in the block for each server or any formatting would do.

    If anyone out there can help me out then i will be very grateful to them.

    Thank you
    Last edited by v-2nas; 17th February 2009, 09:46.
    Thanks & Regards
    v-2nas

    MCTS 2008, MCTIP, MCSE 2003, MCSA+Messaging E2K3, MCP, E2K7
    Sr. Wintel Eng. (Investment Bank)
    Independent IT Consultant and Architect
    Blog: http://www.exchadtech.blogspot.com

    Show your appreciation for my help by giving reputation points

  • #2
    Re: Required VBscript for service status and Disk space

    Originally posted by v-2nas View Post
    i need a vbscript so that i can run it against a list of servers so to get the status.
    WMI and VBScript can solve your problem. Take a look at this MSDN article, the second to last example queries multiple computers for PnP devices. It should be easy to tune the query to get disk space data and service statuses too.

    http://msdn.microsoft.com/en-us/libr...90(VS.85).aspx

    After your script works, schedule a task to run it once a day or the like. Remember to create a service account that has appropriate rights to the target systems so that the task could be run.

    -vP

    Comment


    • #3
      Re: Required VBscript for service status and Disk space

      Thx vonPryz i will look into the link and then find it out.
      Thanks & Regards
      v-2nas

      MCTS 2008, MCTIP, MCSE 2003, MCSA+Messaging E2K3, MCP, E2K7
      Sr. Wintel Eng. (Investment Bank)
      Independent IT Consultant and Architect
      Blog: http://www.exchadtech.blogspot.com

      Show your appreciation for my help by giving reputation points

      Comment


      • #4
        Re: Required VBscript for service status and Disk space

        Checked the link. But i not familiar with vbscript so having hardtime putting all things together.
        Thanks & Regards
        v-2nas

        MCTS 2008, MCTIP, MCSE 2003, MCSA+Messaging E2K3, MCP, E2K7
        Sr. Wintel Eng. (Investment Bank)
        Independent IT Consultant and Architect
        Blog: http://www.exchadtech.blogspot.com

        Show your appreciation for my help by giving reputation points

        Comment


        • #5
          Re: Required VBscript for service status and Disk space

          Originally posted by v-2nas View Post
          i not familiar with vbscript
          There are plenty of resources in the web for learning VBScript. MSDN is good: http://msdn.microsoft.com/en-us/library/ms950396.aspx and Rob van der Voude is one of my favourite sources too: http://www.robvanderwoude.com/index.php

          Anyway, here is a simple sample that queries a few computers and displays the free disk space on C: drive for each computer. Its just a bit modified from the MSDN example mentioned earlier on. No error handling is present, so add some before production environment usage.

          Code:
          option explicit
          const MEGABYTE = 1048576
          dim arrComputers
          dim colItems
          dim colDisks
          dim objDisk
          dim strComputers
          dim strComputer
          dim objWmiService
           
          arrComputers = Array("computer1","computer3","computer3")
           
          For Each strComputer In arrComputers
          WScript.Echo
          WScript.Echo "Computer: "& strComputer
          Set objWMIService = GetObject("winmgmts:\\" & strComputer& "\root\CIMV2") 
          Set colDisks = objWMIService.ExecQuery ("Select * "_
          & "from Win32_LogicalDisk Where DeviceID = 'C:'")
          For Each objDisk in colDisks
          Wscript.Echo round((objDisk.FreeSpace/MEGABYTE), 1) & " MB"
          Next
          Next
          Service status query would work the same way. Just select status from Win32_Service with appropriate parameters.

          Here is an example about querying service states:
          Code:
          option explicit
          Dim colServices
          Dim Service
          dim objWMIService
          dim strComputer
          strComputer ="."
          Set objWMIService = GetObject("winmgmts:\\" & strComputer& "\root\CIMV2") 
          Set colServices =objWMIService.ExecQuery("select * from Win32_Service")
          For each Service in colServices
          WScript.Echo Service.Name & " is " & Service.State
          Next
          Writing the results into a file and combining the scripts is left as an exercise to the reader.

          -vP

          Comment


          • #6
            Re: Required VBscript for service status and Disk space

            Hi VonPryz

            This is what i have done
            Code:
            Set fso = CreateObject("Scripting.FileSystemObject")
            Set objInputFile = fso.OpenTextFile("COMPUTERS.txt", 1, True)
            Set objOutputFile = fso.OpenTextFile("Results.txt", 2, True)
            
            Do While objInputFile.AtEndOfLine <> True
               strComputer = objInputFile.ReadLine
                
                ObjOutputFile.WriteLine("Computer:"& strComputer)
                
                Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
                Set colItems = objWMIService.ExecQuery( _
                "SELECT * FROM Win32_Service WHERE StartMode = 'Auto' AND State='Stopped'") 
                
                    For Each objItem in colItems 
                        ObjOutputFile.WriteLine("-----------------------------------")
                        ObjOutputFile.WriteLine("DisplayName: " & objItem.DisplayName)
                     ObjOutputFile.WriteLine("-----------------------------------")
                     ObjOutputFile.WriteLine("StartMode: " & objItem.StartMode)
                     ObjOutputFile.WriteLine("State: " & objItem.State)
                    Next
            
            Loop
            Now it reads the list of computer which are in the same domain and i am domain admin

            So what my understanding is that when i run the script it would be running under domain admin credentials

            however this is wat i get when it tries to query the remote system listed in computers.txt file

            D:\DoNotDelete\VbScript\servicestatus-1.vbs(10, 2) Microsoft VBScript runtime error: Permission denied: 'GetObject'

            Now when i connect remotely to services console using mmc it just works fine.

            any suggestion to fix this thing and make it work
            Last edited by Rems; 18th February 2009, 17:59. Reason: added [code] tags
            Thanks & Regards
            v-2nas

            MCTS 2008, MCTIP, MCSE 2003, MCSA+Messaging E2K3, MCP, E2K7
            Sr. Wintel Eng. (Investment Bank)
            Independent IT Consultant and Architect
            Blog: http://www.exchadtech.blogspot.com

            Show your appreciation for my help by giving reputation points

            Comment


            • #7
              Re: Required VBscript for service status and Disk space

              Originally posted by v-2nas View Post
              D:\DoNotDelete\VbScript\servicestatus-1.vbs(10, 2) Microsoft VBScript runtime error: Permission denied: 'GetObject'

              Now when i connect remotely to services console using mmc it just works fine.
              You've made good progress this far with reading target list from a file and writing output to a file too.

              Are you a Domain Admin? Your account should belong into local admins group for each remote system in order to make the WMI call work, so check that you can execute local WMI queries.

              You might need to specify impersonation level so that the script can be run with proper credentials.

              Remote connection requirements are explained here: http://msdn.microsoft.com/en-us/library/aa389290(VS.85).aspx

              Impersonation is explained here: http://msdn.microsoft.com/en-us/libr...tionlevel.aspx

              Can't test it right now, but impersonation is used like this:

              Code:
              Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\"_
              & strComputer & "\root\CIMV2")
              -vP

              Comment


              • #8
                Re: Required VBscript for service status and Disk space

                that is what I use (if I good remmeber I've got it partly from microsoft site) to check disk space and temperature of the server

                Code:
                'what doing this cript
                '1 check a disk space and make raport
                '2 check the cpu temperature and make raport
                '3 if we set error valiue then raport will be sent if valiue be same as variables
                '  in raport
                
                
                
                Const Unknown = 0
                Const Removable = 1
                Const Fixed = 2
                Const Remote = 3
                Const CDROM = 4
                Const RAMDisk = 5
                
                
                
                
                
                ' get current computer name (from system environment variables)
                Function GetCurrentComputerName
                set oWsh = WScript.CreateObject("WScript.Shell")
                set oWshSysEnv = oWsh.Environment("PROCESS")
                GetCurrentComputerName = oWshSysEnv("COMPUTERNAME")
                End Function
                
                
                ' Main program code
                
                
                set oFs = WScript.CreateObject("Scripting.FileSystemObject")
                set oDrives = oFs.Drives   
                strComputerName = GetCurrentComputerName ' take computer name only ones
                
                
                
                
                
                
                Set objWMIService = GetObject( "winmgmts://./root/cimv2" )
                ' Set colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk where MediaType=12",,48 )
                Set aaa = objWMIService.ExecQuery("Select * from Win32_LogicalDisk where DriveType=3",,48 )
                
                Dim i
                For Each oDrive in aaa
                     i = Int( 0.5 + ( 100 * oDrive.FreeSpace / oDrive.Size) ) 
                      If i < 5 Then send 
                     procent = procent & oDrive.Name & vbTab & _
                         CStr( i ) & " %"& vbTab & vbCrLf
                         
                Next
                
                
                
                function send
                Dim a,c,d
                
                
                
                
                strComputerName = GetCurrentComputerName ' take computer name only ones
                
                'Set objWMIService = GetObject("winmgmts:\\" & strComputerName & "\root\WMI") 
                'Set colItems = objWMIService.ExecQuery( _ 
                '"SELECT * FROM MSAcpi_ThermalZoneTemperature",,48) 
                'For Each objItem in colItems 
                'temp = "-----------------------------------" &  vbCrLf & "CPU Temperature" & _
                'vbCrLf & "-----------------------------------" & vbCrLf & "CurrentTemperature: " & (objItem.CurrentTemperature / 100) & " C"
                'next
                
                Set objWMIService = GetObject("winmgmts:" _
                    & "{impersonationLevel=impersonate}!\\" & strComputerName & "\root\cimv2")
                Set colDisks = objWMIService.ExecQuery _
                    ("Select * from Win32_LogicalDisk Where DriveType = 3")
                For Each objDisk in colDisks
                    intFreeSpace = objDisk.FreeSpace
                    intTotalSpace = objDisk.Size
                   pctFreeSpace =  ( Int( 0.5 + ( 100 * intFreeSpace / intTotalSpace ) ) )
                   intFreeSpace = ( Int( 0.5 + ( intFreeSpace / 1073741824 ) ) )
                  
                  
                z = z & strComputerName &  " | " & " " & objDisk.DeviceID &   " | " & " " & "Free space: " & pctFreeSpace &   "%" & " | " & " " & intFreeSpace &  " " & "GB" & vbCrLf & "------------------------------------------"  & vbCrLf
                  
                
                Wscript.Echo z
                     
                  Next
                  
                  
                Set objMessage = CreateObject("CDO.Message") 
                objMessage.Subject = "Warning not enough disk space " & strComputerName
                'objMessage.Subject = "Warning not enought disk space" strComputerName
                objMessage.From = "[email protected]" 
                objMessage.To = "[email protected]"      
                
                 objMessage.TextBody = strComputerName & " have less than 10% free hard drive space"  & vbCrLf & vbCrLf & z & vbCrLf & temp
                
                
                'S omething from microsoft config to SMTP remote server
                ' Change IP address and that is it
                
                objMessage.Configuration.Fields.Item _
                ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
                
                'Name or IP of Remote SMTP Server
                objMessage.Configuration.Fields.Item _
                ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "your mail server ip or domain"
                
                'Server port (typically 25)
                objMessage.Configuration.Fields.Item _
                ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 
                
                objMessage.Configuration.Fields.Update
                
                objMessage.Send 
                
                end function
                
                Wscript.Quit
                Just change your email address and mail server, save file as somethig.vbs place on each server and set up schedule to run it.

                And that what you get on your email each time when is not enought X space on disk
                here should be screenshot - don't know why it is not showing it
                Last edited by ryszon; 19th February 2009, 13:56. Reason: change link to the screenshot

                Comment

                Working...
                X