Novell Cool Solutions

VBScript to Check Disk Space Availability



By:

January 4, 2008 6:15 am

Reads:34,726

Comments:2

Score:3

Print/PDF

I have a requirement to check the disk space on our servers on a regular basis. This is obviously quite time consuming. I tried to find a cheap (free) method of saving the volumes sizes and free space to a file so I could import into Excel.

After not having a great deal of success locating one, I decided to try and write something in VBS.

I am not a VBS expert, and I have freely used samples available from the web.

There is probably a better way of doing this, if so feel free to add, remove, mangle as you see fit.

I created the following VBscript:

On Error Resume Next
'specify the file name and location of the output file
strDirectory = "c:\logs3\Disks"
strFile = "\NetwareDrive.txt"
'the mappings will be run on this computer
locComputer = "."
'create and overwrite the existing file
set fso = CreateObject("Scripting.FileSystemObject")
set alog = fso.CreateTextFile(strDirectory&strFile, True)
alog.WriteLine "Server,Driver Letter,Disk Size,FreeDisk Space"
alog.Close
'set the WMI service & shell
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & locComputer & "\root\cimv2")
Set WshShell = WScript.CreateObject("WScript.Shell")

'Create the Array containing all the server volumes in the format "ServerName/Volume1","ServerName/Volume2"
arrVolume = Array("ServerName/Volume1","ServerName/Volume2")

'Run script below for every entry in array
For Each strVolume In arrVolume
'Map the volume to be checked to the designated drive letter
WshShell.Run "cmd /C map root t:="&strVolume&":", 7, True


' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Check that the strDirectory folder exists
If objFSO.FolderExists(strDirectory) Then
	Set objFolder = objFSO.GetFolder(strDirectory)
Else
	Set objFolder = objFSO.CreateFolder(strDirectory)
End If
If objFSO.FileExists(strDirectory & strFile) Then
	Set objFolder = objFSO.GetFolder(strDirectory)
Else
	Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
End If 

' OpenTextFile Method needs a Const value
'' ForAppending = 8 ForReading = 1, ForWriting = 2
Const ForAppending = 8
Set objTextFile = objFSO.OpenTextFile (strDirectory & strFile, ForAppending, True)

'This uses WMI to view all drives on localPC
Set colDisks = objWMIService.ExecQuery ("Select * from Win32_LogicalDisk")
'For each drive attached
For Each objDisk in colDisks
	'Test to see if the drive letter is the one specified
	if objDisk.DeviceID="T:" then
		'Change from Bytes to GB
		DiskSize=objDisk.Size/(1024*1024*1024)
		DiskFree=objDisk.FreeSpace/(1024*1024*1024)
		'wscript.echo strVolume&"," & objDisk.DeviceID & ","& DiskSize & "," & DiskFree
		'Write Volume sizes to file
		strText= strVolume&"," & ","& DiskSize & "," & DiskFree
		objTextFile.WriteLine(strText)
	end if
'next drive
Next
'Remove temp mapping 
WshShell.Run "cmd /C map del t:", 7, True
'Next item in Array
Next

'Close file
objTextFile.Close

and saved it to a file, and ran it from CScript.exe ( I actually use Zen to make the icon )

The basic idea is:

[List of Netware Volumes]
Create Logfile with header row
Open Logfile
For each list of Netware Volumes
Map temp drive letter
For each drive from WMI
If Drive letter is Temp
	Write Volume Name, Disk Size, Disk Space to logfile
End If
	Next Drive letter
Remove drive mapping
Next Volume in List
Close file

I also did something similar for the Microsoft servers, but in this case the WMI can run on the remote server.

'Richard Gray 4/1/2008
' this VBScript writes the size and available size from a list of Microsoft volumes to a text file.

'specify the file name and location of the output file
strDirectory = "c:\logs3\Disks"
strFile = "\microsoftDrive.txt"
'set the computer that writes the file to be local workstation
locComputer = "."
'Domain and admin details to connect to remote server
strDomain = "Domain" 
strUser = "administrator"
strPassword = "adminpassword"
On Error Resume Next
'Create file on local machine
set fso = CreateObject("Scripting.FileSystemObject")
set alog = fso.CreateTextFile(strDirectory&strFile, True)
'Create Header row
alog.WriteLine "Server,Driver Letter,Disk Size,FreeDisk Space"
alog.Close
'Create array with list of servers
arrComputers = Array("MSW2k3_1","MSW2k3_2")
'Run script below for every entry in array
For Each strComputer In arrComputers

'set the WMI service & shell onto remote server
Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator") 
Set objWMIService = objSWbemLocator.ConnectServer(strComputer, _ 
    "root\CIMV2", _ 
    strUser, _ 
    strPassword, _ 
    "MS_409", _ 
    "ntlmdomain:" + strDomain)
' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Check that the strDirectory folder exists
If objFSO.FolderExists(strDirectory) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFolder = objFSO.CreateFolder(strDirectory)
WScript.Echo "Just created " & strDirectory
End If

If objFSO.FileExists(strDirectory & strFile) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
Wscript.Echo "Just created " & strDirectory & strFile
End If 

' OpenTextFile Method needs a Const value
'' ForAppending = 8 ForReading = 1, ForWriting = 2
Const ForAppending = 8
Set objTextFile = objFSO.OpenTextFile _
(strDirectory & strFile, ForAppending, True)
'This uses WMI to view all drives on Remote Server
	Set colDisks = objWMIService.ExecQuery ("Select * from Win32_LogicalDisk")
	For Each objDisk in colDisks
		'Test to see if the drive is a local drive 
		if objDisk.DriveType=3 then
			'convert bytes to GB
			DiskSize=objDisk.Size/(1024*1024*1024)
			DiskFree=objDisk.FreeSpace/(1024*1024*1024)
			'wscript.echo strComputer&"," & objDisk.DeviceID & ","& DiskSize & "," & DiskFree
			'Write Volume sizes to file
			strText= strComputer&"," & objDisk.DeviceID & ","& DiskSize & "," & DiskFree
			objTextFile.WriteLine(strText)
		end if
	'next disk
	Next
'Next server
next
'Close file
objTextFile.Close

The basic idea behind this is:

[List of Microsoft Servers]
Create Logfile with header row
Open Logfile
For each list of Microsoft server
Run WMI on Remote server
For each drive from WMI
If Drive type is local
	Write Volume Name, Disk Size, Disk Space to logfile
End If
	Next Drive letter
Next Server in List
Close file

1 vote, average: 3.00 out of 51 vote, average: 3.00 out of 51 vote, average: 3.00 out of 51 vote, average: 3.00 out of 51 vote, average: 3.00 out of 5 (1 votes, average: 3.00 out of 5)
You need to be a registered member to rate this post.
Loading...Loading...

Tags: , ,
Categories: Uncategorized

2

Disclaimer: This content is not supported by Novell. It was contributed by a community member and is published "as is." It seems to have worked for at least one person, and might work for you. But please be sure to test it thoroughly before using it in a production environment.

2 Comments

  1. By:thomasroll

    I put together an Excel Add-In which enables you to report on Novell Netware Servers and Volumes.

    The Add-In code is (freely) available from:

    https://developer.novell.com/wiki/index.php/Nwaddin

    Regards,
    Thomas

  2. By:smily_03

    I also took this and modified it to rescan every 10 minutes and dump a file up to a webserver so that you can view the status remotely (and embed it in your iGoogle page too :o) ) If you want info on how to do that, email me at cmyers at millikin dot edu.

    For an example, go here: http://admin.millikin.edu/~scripter/volstats.html

Comment

RSS