Posts Tagged ‘Diskspace’

Log diskspace

September 18th, 2009

If you want to log the usage of diskspace on a computer it’s not that hard.. you just need some place to store the data and a script that runs at given intervals.

I have a table in my SQL Server.

CREATE TABLE Diskspace(
	index bigint IDENTITY(1,1) NOT NULL,
	TimeStamp datetime NOT NULL,
	FreePercent decimal(18, 3) NOT NULL,
	DriveLetter char(1) NOT NULL,
 CONSTRAINT PK__Diskspace__49C3F6B7 PRIMARY KEY CLUSTERED
(
	index ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON PRIMARY
) ON PRIMARY

Then I have a VBScript that runs every five minutes (via Eventghost). You need to change databasename, user and password.

Set oDb = createobject("ADODB.Connection")
oDb.ConnectionString = "DRIVER={SQL Server Native Client 10.0};SERVER=localhost;DATABASE=TheDatabaseName;UID=MyUserName;PWD=SomePassword; OPTION=3"
oDb.Open

Set oWmiService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set oDisks = oWmiService.ExecQuery("Select * from Win32_LogicalDisk Where DriveType = 3")

For Each oDisk In oDisks
    sPercent = Replace(Round((oDisk.FreeSpace / oDisk.Size)*100, 1), ",", ".")
    sDisk = uCase(Replace(oDisk.DeviceID, ":", ""))

	sSql = "INSERT INTO diskspace (TimeStamp, FreePercent, DriveLetter) VALUES (GETDATE(), '" & sPercent & "', '" & sDisk & "')"
	WScript.Echo sSql
	oDb.Execute(sSql)
Next

oDb.Close

As easy as that…