Welcome to ServerForumz.com!
FAQFAQ      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

How to Get the Total Disk Space on a server

 
   Windows Server (Home) -> Windows Server Scripting RSS
Next:  Application install failing through script  
Author Message
MFelkins

External


Since: Oct 08, 2004
Posts: 42



(Msg. 1) Posted: Wed Jul 27, 2005 12:17 pm
Post subject: How to Get the Total Disk Space on a server
Archived from groups: microsoft>public>windows>server>scripting (more info?)

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

 >> Stay informed about: How to Get the Total Disk Space on a server 
Back to top
Login to vote
MFelkins

External


Since: Oct 08, 2004
Posts: 42



(Msg. 2) Posted: Wed Jul 27, 2005 1:02 pm
Post subject: RE: How to Get the Total Disk Space on a server [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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.


"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
>
>

 >> Stay informed about: How to Get the Total Disk Space on a server 
Back to top
Login to vote
MFelkins

External


Since: Oct 08, 2004
Posts: 42



(Msg. 3) Posted: Wed Jul 27, 2005 2:17 pm
Post subject: Re: How to Get the Total Disk Space on a server [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

That works great. Can't say I understand it. What does the CDbl do?

"James Whitlow" wrote:

> "MFelkins" <MFelkins.DeleteThis@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
> '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
>
>
 >> Stay informed about: How to Get the Total Disk Space on a server 
Back to top
Login to vote
JC

External


Since: Jun 05, 2006
Posts: 12



(Msg. 4) Posted: Fri Dec 28, 2007 9:33 am
Post subject: Re: How to Get the Total Disk Space on a server [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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.DeleteThis@discussions.microsoft.com> wrote in message
> 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" <MFelkins.DeleteThis@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
> > > '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > >
> > >
> > >
>
>
>
 >> Stay informed about: How to Get the Total Disk Space on a server 
Back to top
Login to vote
Hal Rottenberg

External


Since: Dec 29, 2007
Posts: 1



(Msg. 5) Posted: Sat Dec 29, 2007 12:22 pm
Post subject: Re: How to Get the Total Disk Space on a server [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

JC wrote:
> 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?

Well first let me convert your Perl 60-line script to PowerShell:

Get-WmiObject Win32_LogicalDisk

That's it. Smile

Now, to answer your question, we'll use a PowerShell concept called
calculated properties.

# get only fixed disks and assign to variable
$disks = Get-WmiObject Win32_LogicalDisk -filter 'DriveType = 3'

# Assigning these script blocks to variables to make things more
# legible. Note that GB is a special constant here.
[scriptblock]$FreeGB = { $_.FreeSpace / 1GB }
[scriptblock]$SizeGB = { $_.Size / 1GB }

# For each disk, output the drive letter and volume name,
# then execute those script blocks to create the last two columns
$disks | format-table -AutoSize DeviceID, VolumeName,
@{ Label = "Size"; Expression = $SizeGB },
@{ Label="Free Space"; Expression = $FreeGB }

Output looks like this:

DeviceID VolumeName Size Free Space
-------- ---------- ---- ----------
C: 19.5339584350586 2.51590347290039
D: 232.875793457031 189.731174468994
E: Data 232.875793457031 12.3140716552734




--
Hal Rottenberg <hal RemoveThis @halr9000.com>
Author, TechProsaic (http://halr9000.com)
Webmaster, Psi (http://psi-im.org)
Co-host, PowerScripting Podcast (http://powerscripting.net)
 >> Stay informed about: How to Get the Total Disk Space on a server 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Available Disk Space - I need a script that checks the available disk space and email the results to multiple recipients using smtp. I dont know much about scripting, any help would be greatly appreciated.

Newbie - Available Disk Space - Sorry for the question, but i'm really newbie in this stuff. How can I get the remote available disks space divided by partition or volume? Tks in advance, Giuliano

monitoring disk space - i'm trying to write a simple script which would monitor the disk space and send an email if the usage exceed over certain megs. Any suggestion help is appreciated.

Simple Disk Space Script - Ok using the simple script below I can look at the disk space on my local machine. However if I change the strComputer name to a remote machine I get an error diskspace2.vbs(2, 1) Microsoft VBScript runtime error: Permission denied: 'GetObject' I..

Finding Free Disk Space - Hello, I am trying writing this script to alert me if the disk space gets below 75GB for some reason it's not working .Can anybody help what am i doing wrong or is there a better a way of doing this. rem @echo off rem batch code to detect low disk...
   Windows Server (Home) -> Windows Server Scripting All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]