Monday, November 9, 2009

Disk Space Checking using VB Script

0 comments
'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

Server Status Checking - VB Script

0 comments
'==========================================================================


'NAME : System-UPTIME.vbs *

'AUTHOR: Arumugam
'Date : 12/21/2008 *



'Description: *

'============ *

' Reads a text file of computer names and gathers speicfied *

' performance data on each one at the interval defined And *

' repeats until it reaches the user-defined variable limit *



'Requirements: *

'============= *

' A text file that has 1 computername per line must be saved in *

' the path defined by the sPath variable *

' *

'PLS CHANGE THE FOLLOWING AS PER YOUR NEEDS *

'========================================== *

'Domain = Domain on which servers are present *

'flog = Location of output file *

'strPath = Location of servers.txt file *

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





t1=time





On error resume next



'Constant declarations for File I/O

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

Const ForReading = 1

Const ForWriting = 2

Const ForAppending = 8



' File Path Defintion "Pls change this as per your requirements"

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



'Path of output file

flog = "D:\Temp\Scripts\Server-Stat- check\UpTime-Results.txt"



'Path of servers.txt file

strPath = "D:\Temp\Scripts\Server-Stat- check\Servers.txt"



'Domain of the servers

Domain="domain1"



'Get Login Credentials

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

Wscript.StdOut.Write "Please enter your user ID:"

strID = Wscript.StdIn.ReadLine



'Append domainname (eg: domain) infront of userid

strUser =Domain&"\"&strID



Set objPassword = CreateObject("ScriptPW.Password")

Wscript.StdOut.Write "Please enter your domain password:"

strPassword = objPassword.GetPassword()

Wscript.Echo

wscript.echo



'Instantiate File System Object

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

set fs = CreateObject("Scripting.FileSystemObject")

set objlog = fs.createtextfile(flog)





'Check if input file exists

if fs.FileExists(strPath) then



'open the servers.txt file for reading server names

set fsInput = fs.OpenTextFile(strPath,ForReading)

objlog.writeline " Server " & vbtab & vbtab & " Last Boot Time " & vbtab & " Active since ?"

objlog.writeline "==========" & vbtab & vbtab & " =================" & vbtab & " =============="



do until fsInput.AtEndOfStream

strComputer=fsInput.readline

'Instantiate the SWbemLocator if remote server s pingable

if PingTest(strComputer,1,1000)="Pass" then

Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")



'Connect to server

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





if err.number = -2147024891 then

wscript.echo err.description

wscript.echo "Invalid login. Please check ur login credentials"

wscript.echo

wscript.echo

wscript.echo "Press any key to exit..!"



wscript.stdin.readline

wscript.quit

end if



'Execute

Set colOperatingSystems = objSWbemServices.ExecQuery("Select * from Win32_OperatingSystem")



For Each objOS in colOperatingSystems



dtmBootup = objOS.LastBootUpTime

dtmLastBootupTime =WMIDateStringToDate(dtmBootup)

dtmSystemUptime = DateDiff("h",dtmLastBootUpTime, Now)

objlog.writeline strComputer & vbtab &vbtab & dtmLastBootupTime & vbtab & vbtab & dtmSystemUptime & "Hours"

Next



set colOperatingSystems=nothing

set objSWbemServices = nothing

set objSWbemLocator = nothing



Else



objlog.writeline strComputer & vbtab &vbtab & " Not Pingable "

End if





loop

else

msgbox "No Input file found"



End if



msgbox "Completed. Results at " &flog&vbtab&datediff("s",t1,time)





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

'FUNCTION NAME:WMIDateStringToDate(dtmBootup)

'Description: converts the systemdate string to Date format string

'

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



Function WMIDateStringToDate(dtmBootup)



WMIDateStringToDate = CDate(Mid(dtmBootup, 5, 2) & "/" & _

Mid(dtmBootup, 7, 2) & "/" & Left(dtmBootup, 4) _

& " " & Mid (dtmBootup, 9, 2) & ":" & _

Mid(dtmBootup, 11, 2) & ":" & Mid(dtmBootup, _

13, 2))



End Function







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

'FUNCTION NAME:IsReachable(sComputer, #Pings,TimeOut)

'Description: Attempts to ping each computer name. If successful, Function

' equals true; if ping fails, function equals false.

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

Function PingTest(sComputer, iPings, iTimeOut)

Dim sCommand

Dim oShell

Dim oResult



sCommand="ping -n " & iPings & " -w " & iTimeOut & " " & sComputer



Set oShell = CreateObject("Wscript.Shell")

If iPings = "" Then iPings = 2

If iTimeOut = "" Then iTimeOut = 500

Set oResult = oShell.Exec(sCommand)

Select Case InStr(oResult.StdOut.Readall, "TTL=")

Case 0

PingTest="Fail"

Case Else

PingTest="Pass"



End Select

End Function

Connecting to Database using VB Script

0 comments
Query = "Select count(*) as ab from item"



ConnectionString = "DRIVER={SQL SERVER};SERVER=servername;Database=dbname;User Id=user123;Password=pass123;"



Set DBConnection = CreateObject("ADODB.Connection")

Set DBRows = CreateObject("ADODB.Recordset")





'* Open connection

DBConnection.Open connectionString



if err.number <> 0 then

msgbox "Connection Failed"& vbcrlf & err.description

