Monday, November 9, 2009

Disk Space Checking using VB Script

'File Name : DriveSpace.vbs


'Description : Collects the Space information of hard drives from different servers

'Date Created : 04-29-2008

'Author : Arumugam
'

'Dependencies:

'strServerFile - This script depends on a txt file which contains the server names.

'strOutFile - output file location has to be modified as per your requirement

'========================================================================================



t1=time

On error Resume Next



'Get the Script Name

strScript = wscript.ScriptName



'Get the Folder where the script is being executed

strScriptFolder = Replace(wscript.scriptFullName,strScript,"")



'location of the servers.txt file - contains server names

strServersFile = strScriptFolder & "servers.txt"





'Location of the output file to be created.

strOutFile = strScriptFolder & "\Disk_Space_Monitor_Results.txt"



wscript.echo

wscript.echo "Disk Space Monitor"

wscript.echo "******************"

wscript.echo



'Get the login credentials of the user

Wscript.StdOut.Write "Enter your NBD ID :"

strUserID = Wscript.StdIn.ReadLine

Set objPassword = CreateObject("ScriptPW.Password")

Wscript.StdOut.Write "Enter your password (corp domain):"

strPassword = objPassword.GetPassword()

Wscript.Echo

wscript.echo



'Create the outfile on which the Drive space to be written

set objFSO=createobject("scripting.filesystemobject")

set objFSOFile=objFSO.createTextFile(strOutFile)



'Append the domain name with the login id of the user

strUser="CORP\"&strUserID



'Open the text file which contains the server names

set fso = createobject("scripting.filesystemobject")



'Check if servers file is present int he script location

if fso.fileexists(strServersFile) = FALSE then

wscript.echo "Error:servers File not Found"&vbcrlf&strServersFile

set fso = nothing

wscript.echo "Press any key to exit"

Wscript.StdIn.ReadLine

wscript.quit

end if



set objfile= fso.opentextfile(strServersFile)

wscript.echo "Collecting Drive Space details from 62 servers. please wait for 2 mins.."



ALL_SERVERS_FLAG = TRUE



'Read each line (server name) till the End of the file

do until objfile.atendofstream



strserver=objfile.readline



'Call the method getdriveinfo by passing the server name as argument

strResult = getdriveinfo(strserver)



if strResult = "Error" or strResult=""then



objFSOFile.writeline "An error has occurred"

objFSOFile.close

set objFSOFile = Nothing

objFile.close

set objFile = Nothing

set fso = nothing

wscript.quit



elseif instr(1,strResult,"Fine") = 0 then



ALL_SERVERS_FLAG = FALSE

strMessage = strMessage & strResult



end if





loop



if ALL_SERVERS_FLAG = TRUE then

wscript.echo "All servers are Fine"

objFSOFile.writeline "All CS [C: > 1 GB] [D:> 1 GB] [L: > 5 GB]"

objFSOFile.writeline "All DS [C: > 1 GB] [D:> 1 GB] [F: > 5 GB] [L: > 5 GB]"

objFSOFile.writeline "All DAP [D: > 1 GB]"

else



objFSOFile.writeline vbcrlf &" The following servers were identified"

objFSOFile.writeline strMessage

wscript.echo

wscript.echo "Attention!!. Below are less than the threshold"

wscript.echo "**********************************************"

wscript.echo

wscript.echo " Server " & vbtab& "Drive"& vbtab& "Free Space (GB)"

wscript.echo "============" & vbtab& "====="& vbtab& "==============="

wscript.echo strMessage

wscript.echo "Press any key to exit."

wscript.stdin.readline

end if



objFSOFile.close

set objFSOFile = Nothing

objFile.close

set objFile = Nothing

set fso = nothing



wscript.echo "Disk info written in to "&strOutFile





'----------------------------------------------------------------------------------------

'Function : getDriveInfo

'Input : Server Name

'Output : None - (it simply writes the space info of the server in to the outFile

'

'Dependencies:

