locked
VBS Script issue. RRS feed

  • General discussion

  • I am using below vbs script to check disk space health check. This is working fine for me, Only one modification I want to make is.. To only include servers which are having Critical and Warning reports. I don't want to populate healthy servers, as of now I am running this script for 500+ servers where 56 servers are only with CRITICAL and warnings. I want these servers only in HTML as report of 500+ is too much big.

    Thanks in advance.




    On Error Resume Next
    Const ForAppending = 8
    Const HARD_DISK = 3
    Const ForReading = 1
    set outFile = "RED Servers Disk Space Health Check_" & Date & ".html"
    
    'Declaring the variables
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set SrvList = objFSO.OpenTextFile("Server_List.txt", ForReading)
    Set ReportFile = objFSO.OpenTextFile ("RED Servers Disk Space Health Check_"+cstr(Month(now()))+"_"+cstr(day(now()))+"_"+cstr(year(now()))+".html", ForAppending, True)
    i = 0
    Set strFilePath = "\\myserver\c$\temp"
    
    'Initializing the HTML Tags for better formatting
    ReportFile.writeline("<html>")
    ReportFile.writeline("<head>")
    ReportFile.writeline("<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>")
    ReportFile.writeline("<title>" & "RED Servers Disk Space Report</title>")
    ReportFile.writeline("<style type='text/css'>")
    ReportFile.writeline("<!--")
    ReportFile.writeline("td {")
    ReportFile.writeline("font-family: Tahoma;")
    ReportFile.writeline("font-size: 11px;")
    ReportFile.writeline("border-top: 1px solid #999999;")
    ReportFile.writeline("border-right: 1px solid #999999;")
    ReportFile.writeline("border-bottom: 1px solid #999999;")
    ReportFile.writeline("border-left: 1px solid #999999;")
    ReportFile.writeline("padding-top: 0px;")
    ReportFile.writeline("padding-right: 0px;")
    ReportFile.writeline("padding-bottom: 0px;")
    ReportFile.writeline("padding-left: 0px;")
    ReportFile.writeline("}")
    ReportFile.writeline("body {")
    ReportFile.writeline("margin-left: 5px;")
    ReportFile.writeline("margin-top: 5px;")
    ReportFile.writeline("margin-right: 0px;")
    ReportFile.writeline("margin-bottom: 10px;")
    ReportFile.writeline("")
    ReportFile.writeline("table {")
    ReportFile.writeline("border: thin solid #000000;")
    ReportFile.writeline("}")
    ReportFile.writeline("-->")
    ReportFile.writeline("</style>")
    ReportFile.writeline("</head>")
    ReportFile.writeline("<body>")
    
    ReportFile.writeline("<table width='50%'>")
    ReportFile.writeline("<tr bgcolor='#CCCCCC'>")
    ReportFile.writeline("<td colspan='7' height='25' align='center'>")
    ReportFile.writeline("<font face='tahoma' color='#003399' size='2'><strong>RED Servers Report</strong></font>")
    ReportFile.writeline("</td>")
    ReportFile.writeline("</tr>")
    ReportFile.writeline("</table>")
    
    
    'Declaring the Server Name for report generation
    Do Until SrvList.AtEndOfStream
    	StrComputer = SrvList.Readline
    
    	ReportFile.writeline("<table width='50%'><tbody>")
    	ReportFile.writeline("<tr bgcolor='#CCCCCC'>")
    	ReportFile.writeline("<td width='50%' align='center' colSpan=12><font face='tahoma' color='#003399' size='2'><strong>" & StrComputer & "</strong></font></td>")
    	ReportFile.writeline("</tr>")
    
    
    	Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    	Set colDisks = objWMIService.ExecQuery("Select * from Win32_LogicalDisk Where DriveType = " & HARD_DISK & "")
    
    	ReportFile.writeline("<tr bgcolor=#CCCCCC>")
    		ReportFile.writeline("<td width='05%' align='center'>Drive / Mount</td>")
    		ReportFile.writeline("<td width='05%' align='center'>Total Capacity (in GB)</td>")
    		ReportFile.writeline("<td width='05%' align='center'>Used Capacity (in GB)</td>")
    		ReportFile.writeline("<td width='05%' align='center'>Free Space (in GB)</td>")
    		ReportFile.writeline("<td width='05%' align='center'>Freespace %</td>")
    		ReportFile.writeline("<td width='05%' align='center'>Last Reboot</td>")
    	ReportFile.writeline("</tr>")
    			
    		Dim i
    		i=0
    		'Starting the loop to gather values from all Hard Drives
    		For Each objDisk in colDisks
    
    			'Delcaring the Variables
    			TotSpace=Round(((objDisk.Size)/1073741824),2)
    			FrSpace=Round(objDisk.FreeSpace/1073741824,2)
    			FrPercent=Round((FrSpace / TotSpace)*100,0)
    			UsSpace=Round((TotSpace - FrSpace),2)
    			Drv=objDisk.DeviceID
    			VolName=objDisk.DeviceID
    
    				If FrPercent > 20 Then
    					ReportFile.Write "<tr><td align=center>" & Drv & "</td><td align=center>" & TotSpace & "</td><td align=center>" & UsSpace & "</td><td align=center>" & FrSpace & "</td><td BGCOLOR='#00FF00' align=center>" & FrPercent & "%" &"</td>"
    				ElseIf FrPercent < 10 Then
    					ReportFile.Write "<tr><td align=center>" & Drv & "</td><td align=center>" & TotSpace & "</td><td align=center>" & UsSpace & "</td><td align=center>" & FrSpace & "</td><td bgcolor='#FF0000' align=center>" & FrPercent & "%" &"</td>"
    				Else
    					ReportFile.Write "<tr><td align=center>" & Drv & "</td><td align=center>" & TotSpace & "</td><td align=center>" & UsSpace & "</td><td align=center>" & FrSpace & "</td><td bgcolor='#FBB917' align=center>" & FrPercent & "%" &"</td>"
    				End If
    
    			If i < 1 Then
    			    Set objWMIService = GetObject ("winmgmts:\\" & strComputer & "\root\cimv2")
    			    Set colOperatingSystems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
    			    
    			    For Each objOS in colOperatingSystems
    				dtmBootup = objOS.LastBootUpTime
        				dtmLastBootupTime = WMIDateStringToDate(dtmBootup)
    				'OutFile.WriteLine "Last Reboot: " & dtmLastBootupTime
      				'OutFile.WriteLine "System is online since " & dtmSystemUptime & " hours"
    			    Next
    
    			    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
    			    Set colCSItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem")
    			    
    
    			    If dtmSystemUptime > 24 Then
    					ReportFile.Write "<td align=center>" & dtmLastBootupTime
    			    ElseIf dtmSystemUptime < 12 Then
    					ReportFile.Write "<td align=center>" & dtmLastBootupTime
    			    Else
    					ReportFile.Write "<td align=center>" & dtmLastBootupTime
    			End If 
    			i = i + 1
    			End If
    					ReportFile.Write "</tr>"
    		Next
    
    	ReportFile.writeline("<tr>")
    	ReportFile.writeline("<td width='50%' colSpan=12>&nbsp;</td>")
    	ReportFile.writeline("</tr>")
    
    	ReportFile.writeline("</tbody></table>")
    Loop
    ReportFile.WriteLine "</body></html>"
    msgBox "Health Check Report is Completed"
    
    Function WMIDateStringToDate(dtmBootup)
        WMIDateStringToDate = CDate(Mid(dtmBootup, 5, 2) & "/" & _
             Mid(dtmBootup, 7, 2) & "/" & Left(dtmBootup, 4) _
             & " " & Mid (dtmBootup, 9, 2) & ":" & _
             Mid(dtmBootup, 11, 2) & ":" & Mid(dtmBootup, _
             13, 2))
    End Function


    • Edited by Jay Rana Monday, November 13, 2017 9:20 AM
    • Changed type Bill_Stewart Thursday, January 25, 2018 10:24 PM
    • Moved by Bill_Stewart Thursday, January 25, 2018 10:24 PM This is not "fix/debug/rewrite my script for me" forum
    Monday, November 13, 2017 6:50 AM