end if





'* Execute SQL

Set DBRows = DBconnection.Execute(Query)



if err.number<> 0 then

msgbox "Querying Failed"& vbcrlf & err.description

end if



msgbox DBRows("ab")



'While Not DBRows.EOF



'Msgbox DBRows.Fields.Value



'DBrows.MoveNext

'Wend



DBconnection.Close

Set DBRows = Nothing

Set DBconnection = Nothing

Get the Drivers List in a PC - VBS

0 comments
strLog = Replace(Wscript.ScriptFullname,Wscript.Scriptname,"")&"GetDrivers.log"



set FSO = CreateObject("Scripting.Filesystemobject")



set Logs = FSO.createTextFile(strLog)



Const HKEY_LOCAL_MACHINE = &H80000002



strComputer = "."



Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")



strKeyPath = "SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers"

objRegistry.EnumValues HKEY_LOCAL_MACHINE, strKeyPath, arrValueNames, arrValueTypes



For i = 0 to UBound(arrValueNames)

strValueName = arrValueNames(i)

objRegistry.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue

Logs.writeline arrValueNames(i) & " -- " & strValue

Next



Logs.close

set Logs = Nothing

set FSO = Nothing

set objRegistry = Nothing

Monitor the services of a remote server - VBS

0 comments
'************************************************************************************************


'File Name : Services_Checker.vbs

'Description : Connects to a Server and checks if the XXX services are running

'Author : Arumugam
'Date : 09/07/2008

'Version : 1.0

'************************************************************************************************

on error resume next

wscript.echo

wscript.echo vbcrlf & " Services Checker"

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



logFile=replace(wscript.scriptfullname,wscript.scriptname,"")&"\Services.log"



set FSO = createobject("scripting.filesystemobject")

set objLog = FSO.createtextfile(logfile)



'Get the User ID

Wscript.StdOut.Write "Please enter your user name ( eg: user123):"

strUser = Wscript.StdIn.ReadLine

strUser = "DOMAIN\"&strUser



'Get the Password

Set objPassword = CreateObject("ScriptPW.Password")

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

strPassword = objPassword.GetPassword()

Wscript.Echo



if len(struser)<>12 or len(strPassword)<>8 then

msgbox"Check your Login Credentials"

wscript.quit

end if



objLog.writeline "################################################################################"

objLog.writeline "EXECUTION STARTED AT : " & now & " BY : " & strUser & vbcrlf



wscript.echo "Checking  Services on the Servers.Please wait..."&vbclrf



MonitorGAPBEE ("servername")


objLog.writeline "EXECUTION COMPLETED AT : " & now

objLog.writeline "################################################################################"

objLog.close

set objLog = Nothing

set FSO = Nothing



Public Function MonitorGAPBEE(strServer)



on error resume next



objLog.writeline "SERVER NAME:"& strServer

objLog.writeline "========================="



'Server to be checked

strComputer =strServer



'Connect to WMI of the Remote server

Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")

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



'Check if connection is successful

if err.number <> 0 then

objLog.writeline "Not connected"

objLog.writeline "********************************************"

objLog.writeline "Error occurred" &vbcrlf& err.description

objLog.writeline "********************************************"

objLog.writeline "SCRIPT IS ENDING NOW"

objLog.writeline "################################################################################"

wscript.echo "Error occurred" &vbcrlf& err.description

wscript.quit

else

objLog.writeline "connected"

end if



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

wscript.echo strComputer & " Connected"

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



'Query the WMI for services

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



if err.number <> 0 then

objLog.writeline "WMI Query Failed" & vbcrlf& err.description&vbcrlf

else

objLog.writeline "WMI Query - Success"&vbcrlf

end if





'Set the Flag

Service_Flag = TRUE





objLog.writeline "Checking for abc services"

objLog.writeline "========================="&vbcrlf





'Check each of the services

For Each objDisk in colDisks



Service_Name = objDisk.displayname

Service_Status = objDisk.state





'Check the status of abc services

if instr(Service_Name,"abc") <> 0 and Service_Status<>"Running"then



strService = strService&vbcrlf&Service_Name

Service_Flag = FALSE



do until len(Service_Name) = 45

Service_Name = Service_Name&" "



loop



wscript.echo Service_Name & vbtab & Service_Status

objLog.writeline Service_Name & vbtab & Service_Status



elseif instr(Service_Name,"ULZ") <> 0 and Service_Status="Running"then



do until len(Service_Name) = 45

Service_Name = Service_Name&" "



loop

wscript.echo Service_Name & vbtab & Service_Status

objLog.writeline Service_Name & vbtab & Service_Status



end if





Next



'Check the Flag and display the msg



if Service_Flag = TRUE then



objLog.writeline "All the ULZ services are running fine"



else

WSCRIPT.ECHO

WSCRIPT.ECHO "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"

wscript.echo "Please check the following services"& strService

WSCRIPT.ECHO "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"

WSCRIPT.ECHO



objLog.writeline "Following abc services are not running"

objLog.writeline "======================================"&strService



end if







'Destruct the used objects

Set colDisks=nothing

Set objSWbemServices = nothing

Set objSWbemLocator = nothing



objLog.writeline "**************************************************************"



end function



wscript.echo "Completed. Press any key to exit"

strUser = Wscript.StdIn.ReadLine