Jump to content

Recommended Posts

Posted

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

 

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

Posted

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
[b]If objDisk.DeviceID = "C:" Then[/b]
   Wscript.Echo "DeviceID: " & objDisk.DeviceID       
   Wscript.Echo "Disk Size: " & Int(objDisk.Size/1073741824) & " GB"
[b]End If[/b]
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.

Posted (edited)

Try changing the line which has:

SELECT * FROM Win32_LogicalDisk

to:

SELECT * FROM Win32_LogicalDisk WHERE Name = 'C:'

 

(haven't tried this, just a guess) :)

Edited by anthonyd
Changed " to ' around C:
  • Thanks 1
Posted
Try changing the line which has:

SELECT * FROM Win32_LogicalDisk

to:

SELECT * FROM Win32_LogicalDisk WHERE Name = "C:"

 

(haven't tried this, just a guess) :)

 

That's another way of doing it

  • Thanks 1
Posted (edited)
SELECT * FROM Win32_LogicalDisk WHERE Name = 'C:'

Local HDDs have drivetype of 3, so you could do this...

 

SELECT * from Win32_LogicalDisk WHERE DriveType = 3

Edited by Arthur
  • Thanks 1
Posted

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 :

 


[left][color=#333333]Int(objDisk.Size/1073741824) & " GB"[/color][/left]




 

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

 






[left][/left]



Network Information
    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"
>
<br />
a:link {<br />
    color:[url="http://www.edugeek.net/usertag.php?do=list&action=hash&hash=ffffff"]#ffffff[/url]<br />
    font-size:10px;<br />
    font-family:"Times New Roman", Times, serif;<br />
    text-decoration:none;<br />
    font-style: normal;<br />
    font-variant: normal;<br />
}<br />
a:visited {<br />
    color:[url="http://www.edugeek.net/usertag.php?do=list&action=hash&hash=ffffff"]#ffffff[/url]<br />
    font-size:10px;<br />
    font-family:"Times New Roman", Times, serif;<br />
    text-decoration:none;<br />
    font-style: normal;<br />
    font-variant: normal;<br />
}<br />
a:hover {<br />
    color:[url="http://www.edugeek.net/usertag.php?do=list&action=hash&hash=ffffff"]#ffffff[/url]<br />
    font-size:10px;<br />
    font-family:"Times New Roman", Times, serif;<br />
    text-decoration:underline;<br />
    font-style: normal;<br />
    font-variant: normal;<br />
}<br />
td {<br />
    font-family: "Times New Roman", Times, serif;<br />
    font-size: 18px;<br />
    font-style: normal;<br />
    font-weight: normal;<br />
    font-variant: normal;<br />
    color: [url="http://www.edugeek.net/usertag.php?do=list&action=hash&hash=ffffff"]#ffffff[/url]<br />
    vertical-align: left;<br />
}<br />
.status {<br />
    text-align:center;<br />
}<br />

<br />
Sub Window_onLoad <br />
    window.resizeTo 700,500<br />
End Sub<br />
<br />
<br />
sub user()<br />
Dim strUser, x<br />
Set wshNetwork = CreateObject( "WScript.Network" )<br />
strComputer = Document.getElementById("computer").value<br />
<br />
<br />
Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")<br />
Set objSWbemServices = objSWbemLocator.ConnectServer(strComputer, "root\cimv2", Document.getElementById("username").value,Document.getElementById("password").value)<br />
objSWbemServices.Security_.ImpersonationLevel = 3<br />
<br />
<br />
<br />
<br />
Set colOperatingSystems= objSWbemServices.ExecQuery("Select * from Win32_OperatingSystem")<br />
For Each objOperatingSystem in colOperatingSystems<br />
    OS.innerHTML =  objOperatingSystem.Caption & " " & objOperatingSystem.Version<br />
Next<br />
<br />
<br />
<br />
<br />
Set colSerials= objSWbemServices.ExecQuery("Select * from Win32_BIOS",,48)<br />
<br />
<br />
For Each objSerial in colSerials<br />
serial.innerHTML = "Serial Number : " &  objSerial.serialnumber<br />
Next<br />
<br />
<br />
Set colDisks= objSWbemServices.ExecQuery("Select * from Win32_LogicalDisk")<br />
For Each objDisk in colDisks<br />
If objDisk.DriveType = "3" Then<br />
hdd.innerHTML =  "Hard Drive " &  Int(objDisk.Size /1073741824) & " GB"<br />
End If<br />
Next<br />
<br />
<br />
Set colProcessors= objSWbemServices.ExecQuery("Select * from Win32_Processor")<br />
For Each objProcessor in colProcessors<br />
        cpu.innerHTML = "Processor : " &  objProcessor.Name <br />
Next<br />
<br />
<br />
Dim totMem<br />
Dim strModel<br />
<br />
<br />
'Get currently logged on user's username<br />
Set colComputer = objSWbemServices.ExecQuery("Select * from Win32_ComputerSystem")<br />
For Each objComputer in colComputer<br />
strUser = "Username : " & objComputer.UserName<br />
    If IsNull(objComputer.UserName) Then 'If no one is logged on<br />
        strLoggedOn = "No"<br />
    Else<br />
        strLoggedOn = "Yes"<br />
    End If<br />
totMem = "RAM : " &  int((objComputer.TotalPhysicalMemory) /1048576)+1<br />
strModel = "Model : " & objComputer.Model<br />
<br />
<br />
Next<br />
<br />
<br />
Model.innerHTML = strModel<br />
<br />
<br />
If strLoggedOn = "No" Then<br />
  msgbox "computer is free"<br />
Else<br />
'msgbox strUser<br />
'x = split(struser,"\")<br />
info.innerHTML = strUser<br />
End If<br />
'Get currently logged on user's username<br />
<br />
<br />
ram.innerHTML = totMem & " Mb"<br />
<br />
<br />
<br />
<br />
<br />
<br />
end sub<br />
<br />
<br />
Sub Clear()<br />
Model.innerHTML = ""<br />
OS.innerHTML = ""<br />
info.innerHTML = ""<br />
cpu.innerHTML = ""<br />
ram.innerHTML = ""<br />
hdd.innerHTML = ""<br />
serial.innerHTML = ""<br />
end sub<br />
<br />
<br />




Remote Computer
Computer : 

Username : 

Password : 




















[left][/left]




Posted (edited)

As in show more decimal places?

 

Set colDisks= objSWbemServices.ExecQuery("Select * from Win32_LogicalDisk")
For Each objDisk in colDisks
If objDisk.DriveType = "3" Then
hdd.innerHTML =  "Hard Drive " &  [color="#FF0000"]Round((objDisk.size/1024^3),2)[/color] & " GB"
End If
Next

Edited by Arthur
Posted (edited)
As in show more decimal places?

 

Set colDisks= objSWbemServices.ExecQuery("Select * from Win32_LogicalDisk")
For Each objDisk in colDisks
If objDisk.DriveType = "3" Then
hdd.innerHTML =  "Hard Drive " &  [color=#FF0000]Round((objDisk.size/1073741824),2)[/color] & " 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

Edited by mac_shinobi
Posted

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)

 

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(";(&(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

  • Thanks 1
Posted

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

  • Thanks 1
Posted
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

Posted (edited)

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"

Edited by PiqueABoo
  • Thanks 1
Posted
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 )

Posted

(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.

  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...