+ Post New Thread
Results 1 to 15 of 15
Scripts Thread, help with vbs script in Coding and Web Development; ive pinched/edited a script for finding hdd size of a pc but it does all drives in a system so ...
  1. #1


    Join Date
    Mar 2009
    Location
    Leeds
    Posts
    6,812
    Thank Post
    231
    Thanked 886 Times in 762 Posts
    Rep Power
    301

    help with vbs script

    ive pinched/edited a script for finding hdd size of a pc but it does all drives in a system so on my laptop does both hard drives, and dvd drives. Can anyone see a way of limiting it to just c drive

    Code:
    option explicit
    
    dim strcomputer, objWMIService, objDisk, colDisks
    
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" _
        & strComputer & "\root\cimv2")
    Set colDisks = objWMIService.ExecQuery _
        ("Select * from Win32_LogicalDisk")
    For Each objDisk in colDisks
        Wscript.Echo "DeviceID: " & objDisk.DeviceID       
        Wscript.Echo "Disk Size: " & Int(objDisk.Size/1073741824) & " GB"
    Next

  2. #2

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,897
    Thank Post
    3,420
    Thanked 1,081 Times in 997 Posts
    Rep Power
    369
    Code:
    option explicit
    
    dim strcomputer, objWMIService, objDisk, colDisks
    
    
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" _
        & strComputer & "\root\cimv2")
    Set colDisks = objWMIService.ExecQuery _
        ("Select * from Win32_LogicalDisk")
    For Each objDisk in colDisks
    If objDisk.DeviceID = "C:" Then
        Wscript.Echo "DeviceID: " & objDisk.DeviceID       
        Wscript.Echo "Disk Size: " & Int(objDisk.Size/1073741824) & " GB"
    End If
    Next
    Just an if statement inside of the for next loop to check if the device id is equivelant to "C:" and if so to execute the code.

  3. #3

    Join Date
    Oct 2011
    Location
    United Kingdom
    Posts
    14
    Thank Post
    0
    Thanked 2 Times in 2 Posts
    Rep Power
    7
    Try changing the line which has:
    Code:
    SELECT * FROM Win32_LogicalDisk
    to:
    Code:
    SELECT * FROM Win32_LogicalDisk WHERE Name = 'C:'
    (haven't tried this, just a guess)
    Last edited by anthonyd; 24th February 2012 at 02:31 PM. Reason: Changed " to ' around C:

  4. Thanks to anthonyd from:

    sted (24th February 2012)

  5. #4

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,897
    Thank Post
    3,420
    Thanked 1,081 Times in 997 Posts
    Rep Power
    369
    Quote Originally Posted by anthonyd View Post
    Try changing the line which has:
    Code:
    SELECT * FROM Win32_LogicalDisk
    to:
    Code:
    SELECT * FROM Win32_LogicalDisk WHERE Name = "C:"
    (haven't tried this, just a guess)
    That's another way of doing it

  6. Thanks to mac_shinobi from:

    sted (24th February 2012)

  7. #5


    Join Date
    Feb 2007
    Location
    51.403651, -0.515458
    Posts
    9,389
    Thank Post
    241
    Thanked 2,817 Times in 2,078 Posts
    Rep Power
    813
    Quote Originally Posted by anthonyd View Post
    Code:
    SELECT * FROM Win32_LogicalDisk WHERE Name = 'C:'
    Local HDDs have drivetype of 3, so you could do this...

    Code:
    SELECT * from Win32_LogicalDisk WHERE DriveType = 3
    Last edited by Arthur; 24th February 2012 at 02:38 PM.

  8. Thanks to Arthur from:

    sted (24th February 2012)

  9. #6


    Join Date
    Mar 2009
    Location
    Leeds
    Posts
    6,812
    Thank Post
    231
    Thanked 886 Times in 762 Posts
    Rep Power
    301
    picked the top one as its the top one works thanks

  10. Thanks to sted from:

    mac_shinobi (24th February 2012)

  11. #7

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,897
    Thank Post
    3,420
    Thanked 1,081 Times in 997 Posts
    Rep Power
    369
    not related to your script entirely but just as a side note ( sorry if I am hi jacking this ) but @Steve21 or @Arthur

    Is there a more accurate way to work out the hard drive size / capacity then doing this :

    Code:
    Int(objDisk.Size/1073741824) & " GB"


    Basically I did a hta and doing it this way is showing the wrong hard drive capacities from remote computers.


    Code:
    <html>
    <head> <title>Network Information</title> <hta:application applicationname="Network Information" border="dialog" borderstyle="normal" caption="Network Information" contextmenu="yes" icon="globe.ico" maximizebutton="no" minimizebutton="yes" navigable="yes" scroll="no" selection="yes" showintaskbar="yes" singleinstance="yes" sysmenu="yes" version="1.0" windowstate="normal" > <style type="text/css"> a:link { color:#ffffff font-size:10px; font-family:"Times New Roman", Times, serif; text-decoration:none; font-style: normal; font-variant: normal; } a:visited { color:#ffffff font-size:10px; font-family:"Times New Roman", Times, serif; text-decoration:none; font-style: normal; font-variant: normal; } a:hover { color:#ffffff font-size:10px; font-family:"Times New Roman", Times, serif; text-decoration:underline; font-style: normal; font-variant: normal; } td { font-family: "Times New Roman", Times, serif; font-size: 18px; font-style: normal; font-weight: normal; font-variant: normal; color: #ffffff vertical-align: left; } .status { text-align:center; } </style> <script language="vbscript"> Sub Window_onLoad window.resizeTo 700,500 End Sub sub user() Dim strUser, x Set wshNetwork = CreateObject( "WScript.Network" ) strComputer = Document.getElementById("computer").value Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator") Set objSWbemServices = objSWbemLocator.ConnectServer(strComputer, "root\cimv2", Document.getElementById("username").value,Document.getElementById("password").value) objSWbemServices.Security_.ImpersonationLevel = 3 Set colOperatingSystems= objSWbemServices.ExecQuery("Select * from Win32_OperatingSystem") For Each objOperatingSystem in colOperatingSystems OS.innerHTML = objOperatingSystem.Caption & " " & objOperatingSystem.Version Next Set colSerials= objSWbemServices.ExecQuery("Select * from Win32_BIOS",,48) For Each objSerial in colSerials serial.innerHTML = "Serial Number : " & objSerial.serialnumber Next Set colDisks= objSWbemServices.ExecQuery("Select * from Win32_LogicalDisk") For Each objDisk in colDisks If objDisk.DriveType = "3" Then hdd.innerHTML = "Hard Drive " & Int(objDisk.Size /1073741824) & " GB" End If Next Set colProcessors= objSWbemServices.ExecQuery("Select * from Win32_Processor") For Each objProcessor in colProcessors cpu.innerHTML = "Processor : " & objProcessor.Name Next Dim totMem Dim strModel 'Get currently logged on user's username Set colComputer = objSWbemServices.ExecQuery("Select * from Win32_ComputerSystem") For Each objComputer in colComputer strUser = "Username : " & objComputer.UserName If IsNull(objComputer.UserName) Then 'If no one is logged on strLoggedOn = "No" Else strLoggedOn = "Yes" End If totMem = "RAM : " & int((objComputer.TotalPhysicalMemory) /1048576)+1 strModel = "Model : " & objComputer.Model Next Model.innerHTML = strModel If strLoggedOn = "No" Then msgbox "computer is free" Else 'msgbox strUser 'x = split(struser,"\") info.innerHTML = strUser End If 'Get currently logged on user's username ram.innerHTML = totMem & " Mb" end sub Sub Clear() Model.innerHTML = "" OS.innerHTML = "" info.innerHTML = "" cpu.innerHTML = "" ram.innerHTML = "" hdd.innerHTML = "" serial.innerHTML = "" end sub </script> </head> <body> <div align="center"> <h1>Remote Computer</h1> Computer : <input type="text" name="computer" size="30"><P> Username : <input type="text" name="username" value="l1.shane.russell" size="30"><P> Password : <input type="password" name="password" size="30"> <p> <input id=runbutton type="button" value="Get Computer Info" onClick="user()"> <input id=runbutton type="button" value="Clear Info" onClick="Clear()"> <p> <div id="Model" style="font-size:150%"></div> <div id="OS" style="font-size:150%"></div> <div id="info" style="font-size:150%"></div> <div id="cpu" style="font-size:150%"></div> <div id="ram" style="font-size:150%"></div> <div id="hdd" style="font-size:150%"></div> <div id="serial" style="font-size:150%"></div> <p> </body>
    </html>


  12. #8


    Join Date
    Feb 2007
    Location
    51.403651, -0.515458
    Posts
    9,389
    Thank Post
    241
    Thanked 2,817 Times in 2,078 Posts
    Rep Power
    813
    As in show more decimal places?

    Code:
    Set colDisks= objSWbemServices.ExecQuery("Select * from Win32_LogicalDisk")
    For Each objDisk in colDisks
    If objDisk.DriveType = "3" Then
    hdd.innerHTML =  "Hard Drive " &  Round((objDisk.size/1024^3),2) & " GB"
    End If
    Next
    Last edited by Arthur; 24th February 2012 at 05:02 PM.

  13. #9

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,897
    Thank Post
    3,420
    Thanked 1,081 Times in 997 Posts
    Rep Power
    369
    Quote Originally Posted by Arthur View Post
    As in show more decimal places?

    Code:
    Set colDisks= objSWbemServices.ExecQuery("Select * from Win32_LogicalDisk")
    For Each objDisk in colDisks
    If objDisk.DriveType = "3" Then
    hdd.innerHTML =  "Hard Drive " &  Round((objDisk.size/1073741824),2) & " GB"
    End If
    Next
    Just trying to connect to a local machine at the minute and can show you in a second.

    sods law, tried it and it returns 148 Gb for a 160 Gb HDD which afaik is correct due to the way you calculate what is being used by the FS , system etc

    Had some occasions where I connect to a remote machine and the hard drive is an 80Gb yet it returns some odd values ie 69 Gb when it is really 80 Gb

    Also using WMI to get the cpu isn't always accurate so wondering if remotely querying the machines registry to establish what cpu it has would be better
    Last edited by mac_shinobi; 24th February 2012 at 04:58 PM.

  14. #10


    Join Date
    Mar 2009
    Location
    Leeds
    Posts
    6,812
    Thank Post
    231
    Thanked 886 Times in 762 Posts
    Rep Power
    301
    cpu query dosent seem too bad on the odd pc ive tried

    if it helps this is the script that the top fragment is for (basically takes a quick inventory of a pc and dumps it into active directory in the computers description field) (again granted its probably not the best bit of code ever)

    Code:
    Option Explicit
    On Error Resume Next
    
    Dim objRootDSE, objNetwork, objWMIService, objComputer
    Dim strComputer, strMake, strModel, strSerialNumber, strMacAddresses, strMemory, intMemory
    Dim colBIOS, objBIOS
    Dim colNetworkAdapterConfiguration, objNetworkAdapterConfiguration
    Dim colComputerSystem, objComputerSystem
    Dim colPhysicalMemory, objPhysicalMemory
    Dim adoConnection, adoRecordset
    dim mydatestring
    dim colOperatingSystems,objOperatingSystem,osver
    dim oReg, lastuser, snode, spath, svaluename, svalue
    dim colcpu, objItem, cpuspec
    dim objDisk, colDisks , cspace
    
    strComputer = "."
    strMemory = ""
    strMake = ""
    strModel = ""
    strSerialNumber = ""
    strMacAddresses = ""
    myDateString = Date()
    
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" _
        & strComputer & "\root\cimv2")
    Set colDisks = objWMIService.ExecQuery _
        ("Select * from Win32_LogicalDisk")
    For Each objDisk in colDisks
    	If objDisk.DeviceID = "C:" Then
    		'Wscript.Echo "DeviceID: " & objDisk.DeviceID       
    		cspace = int(objDisk.Size/1073741824) & " GB"
    	end if
    Next
    
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colcpu = objWMIService.ExecQuery("Select * from Win32_Processor")
    For Each objItem in colcpu
        cpuspec = objItem.Name
    Next
    
    ' use "." for local computer
    sNode = "."
    
    Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
    
    On Error Resume Next
    Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
    & sNode & "/root/default:StdRegProv")
    
    If Err.Number <> 0 Then
    On Error Goto 0
    WScript.Echo "Could not connect to computer " & sNode
    Else
    On Error Goto 0
    sPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI"
    sValueName = "LastLoggedonUser"
    
    If oReg.GetStringValue(HKLM, sPath, sValueName, sValue) = 0 Then
    lastuser = sValue
    Else
    lastuser = "unknown" & sNode
    End If
    End If
    
    
    Set objRootDSE = GetObject("LDAP://RootDSE")
    Set objNetwork = WScript.CreateObject("WScript.Network")
    Set objWMIService = GetObject("Winmgmts:\\" & strComputer & "\root\cimv2") 
    
    Set colComputerSystem = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem") 
    Set colOperatingSystems = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem")
    For Each objOperatingSystem in colOperatingSystems
        osver = objOperatingSystem.Caption & " sp " & objOperatingSystem.ServicePackMajorVersion'& "  " & objOperatingSystem.Version
    Next
    
    If Not colComputerSystem Is Nothing Then
    	For Each objComputerSystem In colComputerSystem 
    		strMake = objComputerSystem.Manufacturer 
    		strModel = objComputerSystem.Model 
    	Next
    End If
    
    Set colBIOS = objWMIService.ExecQuery("Select * From Win32_BIOS")
    
    If Not colBIOS Is Nothing Then
    	For Each objBIOS in colBIOS
    		strSerialNumber = objBIOS.SerialNumber
    	Next
    End If
    
    Set colNetworkAdapterConfiguration = objWMIService.ExecQuery("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
    
    strMacAddresses = ""
    
    If Not colNetworkAdapterConfiguration Is Nothing Then
    	For Each objNetworkAdapterConfiguration in colNetworkAdapterConfiguration
    		If strMacAddresses <> "" Then
    			strMacAddresses = strMacAddresses & ", "
    		End If
    		strMacAddresses = strMacAddresses & objNetworkAdapterConfiguration.MACAddress
    	Next
    End If
    
    Set colPhysicalMemory = objWMIService.ExecQuery("Select * From Win32_PhysicalMemory")
    
    If Not colPhysicalMemory Is Nothing Then
    	intMemory = 0
    	For Each objPhysicalMemory In colPhysicalMemory
    		intMemory = intMemory + Int(objPhysicalMemory.Capacity)
    	Next
    	strMemory = (intMemory / 1024 / 1024) & " MB"
    End If
    
    Set adoConnection = CreateObject("ADODB.Connection")
    adoConnection.Provider = "ADsDSOObject"
    adoConnection.Open "Active Directory Provider"
    
    If Err.Number <> 0 Then
    	MsgBox "Connect Error: " & Err.Description
    	WScript.Quit
    End If
    
    Set adoRecordset = adoConnection.Execute("<LDAP://" & objRootDSE.Get("defaultNamingContext") & ">;(&(objectCategory=Computer)(name=" & objNetwork.Computername & "));adspath;subtree")
    
    If Err.Number <> 0 Then
    	MsgBox "Query Error: " & Err.Description
    	WScript.Quit
    End If
    
    If Not adoRecordset.EOF Then
    	Set objComputer = GetObject(adoRecordset.Fields(0).Value)
    	objComputer.Put "description", "Make, " & strmake & " ,Model, " &strModel & ",cpu," & cpuspec &  ",Ram, " & strMemory & ",hdd size," & cspace & " ,Serial No, " & strSerialNumber & " ,MAC Addresses, " & strMACAddresses & "," & osver & ", Last booted, " & mydatestring & ", lastuser, " &lastuser
    	objComputer.SetInfo
    End If
    
    If Err.Number <> 0 Then
    	MsgBox "Write Error: " & Err.Description
    	WScript.Quit
    End If

  15. Thanks to sted from:

    mac_shinobi (24th February 2012)

  16. #11

    Join Date
    Jan 2006
    Location
    Surburbia
    Posts
    2,178
    Thank Post
    74
    Thanked 307 Times in 243 Posts
    Rep Power
    115
    Forget whether this is just XP: WMI CPU queries can mess up on some multi-core systems e.g. for a dual you sometimes see one that's OK and the other will be a Pentium III or something, but there is a KB with a patch. Also don't recall why I did this, it might have been the previous issue, but in something I wrote years and years ago I used WMI for getting nearly all the inventory data I wanted except for the CPU name which I got from the entry for the first logical processor here:

    HKLM\HARDWARE\DESCRIPTION\System\CentralProcessor\ 0\ProcessorNameString

  17. Thanks to PiqueABoo from:

    mac_shinobi (24th February 2012)

  18. #12

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,897
    Thank Post
    3,420
    Thanked 1,081 Times in 997 Posts
    Rep Power
    369
    Quote Originally Posted by PiqueABoo View Post
    Forget whether this is just XP: WMI CPU queries can mess up on some multi-core systems e.g. for a dual you sometimes see one that's OK and the other will be a Pentium III or something, but there is a KB with a patch. Also don't recall why I did this, it might have been the previous issue, but in something I wrote years and years ago I used WMI for getting nearly all the inventory data I wanted except for the CPU name which I got from the entry for the first logical processor here:

    HKLM\HARDWARE\DESCRIPTION\System\CentralProcessor\ 0\ProcessorNameString
    That is what is happening to me with ref to both the cpu ( if its multi core ie Intel Core 2 Duo or otherwise and it shows up as a Pentium III or Intel Pentium Xeon when I know it is not that CPU and the same with sometimes it mis calculates the hard drive capacity so wondering if there is a registry key somewhere that just gives me the over all hard drive capacity regardless of how the hdd is partitioned etc

  19. #13

    Join Date
    Jan 2006
    Location
    Surburbia
    Posts
    2,178
    Thank Post
    74
    Thanked 307 Times in 243 Posts
    Rep Power
    115
    Are you sure this isn't just the standard drive capacity thing i.e. to a drive vendor a GB is 1,000,000,000 bytes, NOT 1,073,741,824 bytes?

    Oh hang on.. all the above examples are using logical disks. To get physical drive sizes: "Select * from Win32_DiskDrive"
    Last edited by PiqueABoo; 24th February 2012 at 08:34 PM.

  20. Thanks to PiqueABoo from:

    mac_shinobi (24th February 2012)

  21. #14

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,897
    Thank Post
    3,420
    Thanked 1,081 Times in 997 Posts
    Rep Power
    369
    Quote Originally Posted by PiqueABoo View Post
    Are you sure this isn't just the standard drive capacity thing i.e. to a drive vendor a GB is 1,000,000,000 bytes, NOT 1,073,741,824 bytes?

    Oh hang on.. all the above examples are using logical disks. To get physical drive sizes: "Select * from Win32_DiskDrive"
    What about the RAM, as the RAM doesn't always show up correctly either, ie instead of showing as 1024 Mb it is showing as 1015 or other values ( or will the shared memory effect this )

  22. #15

    Join Date
    Jan 2006
    Location
    Surburbia
    Posts
    2,178
    Thank Post
    74
    Thanked 307 Times in 243 Posts
    Rep Power
    115
    (Yes)
    Win32_PhysicalMemory::Capacity tells you about the size of each memory module, so you loop through them all and add it up. Or perhaps report them independently if you want to know what is in there without taking the lid off and looking.

  23. Thanks to PiqueABoo from:

    mac_shinobi (25th February 2012)

SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 5
    Last Post: 1st July 2014, 08:34 AM
  2. Replies: 13
    Last Post: 22nd April 2011, 03:50 PM
  3. Disable OWA with VB Script
    By davidphillips0120 in forum Scripts
    Replies: 3
    Last Post: 25th March 2010, 06:36 PM
  4. Help with Backup Script
    By soze in forum Scripts
    Replies: 5
    Last Post: 18th June 2008, 12:12 AM
  5. Help with KIX script please
    By salan in forum Windows
    Replies: 8
    Last Post: 8th November 2007, 06:55 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •