none
VB Script to get Disk usage and computer description. RRS feed

  • Question

  • I have script to get disk information and is working fine. But I have modified to get computer information and it is working as well. however my end results are redundant because of FOR LOOP. Also I have to run this scrip twice as we have two domain. Can someone help me to get rid of redundant information and how can I run script once to get information from both domain?

    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("C:\scripts\Disk_Usage\Server_List.txt", ForReading) 
    Dim strBadDisks
    
    Set WriteFile = objFSO.OpenTextFile("C:\scripts\Disk_Usage\List.txt", ForAppending, True)
    
    'Declaring the Server Name for report generation 
    Do Until SrvList.AtEndOfStream 
        StrComputer = SrvList.Readline 
    
        Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
           ExecQuery("select * from Win32_PingStatus where address = '"_
           & strComputer & "'")
    
        For Each objStatus in objPing
           If not IsNull(objStatus.StatusCode) Then
              If objStatus.StatusCode = 0 Then
     
                 WriteFile.writeline() 
     
                 On Error Resume Next
                 Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") 
                 Set colDisks = objWMIService.ExecQuery("Select * from Win32_LogicalDisk Where DriveType = " & HARD_DISK & "") 
                 Set colItems = objWMIService.ExecQuery("Select Description from Win32_OperatingSystem",,48)
                 If Err.Number = 0 Then
                    WriteFile.writeline() 
     
    				For Each objItem in colItems
    					'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 
    						If FrPercent < 30 and Drv <> "C:" and Drv <> "Z:"Then 
    							 'WriteFile.WriteLine Drv & "," &TotSpace &"," &UsSpace &"," &FrSpace &  FrPercent & "%" 
    							 strBadDisks = strBadDisks & strComputer & "- Description: " & objItem.Description &"--> " & Drv & " Drive is @ " & FrPercent & "%" & vbcrlf 
    							 WriteFile.WriteLine (strBadDisks)                    
    						End If                   
    					Next 
    				Next
                     
                 End If
              End If
            End If   'ERROR CHECK      
          Next     
    Loop 
    
    WriteFile.Close


    Output has lots of blank space because of list contains servers belongs to other domain. I would appreciate if someone help me to fix the issue.

    Thank you very much,

    Best Regards,
    • Moved by Bill_Stewart Wednesday, September 4, 2019 6:55 PM This is not "fix/debug/rewrite script for me" forum
    Tuesday, March 19, 2019 1:13 PM

All replies

  • Put the loop in a function and call it once for each domain.

    You should not be using VBScript.  It is obsolete.  This can all be done in PowerShell much more easily.  It would also be easier for you to learn Powerhell as VBScript is more difficult and there is almost no good documentation left on the net.


    \_(ツ)_/

    Wednesday, March 20, 2019 12:08 AM