All replies

  • Can you post a link to the original script.  Where did you find it?


    \_(ツ)_/

    Monday, November 13, 2017 6:56 AM
  • Hi Jay,

    Please post your code and the modifications you have done to accomplish your work. 


    (66,65,83,65,84,73|%{[char]$_})-join''

    Monday, November 13, 2017 6:58 AM
  • I didn't downloaded it from any online source. This was already configured by previous admins so I don't know hat was original and if they made any changes. I only want to clean up the report so I don't get those servers in HTML report on which are healthy.

    I was only trying to set Set strFilePath = "\\myserver\c$\temp"

    which doesn't seems to be working as output is only coming in the directory where script is running from so just ignore it.

    • Edited by Jay Rana Monday, November 13, 2017 7:20 AM
    Monday, November 13, 2017 7:17 AM
  • Sounds like you need to start by learning how to write VBScript.  This forum is not for getting free script fixes.  It is for questions about scripting and discussions about scripting.

    Study VBScript and try to make the changes you want and post back with any specific questions.

    Of course you can actually do this with PowerShell in only  few lines of code.

    You can also look in the Gallery for the dozens of scripts that do what your script is trying to do.


    \_(ツ)_/

    Monday, November 13, 2017 7:35 AM
  • If someone has full knowledge of anything then why he/she would come over here on forums. I do some PowerShell codes, that is also working fine but due to dozen of network routes in infrastructure PowerShell isn't reporting few servers. This old vbs one is working fine so trying to make changes.

    Anyhow, I find original source code below and will try to fix this. Can someone move this script request page.

    *******DiskSpace.vbs** 
     
    On Error Resume Next 
    Const ForAppending = 8 
    Const HARD_DISK = 3 
    Const ForReading = 1 
     
    'Declaring the variables 
    Set objFSO = CreateObject("Scripting.FileSystemObject") 
    Set SrvList = objFSO.OpenTextFile("Server_List.txt", ForReading) 
    Set ReportFile = objFSO.OpenTextFile ("Diskspace_status.html", ForAppending, True) 
    i = 0 
     
    'Initializing the HTML Tags for better formatting 
    ReportFile.writeline("<html>") 
    ReportFile.writeline("<head>") 
    ReportFile.writeline("<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>") 
    ReportFile.writeline("<title>" & "Asset Management SQL Servers Disk Space Report</title>") 
    ReportFile.writeline("<style type='text/css'>") 
    ReportFile.writeline("<!--") 
    ReportFile.writeline("td {") 
    ReportFile.writeline("font-family: Tahoma;") 
    ReportFile.writeline("font-size: 11px;") 
    ReportFile.writeline("border-top: 1px solid #999999;") 
    ReportFile.writeline("border-right: 1px solid #999999;") 
    ReportFile.writeline("border-bottom: 1px solid #999999;") 
    ReportFile.writeline("border-left: 1px solid #999999;") 
    ReportFile.writeline("padding-top: 0px;") 
    ReportFile.writeline("padding-right: 0px;") 
    ReportFile.writeline("padding-bottom: 0px;") 
    ReportFile.writeline("padding-left: 0px;") 
    ReportFile.writeline("}") 
    ReportFile.writeline("body {") 
    ReportFile.writeline("margin-left: 5px;") 
    ReportFile.writeline("margin-top: 5px;") 
    ReportFile.writeline("margin-right: 0px;") 
    ReportFile.writeline("margin-bottom: 10px;") 
    ReportFile.writeline("") 
    ReportFile.writeline("table {") 
    ReportFile.writeline("border: thin solid #000000;") 
    ReportFile.writeline("}") 
    ReportFile.writeline("-->") 
    ReportFile.writeline("</style>") 
    ReportFile.writeline("</head>") 
    ReportFile.writeline("<body>") 
     
    ReportFile.writeline("<table width='50%'>") 
    ReportFile.writeline("<tr bgcolor='#CCCCCC'>") 
    ReportFile.writeline("<td colspan='7' height='25' align='center'>") 
    ReportFile.writeline("<font face='tahoma' color='#003399' size='2'><strong>Asset Management SQL Servers Disk Space Report</strong></font>") 
    ReportFile.writeline("</td>") 
    ReportFile.writeline("</tr>") 
    ReportFile.writeline("</table>") 
     
     
    'Declaring the Server Name for report generation 
    Do Until SrvList.AtEndOfStream 
        StrComputer = SrvList.Readline 
     
        ReportFile.writeline("<table width='50%'><tbody>") 
        ReportFile.writeline("<tr bgcolor='#CCCCCC'>") 
        ReportFile.writeline("<td width='50%' align='center' colSpan=6><font face='tahoma' color='#003399' size='2'><strong>" & StrComputer & "</strong></font></td>") 
        ReportFile.writeline("</tr>") 
     
     
        Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") 
        Set colDisks = objWMIService.ExecQuery("Select * from Win32_LogicalDisk Where DriveType = " & HARD_DISK & "") 
     
        ReportFile.writeline("<tr bgcolor=#CCCCCC>") 
            ReportFile.writeline("<td width='05%' align='center'>Drive / Mount</td>") 
            ReportFile.writeline("<td width='05%' align='center'>Total Capacity (in GB)</td>") 
            ReportFile.writeline("<td width='05%' align='center'>Used Capacity (in GB)</td>") 
            ReportFile.writeline("<td width='05%' align='center'>Free Space (in GB)</td>") 
            ReportFile.writeline("<td width='05%' align='center'>Freespace %</td>") 
        ReportFile.writeline("</tr>") 
     
            'Starting the loop to gather values from all Hard Drives 
            For Each objDisk in colDisks 
     
                'Delcaring the Variables 
     
                TotSpace=Round(((objDisk.Size)/1073741824),2) 
                FrSpace=Round(objDisk.FreeSpace/1073741824,2) 
                FrPercent=Round((FrSpace / TotSpace)*100,0) 
                UsSpace=Round((TotSpace - FrSpace),2) 
                Drv=objDisk.DeviceID 
                VolName=objDisk.DeviceID 
     
                'Lnt=Len(VolName) 
     
                'If  Len(VolName) =  3 then 
                    If FrPercent > 20 Then 
                        ReportFile.WriteLine "<tr><td align=center>" & Drv & "</td><td align=center>" & TotSpace & "</td><td align=center>" & UsSpace & "</td><td align=center>" & FrSpace & "</td><td BGCOLOR='#00FF00' align=center>" & FrPercent & "%" &"</td></tr>" 
                    ElseIf FrPercent < 10 Then 
                        ReportFile.WriteLine "<tr><td align=center>" & Drv & "</td><td align=center>" & TotSpace & "</td><td align=center>" & UsSpace & "</td><td align=center>" & FrSpace & "</td><td bgcolor='#FF0000' align=center>" & FrPercent & "%" &"</td></tr>" 
                    Else 
                        ReportFile.WriteLine "<tr><td align=center>" & Drv & "</td><td align=center>" & TotSpace & "</td><td align=center>" & UsSpace & "</td><td align=center>" & FrSpace & "</td><td bgcolor='#FBB917' align=center>" & FrPercent & "%" &"</td></tr>" 
                    End If 
                'Else 
                'End If 
            Next 
     
        ReportFile.writeline("<tr>") 
        ReportFile.writeline("<td width='50%' colSpan=6>&nbsp;</td>") 
        ReportFile.writeline("</tr>") 
     
        ReportFile.writeline("</tbody></table>") 
    Loop 
    ReportFile.WriteLine "</body></html>" 


    • Edited by Jay Rana Monday, November 13, 2017 9:21 AM
    Monday, November 13, 2017 8:47 AM
  • Can someone move this script request page.

    If you want to put a request on the script request page, you will need to create the request yourself (we can't "move" it for you). Also, please read the following:

    This forum is for scripting questions rather than script requests

    Please note the following bullet point from that post:

    • The individual that wrote this script left the company and now we need to make changes. Can someone do this for me?

    Your request is not within the scope of this forum.


    -- Bill Stewart [Bill_Stewart]

    Monday, November 13, 2017 3:35 PM
  • I can give you a hint as to how to easily do this>  Just change this set of lines to select the output you want:

                'If  Len(VolName) =  3 then 
                    If FrPercent > 20 Then 
                        ReportFile.WriteLine "<tr><td align=center>" & Drv & "</td><td align=center>" & TotSpace & "</td><td align=center>" & UsSpace & "</td><td align=center>" & FrSpace & "</td><td BGCOLOR='#00FF00' align=center>" & FrPercent & "%" &"</td></tr>" 
                    ElseIf FrPercent < 10 Then 
                        ReportFile.WriteLine "<tr><td align=center>" & Drv & "</td><td align=center>" & TotSpace & "</td><td align=center>" & UsSpace & "</td><td align=center>" & FrSpace & "</td><td bgcolor='#FF0000' align=center>" & FrPercent & "%" &"</td></tr>" 
                    Else 
                        ReportFile.WriteLine "<tr><td align=center>" & Drv & "</td><td align=center>" & TotSpace & "</td><td align=center>" & UsSpace & "</td><td align=center>" & FrSpace & "</td><td bgcolor='#FBB917' align=center>" & FrPercent & "%" &"</td></tr>" 
                    End If 
    

    Comment out the line you don't want to see.


    \_(ツ)_/

    Monday, November 13, 2017 4:49 PM
  • Yeah, that's got attention and I made a tweak with

                If FrPercent < 10 Then 
                                           ReportFile.WriteLine "<tr><td align=center>" & Drv & "</td><td align=center>" & TotSpace & "</td><td align=center>" & UsSpace & "</td><td align=center>" & FrSpace & "</td><td bgcolor='#FF0000' align=center>" & FrPercent & "%" &"</td></tr>" 
                                    ElseIf FrPercent < 20 And FrPercent > 10 Then
                                           ReportFile.WriteLine "<tr><td align=center>" & Drv & "</td><td align=center>" & TotSpace & "</td><td align=center>" & UsSpace & "</td><td align=center>" & FrSpace & "</td><td bgcolor='#FBB917' align=center>" & FrPercent & "%" &"</td></tr>" 
                                    Else
                                         Wscript.Echo "Server Disks are Healthy"
                                    End If

    This worked as now this is showing only drives with Critical and warning annotation. The issue here is before these lines we already have populated server names in report.

    'Declaring the Server Name for report generation
    Do Until SrvList.AtEndOfStream
    	StrComputer = SrvList.Readline
    
    So now all servers are displaying in report listed in Server_list.txt file, only change in report is that which servers are not under critical or warning are showing with blank rows.


    • Edited by Jay Rana Tuesday, November 14, 2017 5:17 AM
    Tuesday, November 14, 2017 5:16 AM
  • I understand, will take care for next time Bill.
    Tuesday, November 14, 2017 5:21 AM