#87281 - 2002-08-17 11:32 AM
Get percent drive space alerts
|
Douglas Cohn
Lurker
Registered: 2002-08-17
Posts: 1
Loc: New York
|
How would I create a routine that can notify me when the percentage of available drive space on specified drives falls below a set amount.
I do not care about user or quotas. I just want to know when the drive has less than 15% fee space.
I can schedule it to run every 30 minutes and can email it using smtp or blat but I am lost as to what will give such results.
Thanks
Doug
|
|
Top
|
|
|
|
#87283 - 2002-08-17 03:42 PM
Re: Get percent drive space alerts
|
Dunc
Getting the hang of it
Registered: 2002-05-25
Posts: 54
Loc: Guildford England
|
Douglas
This bit of code may help you. It gets the disk size and freespace for all fixed disks from WMI.
Duncan
code:
$objWMI = GetObject("winmgmts:{impersonationlevel=impersonate}!//" + @WKSTA)
$colDisks = $objWMI.ExecQuery("select * from win32_logicalDisk") For Each $objDisk In $colDisks
if $objdisk.mediatype <> 12
else
$name = $objdisk.name $volName = $objDisk.VolumeName $volfreepace = $objDisk.freespace $volsize = $objDisk.size
? "Name is: " + $name ? "Volume name is: " + $volName ? "Volume size is: " + $volsize ? "Free space : " + $volfreepace
endif
Next $objWMI = 0
|
|
Top
|
|
|
|
#87289 - 2002-08-17 04:32 PM
Re: Get percent drive space alerts
|
Dunc
Getting the hang of it
Registered: 2002-05-25
Posts: 54
Loc: Guildford England
|
humm..
The problem with converting from a string is it alters the value and then the info is incorrect.
$mb = 0.0 + $volsize / 1024 still come up with same error!
Duncan
|
|
Top
|
|
|
|
#87291 - 2002-08-17 04:33 PM
Re: Get percent drive space alerts
|
kholm
Korg Regular
   
Registered: 2000-06-19
Posts: 714
Loc: Randers, Denmark
|
Duncan,
Assuming you are using KiX 4.1+ Try using the CDbl() and FormatNumber() functions:
code:
$objWMI = GetObject("winmgmts:{impersonationlevel=impersonate}!//" + @WKSTA)
$colDisks = $objWMI.ExecQuery("select * from win32_logicalDisk") For Each $objDisk In $colDisks if $objdisk.mediatype = 12 $name = $objdisk.name $volsize = $objDisk.size $Kb = CDbl($volsize) / 1024 $Mb = CDbl($Kb) / 1024 ? "Name is: " + $name ? "Volume size in bytes is: " + $volsize ? "Volume size in kb is: " + FormatNumber($Kb,1) ? "Volume size in Mb is: " + FormatNumber($Mb,1) endif Next $objWMI = 0
I just tested this code, works on XP
-Erik
|
|
Top
|
|
|
|
#87292 - 2002-08-17 04:40 PM
Re: Get percent drive space alerts
|
Dunc
Getting the hang of it
Registered: 2002-05-25
Posts: 54
Loc: Guildford England
|
Erik,
Thanks
That works great (Windows 2000).
Duncan
|
|
Top
|
|
|
|
#87294 - 2002-08-17 05:26 PM
Re: Get percent drive space alerts
|
kholm
Korg Regular
   
Registered: 2000-06-19
Posts: 714
Loc: Randers, Denmark
|
To use implicit typecasting, you can use:
code:
$Kb = (0.0 + $volsize) / 1024
I think you should be very carefull when using Val() If you use Val() on a variable containing a value bigger than the largest possible integer, you get unpredictable results.
Lonkero, I know the implicit typecating is a finding from one of the KiX-golf games, but maybe we should be careful when using it on big values. In this situation we might as well use the CDbl()
The code above works in v. 4.10 and 4.11b2
-Erik
|
|
Top
|
|
|
|
#87295 - 2002-08-17 05:38 PM
Re: Get percent drive space alerts
|
Dunc
Getting the hang of it
Registered: 2002-05-25
Posts: 54
Loc: Guildford England
|
Douglas,
This should give you the required results. (I need to go away and put this into some of my other scripts now!)
Thanks for Lonkero's and Erik's help.
Duncan
code:
$objWMI = GetObject("winmgmts:{impersonationlevel=impersonate}!//" + @WKSTA)
$colDisks = $objWMI.ExecQuery("select * from win32_logicalDisk") For Each $objDisk In $colDisks if $objdisk.mediatype = 12 $name = $objdisk.name $volsize = $objDisk.size $freespace = $objDisk.freespace $freekb = CDbl($freespace) / 1024 $freemb = CDbl($freekb) / 1024 $VolKb = CDbl($volsize) / 1024 $volMb = CDbl($volKb) / 1024 ; ? "Name is: " + $name ; ? "Volume size in bytes is: " + $volsize ; ? "Volume size in kb is: " + FormatNumber($volKb,1) ; ? "Volume size in Mb is: " + FormatNumber($volMb,1) ; ? "Free space in bytes is: " + $freespace ; ? "Free space kb is: " + FormatNumber($freeKb,1) ; ? "Free space in Mb is: " + FormatNumber($freeMb,1) $per = $freemb / $volmb * 100
If $per < 15
messagebox(FormatNumber($per,1)+ "% Disk Space free","Disk Space",48) Endif
endif
Next $objWMI = 0
|
|
Top
|
|
|
|
#87297 - 2002-08-19 11:39 AM
Re: Get percent drive space alerts
|
Dunc
Getting the hang of it
Registered: 2002-05-25
Posts: 54
Loc: Guildford England
|
sealeopard
Thats fine if you are using NT/2000/XP.
We've still got 11,000 Windows 98 clients!
Unless you know of a way under 98 to do the same.
Still the code has come in usful for other tasks I have in mind as well!
Duncan
|
|
Top
|
|
|
|
Moderator: Shawn, ShaneEP, Ruud van Velsen, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Mart
|
0 registered
and 484 anonymous users online.
|
|
|