|
server
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How to Get the Total Disk Space on a server
machine. I need to get the disk size, free space and used for each disk and total them up for the agreget disk size for each machine. I would also like to limit the search to local hard disks , type 3 in Win32_LogicalDisk where DriveType=3. '---------------------------------------<8>-------------------------------------------------- For Each Disk In GetObject( _ "winmgmts:").InstancesOf ("CIM_LogicalDisk") WScript.Echo "Instance:", Disk.Path_.Relpath Next If Err <> 0 Then set lasterr = CreateObject("WbemScripting.SWbemLastError") Wscript.echo lasterr.Operation End If '--------------------------------------<8>---------------------------------------------------- Here is what I tried and all I get is Zero '--------------------------------------<8>---------------------------------------------------- strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") Set dskItems = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk where DriveType=3", "WQL", _ wbemFlagReturnImmediately + wbemFlagForwardOnly) For Each oItem In dskItems AgDisk = oItem.name(0) Select Case AgDisk Case "C" CDisk = oItem.size CUsed = oItem.FreeSpace Case "E" EDisk = oItem.size EFree = oItem.FreeSpace Case "F" FDisk = oItem.size FFree = oItem.FreeSpace Case "G" GDisk = oItem.size GFree = oItem.FreeSpace Case "I" IDisk = oItem.size IFree = oItem.FreeSpace Case Else On Error GoTo 0 End Select TDisk = (CDisk + EDisk + FDisk + GDisk + IDisk) TFree = (CFree + EFree + FFree + GFree + IFree) 'TUsed = (oItem.Size - oItem.FreeSpace) Next wscript.echo TDisk wscript.echo TFree Found one problem. The oItem.name is C: not just C.
But the disk sizes are wrong, I believe they are stored in "uint64" and may need to be converted. Show quote "MFelkins" wrote: > This sample script will pull the Disk Name for every logical disk on a > machine. I need to get the disk size, free space and used for each disk and > total them up for the agreget disk size for each machine. I would also like > to limit the search to local hard disks , type 3 in Win32_LogicalDisk where > DriveType=3. > '---------------------------------------<8>-------------------------------------------------- > For Each Disk In GetObject( _ > "winmgmts:").InstancesOf ("CIM_LogicalDisk") > WScript.Echo "Instance:", Disk.Path_.Relpath > Next > If Err <> 0 Then > set lasterr = CreateObject("WbemScripting.SWbemLastError") > Wscript.echo lasterr.Operation > End If > '--------------------------------------<8>---------------------------------------------------- > Here is what I tried and all I get is Zero > '--------------------------------------<8>---------------------------------------------------- > strComputer = "." > Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") > Set dskItems = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk > where DriveType=3", "WQL", _ > wbemFlagReturnImmediately + > wbemFlagForwardOnly) > For Each oItem In dskItems > AgDisk = oItem.name(0) > Select Case AgDisk > Case "C" > CDisk = oItem.size > CUsed = oItem.FreeSpace > Case "E" > EDisk = oItem.size > EFree = oItem.FreeSpace > Case "F" > FDisk = oItem.size > FFree = oItem.FreeSpace > Case "G" > GDisk = oItem.size > GFree = oItem.FreeSpace > Case "I" > IDisk = oItem.size > IFree = oItem.FreeSpace > Case Else > On Error GoTo 0 > End Select > TDisk = (CDisk + EDisk + FDisk + GDisk + IDisk) > TFree = (CFree + EFree + FFree + GFree + IFree) > 'TUsed = (oItem.Size - oItem.FreeSpace) > Next > > wscript.echo TDisk > wscript.echo TFree > > "MFelkins" <MFelk***@discussions.microsoft.com> wrote in message '---------------------------------------<8>---------------------------------news:5B8B6D7E-B5BD-43D6-9FB7-58589B4D688D@microsoft.com... > This sample script will pull the Disk Name for every logical disk on a > machine. I need to get the disk size, free space and used for each disk and > total them up for the agreget disk size for each machine. I would also like > to limit the search to local hard disks , type 3 in Win32_LogicalDisk where > DriveType=3. > ----------------- > For Each Disk In GetObject( _ '--------------------------------------<8>----------------------------------> "winmgmts:").InstancesOf ("CIM_LogicalDisk") > WScript.Echo "Instance:", Disk.Path_.Relpath > Next > If Err <> 0 Then > set lasterr = CreateObject("WbemScripting.SWbemLastError") > Wscript.echo lasterr.Operation > End If > ------------------ > Here is what I tried and all I get is Zero '--------------------------------------<8>----------------------------------> ------------------ Show quote > strComputer = "." I see one little type in your code. For all of the items in your> Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") > Set dskItems = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk > where DriveType=3", "WQL", _ > wbemFlagReturnImmediately + > wbemFlagForwardOnly) > For Each oItem In dskItems > AgDisk = oItem.name(0) > Select Case AgDisk > Case "C" > CDisk = oItem.size > CUsed = oItem.FreeSpace > Case "E" > EDisk = oItem.size > EFree = oItem.FreeSpace > Case "F" > FDisk = oItem.size > FFree = oItem.FreeSpace > Case "G" > GDisk = oItem.size > GFree = oItem.FreeSpace > Case "I" > IDisk = oItem.size > IFree = oItem.FreeSpace > Case Else > On Error GoTo 0 > End Select > TDisk = (CDisk + EDisk + FDisk + GDisk + IDisk) > TFree = (CFree + EFree + FFree + GFree + IFree) > 'TUsed = (oItem.Size - oItem.FreeSpace) > Next > > wscript.echo TDisk > wscript.echo TFree Select...Case statements, your are assigning oItem.FreeSpace to 'xFree' (where x is the drive letter) except the C: drive, where you are assigning it to 'CUsed'. If you are wanting the total space for all the local disk & not just C: & E: - I:, you could change your code to something like this (watch for wrapping): '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") Set dskItems = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk where DriveType=3") For Each oItem In dskItems TDisk = CDbl(TDisk) + oItem.size TFree = CDbl(TFree) + oItem.FreeSpace TUsed = CDbl(TUsed) + (oItem.size - oItem.FreeSpace) Next wscript.echo "Total Size: " & TDisk & vbCrLf &_ "Total Free: " & TFree & vbCrLf &_ "Total Used: " & TUsed '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ That works great. Can't say I understand it. What does the CDbl do?
Show quote "James Whitlow" wrote: > "MFelkins" <MFelk***@discussions.microsoft.com> wrote in message > news:5B8B6D7E-B5BD-43D6-9FB7-58589B4D688D@microsoft.com... > > This sample script will pull the Disk Name for every logical disk on a > > machine. I need to get the disk size, free space and used for each disk > and > > total them up for the agreget disk size for each machine. I would also > like > > to limit the search to local hard disks , type 3 in Win32_LogicalDisk > where > > DriveType=3. > > > '---------------------------------------<8>--------------------------------- > ----------------- > > For Each Disk In GetObject( _ > > "winmgmts:").InstancesOf ("CIM_LogicalDisk") > > WScript.Echo "Instance:", Disk.Path_.Relpath > > Next > > If Err <> 0 Then > > set lasterr = CreateObject("WbemScripting.SWbemLastError") > > Wscript.echo lasterr.Operation > > End If > > > '--------------------------------------<8>---------------------------------- > ------------------ > > Here is what I tried and all I get is Zero > > > '--------------------------------------<8>---------------------------------- > ------------------ > > strComputer = "." > > Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") > > Set dskItems = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk > > where DriveType=3", "WQL", _ > > wbemFlagReturnImmediately + > > wbemFlagForwardOnly) > > For Each oItem In dskItems > > AgDisk = oItem.name(0) > > Select Case AgDisk > > Case "C" > > CDisk = oItem.size > > CUsed = oItem.FreeSpace > > Case "E" > > EDisk = oItem.size > > EFree = oItem.FreeSpace > > Case "F" > > FDisk = oItem.size > > FFree = oItem.FreeSpace > > Case "G" > > GDisk = oItem.size > > GFree = oItem.FreeSpace > > Case "I" > > IDisk = oItem.size > > IFree = oItem.FreeSpace > > Case Else > > On Error GoTo 0 > > End Select > > TDisk = (CDisk + EDisk + FDisk + GDisk + IDisk) > > TFree = (CFree + EFree + FFree + GFree + IFree) > > 'TUsed = (oItem.Size - oItem.FreeSpace) > > Next > > > > wscript.echo TDisk > > wscript.echo TFree > > I see one little type in your code. For all of the items in your > Select...Case statements, your are assigning oItem.FreeSpace to 'xFree' > (where x is the drive letter) except the C: drive, where you are assigning > it to 'CUsed'. > > If you are wanting the total space for all the local disk & not just C: & > E: - I:, you could change your code to something like this (watch for > wrapping): > > '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > strComputer = "." > Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") > > Set dskItems = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk > where DriveType=3") > > For Each oItem In dskItems > TDisk = CDbl(TDisk) + oItem.size > TFree = CDbl(TFree) + oItem.FreeSpace > TUsed = CDbl(TUsed) + (oItem.size - oItem.FreeSpace) > Next > > wscript.echo "Total Size: " & TDisk & vbCrLf &_ > "Total Free: " & TFree & vbCrLf &_ > "Total Used: " & TUsed > '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > > > 'CDbl()' converts a variable to the "Double" datatype. See this link:
http://msdn.microsoft.com/library/en-us/script56/html/vsfctcdbl.asp The 'oItem.Size' has a data type of 'String', so when you set your variant variable to 'oItem.Size', it became a string. If you add 2 variables containing the value of '1' in some numeric datatype together, you will get a value of '2'. If you add 2 variables containing the value of '1' in the "String" datatype together, you will get a value of '11'. It simply concatenates them. Specifically converting your variables to the "Double" datatype prevents the automatic conversion to the "String" datatype. Alternately, you could convert the 'oItem.Size' before assigning it to your variables and you would get the same results: '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") Set dskItems = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk where DriveType=3") For Each oItem In dskItems TDisk = TDisk + CDbl(oItem.Size) TFree = TFree + CDbl(oItem.FreeSpace) TUsed = TUsed + (CDbl(oItem.Size) - CDbl(oItem.FreeSpace)) Next WScript.Echo "Total Size: " & TDisk & vbCrLf &_ "Total Free: " & TFree & vbCrLf &_ "Total Used: " & TUsed '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ If you are unsure of a variable's datatype, temporarily add a 'TypeName' statement into you code. Something along these lines: WScript.Echo TypeName(oItem.Size) It will show you what datatype you are working with. This is really handy for finding out some function returned an array to you when you were expecting a string. Most of the time when you work with the "Variant" datatype in VBScript, it will make the correct assumption on whether to concatenate or add, but not always. Show quote "MFelkins" <MFelk***@discussions.microsoft.com> wrote in message '---------------------------------------<8>---------------------------------news:5DAE12CB-2FBD-4F53-A916-0636D1050099@microsoft.com... > That works great. Can't say I understand it. What does the CDbl do? > > "James Whitlow" wrote: > > > "MFelkins" <MFelk***@discussions.microsoft.com> wrote in message > > news:5B8B6D7E-B5BD-43D6-9FB7-58589B4D688D@microsoft.com... > > > This sample script will pull the Disk Name for every logical disk on a > > > machine. I need to get the disk size, free space and used for each disk > > and > > > total them up for the agreget disk size for each machine. I would also > > like > > > to limit the search to local hard disks , type 3 in Win32_LogicalDisk > > where > > > DriveType=3. > > > > > > > ----------------- '--------------------------------------<8>----------------------------------> > > For Each Disk In GetObject( _ > > > "winmgmts:").InstancesOf ("CIM_LogicalDisk") > > > WScript.Echo "Instance:", Disk.Path_.Relpath > > > Next > > > If Err <> 0 Then > > > set lasterr = CreateObject("WbemScripting.SWbemLastError") > > > Wscript.echo lasterr.Operation > > > End If > > > > > > > ------------------ '--------------------------------------<8>----------------------------------> > > Here is what I tried and all I get is Zero > > > > > Show quote > > ------------------ > > > strComputer = "." > > > Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") > > > Set dskItems = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk > > > where DriveType=3", "WQL", _ > > > wbemFlagReturnImmediately + > > > wbemFlagForwardOnly) > > > For Each oItem In dskItems > > > AgDisk = oItem.name(0) > > > Select Case AgDisk > > > Case "C" > > > CDisk = oItem.size > > > CUsed = oItem.FreeSpace > > > Case "E" > > > EDisk = oItem.size > > > EFree = oItem.FreeSpace > > > Case "F" > > > FDisk = oItem.size > > > FFree = oItem.FreeSpace > > > Case "G" > > > GDisk = oItem.size > > > GFree = oItem.FreeSpace > > > Case "I" > > > IDisk = oItem.size > > > IFree = oItem.FreeSpace > > > Case Else > > > On Error GoTo 0 > > > End Select > > > TDisk = (CDisk + EDisk + FDisk + GDisk + IDisk) > > > TFree = (CFree + EFree + FFree + GFree + IFree) > > > 'TUsed = (oItem.Size - oItem.FreeSpace) > > > Next > > > > > > wscript.echo TDisk > > > wscript.echo TFree > > > > I see one little type in your code. For all of the items in your > > Select...Case statements, your are assigning oItem.FreeSpace to 'xFree' > > (where x is the drive letter) except the C: drive, where you are assigning > > it to 'CUsed'. > > > > If you are wanting the total space for all the local disk & not just C: & > > E: - I:, you could change your code to something like this (watch for > > wrapping): > > > > '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > > strComputer = "." > > Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") > > > > Set dskItems = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk > > where DriveType=3") > > > > For Each oItem In dskItems > > TDisk = CDbl(TDisk) + oItem.size > > TFree = CDbl(TFree) + oItem.FreeSpace > > TUsed = CDbl(TUsed) + (oItem.size - oItem.FreeSpace) > > Next > > > > wscript.echo "Total Size: " & TDisk & vbCrLf &_ > > "Total Free: " & TFree & vbCrLf &_ > > "Total Used: " & TUsed > > '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > > > > > > |
|||||||||||||||||||||||