Re: How to Get the Total Disk Space on a server
- From: JC <JC@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 28 Dec 2007 09:33:03 -0800
I have a perl script that gets a lot of the disk info I need but I would like
the free space response to be in Gb. Does anyone know a way to add /
1073741824 to it so that the number will appear in Gb?
use Win32::OLE('in');
use constant wbemFlagReturnImmediately => 0x10;
use constant wbemFlagForwardOnly => 0x20;
$computer = ".";
$objWMIService = Win32::OLE->GetObject
("winmgmts:\\\\$computer\\root\\CIMV2") or die "WMI connection failed.\n";
$colItems = $objWMIService->ExecQuery
("SELECT * FROM Win32_LogicalDisk","WQL",wbemFlagReturnImmediately |
wbemFlagForwardOnly);
foreach my $objItem (in $colItems)
{
print "Access: $objItem->{Access}\n";
print "Availability: $objItem->{Availability}\n";
print "Block Size: $objItem->{BlockSize}\n";
print "Caption: $objItem->{Caption}\n";
print "Compressed: $objItem->{Compressed}\n";
print "Config Manager Error Code: $objItem->{ConfigManagerErrorCode}\n";
print "Config Manager User Config:
$objItem->{ConfigManagerUserConfig}\n";
print "Creation Class Name: $objItem->{CreationClassName}\n";
print "Description: $objItem->{Description}\n";
print "Device ID: $objItem->{DeviceID}\n";
print "Drive Type: $objItem->{DriveType}\n";
print "Error Cleared: $objItem->{ErrorCleared}\n";
print "Error Description: $objItem->{ErrorDescription}\n";
print "Error Methodology: $objItem->{ErrorMethodology}\n";
print "File System: $objItem->{FileSystem}\n";
print "Free Space: $objItem->{FreeSpace}\n";
print "Install Date: $objItem->{InstallDate}\n";
print "Last Error Code: $objItem->{LastErrorCode}\n";
print "Maximum Component Length: $objItem->{MaximumComponentLength}\n";
print "Media Type: $objItem->{MediaType}\n";
print "Name: $objItem->{Name}\n";
print "Number Of Blocks: $objItem->{NumberOfBlocks}\n";
print "PNP Device ID: $objItem->{PNPDeviceID}\n";
print "Power Management Capabilities: " . join(",", (in
$objItem->{PowerManagementCapabilities})) . "\n";
print "Power Management Supported:
$objItem->{PowerManagementSupported}\n";
print "Provider Name: $objItem->{ProviderName}\n";
print "Purpose: $objItem->{Purpose}\n";
print "Quotas Disabled: $objItem->{QuotasDisabled}\n";
print "Quotas Incomplete: $objItem->{QuotasIncomplete}\n";
print "Quotas Rebuilding: $objItem->{QuotasRebuilding}\n";
print "Size: $objItem->{Size}\n";
print "Status: $objItem->{Status}\n";
print "Status Info: $objItem->{StatusInfo}\n";
print "Supports Disk Quotas: $objItem->{SupportsDiskQuotas}\n";
print "Supports File-Based Compression:
$objItem->{SupportsFileBasedCompression}\n";
print "System Creation Class Name:
$objItem->{SystemCreationClassName}\n";
print "System Name: $objItem->{SystemName}\n";
print "Volume Dirty: $objItem->{VolumeDirty}\n";
print "Volume Name: $objItem->{VolumeName}\n";
print "Volume Serial Number: $objItem->{VolumeSerialNumber}\n";
print "\n";
}
Thanks,
JC
"James Whitlow" wrote:
'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.
"MFelkins" <MFelkins@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:5DAE12CB-2FBD-4F53-A916-0636D1050099@xxxxxxxxxxxxxxxx
That works great. Can't say I understand it. What does the CDbl do?disk
"James Whitlow" wrote:
"MFelkins" <MFelkins@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:5B8B6D7E-B5BD-43D6-9FB7-58589B4D688D@xxxxxxxxxxxxxxxx
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
'---------------------------------------<8>---------------------------------and
total them up for the agreget disk size for each machine. I would alsolike
to limit the search to local hard disks , type 3 in Win32_LogicalDiskwhere
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
"\root\CIMV2")------------------
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer &
Win32_LogicalDiskSet dskItems = objWMIService.ExecQuery("SELECT * FROM
assigningwhere 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
C: &it to 'CUsed'.
If you are wanting the total space for all the local disk & not just
"\root\CIMV2")E: - I:, you could change your code to something like this (watch for
wrapping):
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer &
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
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Follow-Ups:
- Re: How to Get the Total Disk Space on a server
- From: Hal Rottenberg
- Re: How to Get the Total Disk Space on a server
- Prev by Date: Re: remote server shutdown options
- Next by Date: How to bring a running application to the foreground.
- Previous by thread: Need Reg key for enable detail folder view in Explorer
- Next by thread: Re: How to Get the Total Disk Space on a server
- Index(es):
Relevant Pages
|