'It works perfectly for the correct login credentials

'If either of the login credential fails, displays a msg as "Access Denied"

'----------------------------------------------------------------------------------------



public function getDriveInfo(strserver)



on error resume next



strOutput =""



'Assign the input argument to a local variable

strComputer = strserver



'Create required objects which connects to remote server

Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")

Set objSWbemServices = objSWbemLocator.ConnectServer(strComputer,"root\cimv2",strUser,strPassword)



if err.number <> 0 then

wscript.echo

wscript.echo "########################################################"

wscript.echo "Error:"&err.description&vbcrlf&"Error Number:"&err.number

wscript.echo "Check if you have given correct login credentials"

wscript.echo "########################################################"

wscript.echo

wscript.echo "Press any key to exit"

wscript.stdin.Readline

getDriveInfo="Error"

exit function

end if



'wscript.echo

'wscript.echo strComputer

'wscript.echo "=========="

Set colDisks = objSWbemServices.ExecQuery("Select * from Win32_LogicalDisk")



ALERT_FLAG = FALSE



'Loop through the collection retrived by colDisks object

For Each objDisk in colDisks

Select Case objDisk.DriveType



'3 represents a Hard Drive

Case 3



'Rounding of to 2 decimal places. Dividing by 1024^3 to have in GB as unit of disk size.

strFreeSpace = Round((objDisk.FreeSpace/(1024*1024*1024)),2)

strTotalSpace = Round((objDisk.Size/(1024*1024*1024)),2)



'Check CS and DS - C drive should be > 1 GB

if instr(1,strComputer,"imr")<> 0 or instr(1,strComputer,"imc") <> 0 then

if objDisk.DeviceID="C:" and cint(strFreeSpace) < 1 then

ALERT_FLAG = TRUE

strOutValue = strOutValue & vbcrlf & strComputer& vbtab& objDisk.DeviceID&vbtab& strFreeSpace

end if

end if



'Check CS G drive should be > 5 GB

if instr(1,strComputer,"imc")<> 0 then

if objDisk.DeviceID="G:" and cint(strFreeSpace) < 5 then

ALERT_FLAG= TRUE

strOutValue = strOutValue & vbcrlf & strComputer& vbtab& objDisk.DeviceID&vbtab& strFreeSpace

end if



if objDisk.DeviceID="I:" and cint(strFreeSpace) < 5 then

ALERT_FLAG= TRUE

strOutValue = strOutValue & vbcrlf & strComputer& vbtab& objDisk.DeviceID&vbtab& strFreeSpace

end if

end if



'Check CS,DS and DAP - F drive should be > 1 GB

if instr(1,strComputer,"imr")<> 0 or instr(1,strComputer,"imc") or instr(1,strComputer,"imd") <> 0 then

if objDisk.DeviceID="D:" and cint(strFreeSpace) < 1 then

ALERT_FLAG= TRUE

strOutValue = strOutValue & vbcrlf & strComputer& vbtab& objDisk.DeviceID&vbtab& strFreeSpace

end if

end if



if instr(1,strComputer,"imr")<> 0 or instr(1,strComputer,"imb") <> 0 then

if objDisk.DeviceID="F:" and cint(strFreeSpace) <= 5 then

ALERT_FLAG= TRUE

strOutValue = strOutValue & vbcrlf & strComputer& vbtab& objDisk.DeviceID&vbtab& strFreeSpace

end if

end if



if instr(1,strComputer,"imc")<> 0 then

if objDisk.DeviceID="L:" and cint(strFreeSpace) < 5 then

ALERT_FLAG= TRUE

strOutValue = strOutValue & vbcrlf & strComputer& vbtab& objDisk.DeviceID&vbtab& strFreeSpace

end if

end if







End Select

Next



if ALERT_FLAG = TRUE then

getDriveInfo=strOutValue

else

getDriveInfo="Fine"

end if



set objSWbemLocator=Nothing

set objSWbemServices=Nothing





End function

No comments:

Post a Comment