The necessity of Setting Objects to Nothing
- From: "Highlander" <tron9901@xxxxxxx>
- Date: 12 May 2006 08:07:51 -0700
Hello all. When creating scripts, I've made it a practice to set
objects to nothing as the script closes in order to free memory
resources. But what happens when you've got a script that is running as
a Windows Service (SRVANY.EXE)?
In my script I've got a "Do While" Loop that repeats every 15 minutes
(Wscript.Sleep 900000) . So the script will keep running indefinitely
until I stop the Windows Service. But when I do stop the service, the
commands that set objects to nothing won't execute - since they are
outside of the "Do While" Loop. What to do?
Here's the script:
(watch for word wrap)
....
Const HKEY_LOCAL_MACHINE = &H80000002
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const OverwriteExisting = True
Const OverWriteFiles = True
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim strCRI, Today, mm, dd, hh, mn, TimeStamp, LogFolder
Dim Servers, List, strComputer
Dim strKeyPath, strEntryName, IsOnline, Timeout
Dim LogFile, UsersVal, Threshold, AlertLog, FlagFile, Increased
Dim strSubj, strContents, SendTo1, SendTo2, SendTo3, SendTo4, SendTo
Dim CurDir
CurDir = objFSO.GetFolder(".").Path
Set Folder = objFSO.GetFolder(CurDir)
'~~~ Repeat the subroutine Query every 15 minutes.
'~~~ At midnight run the subroutine RemoveFolders to
'~~~ delete the previous day's folders.
Do While ScriptFinish <> True
IF Hour(Now) = 0 Then
RemoveFolders
End IF
Query
Wscript.Sleep 900000
Loop
Sub Query
If WeekDay(Now) = "1" Then Today = "Sun"
If WeekDay(Now) = "2" Then Today = "Mon"
If WeekDay(Now) = "3" Then Today = "Tue"
If WeekDay(Now) = "4" Then Today = "Wed"
If WeekDay(Now) = "5" Then Today = "Thu"
If WeekDay(Now) = "6" Then Today = "Fri"
If WeekDay(Now) = "7" Then Today = "Sat"
mm = Right("00" & Month(Now), 2)
dd = Right("00" & Day(Now), 2)
hh = Right("00" & Hour(Now), 2)
mn = Right("00" & Minute(Now), 2)
TimeStamp = Today & "_" & mm & "_" & dd & "___" & hh & "-" & mn
'~~~ Create a log folder to store the results of each query.
LogFolder = Today & "___" & hh & "_" & mn
IF NOT objFSO.FolderExists(LogFolder) Then _
objFSO.CreateFolder(LogFolder)
List = "List.txt"
Set ListFile = objFSO.OpenTextFile(List, 1)
Do Until ListFile.AtEndOfStream
strComputer = ListFile.ReadLine
'~~~ Use a flag file to indicate that a server has already exceeded
'~~~ the (number of users) threshold and triggered the alert.
FlagFile = CurDir & "\Flags\" & strComputer & ".txt"
'~~~ Check Registry to see if server is on the Farm.
On Error Resume Next
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &
_
strComputer & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\AppServers\HealthCheck"
strEntryName = "IsOnline"
objReg.GetDWORDValue
HKEY_LOCAL_MACHINE,strKeyPath,strEntryName,dwValue
IsOnline = dwValue
'~~~ Delete any previous flag files if the server is no longer on
the Farm.
IF NOT IsOnline = 1 Then
IF objFSO.FileExists(FlagFile) Then objFSO.DeleteFile(FlagFile)
Else
'~~~ Check Registry for the "Timeout" value to see if server is at
"Reset" status
strEntryName = "Timeout"
objReg.GetDWORDValue
HKEY_LOCAL_MACHINE,strKeyPath,strEntryName,dwValue
Timeout = dwValue
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer &
"\root\cimv2")
Set colItems = objWMIService.ExecQuery _
("Select * from Win32_PerfRawData_W3SVC_WebService where
Name='Default Web Site'")
For Each objItem in colItems
Users = objItem.CurrentAnonymousUsers
Next
On Error GoTo 0
'~~~ Write the results of the number of users query to a log file.
LogFile = LogFolder & "\" & strComputer & ".txt"
Set f = objFSO.OpenTextFile (LogFile,8,True)
f.writeline Users
f.close
'~~~ Read each server's log file, and if it has exceeded
'~~~ the (number of users) threshold then trigger the alert.
Set BoxLog = objFSO.OpenTextFile(LogFile, 1)
UsersVal = BoxLog.ReadLine
UsersVal = CInt(UsersVal)
Threshold = 900
'~~~ If the "Timeout" registry value is anything other than 0,
'~~~ then that server is at "Reset" status and will be skipped.
'~~~ Otherwise run the threshold comparison.
IF Timeout = 0 Then
IF UsersVal > Threshold Then
TriggerAlert strComputer, UsersVal
End IF
END IF
BoxLog.close
End IF ' IsOnline = 1
Loop ' Do Until ListFile.AtEndOfStream
ListFile.close
End Sub ' Query
Function TriggerAlert(strComputer,UsersVal)
AlertLog = "D:\Tasks\Anonymous_Users_Alert__" & TimeStamp & "_.txt"
'~~~ If it's non-business hours or weekends, write to the
'~~~ Alert log file only; otherwise write to the Alert log file
'~~~ AND send a message (e-mails and SKYTEL pages).
IF Weekday(Now) mod 7 < 2 Or Hour(Now) mod 18 < 9 Then
AlertLogWrite AlertLog, strComputer, UsersVal
Else
'~~~ If flag file exists write to the Alert log file only.
IF objFSO.FileExists(FlagFile) Then
AlertLogWrite AlertLog, strComputer, UsersVal
Else
AlertLogWrite AlertLog, strComputer, UsersVal
strSubj = "...Excessive Number of Users on " & strComputer & "."
strContents = "There are " & UsersVal & " users on " & strComputer
SendTo1 = "drmfs@xxxxxxxxxxxxx; 1248627@xxxxxxxxxx; "
SendTo2 = "gwila@xxxxxxxxxxxxx; 1958471@xxxxxxxxxx; "
SendTo3 = "torfn@xxxxxxxxxxxxx; 1125698@xxxxxxxxxx; "
SendTo4 = "dbron@xxxxxxxxxxxxx; NYCtech@xxxxxxxxxxxxx"
SendTo = SendTo1 & SendTo2 & SendTo3 & SendTo4
Send_Message SendTo
'~~~ Create the flag file.
Set objFlag = objFSO.OpenTextFile(FlagFile, 2, True, 0)
objFlag.WriteLine UsersVal
objFlag.Close
End IF ' objFSO.FileExists(FlagFile)
End IF ' Weekday(Now)
End Function ' TriggerAlert
Function AlertLogWrite(AlertLog,strComputer,UsersVal)
Set objAlertLog = objFSO.OpenTextFile(AlertLog, 8, True, 0)
objAlertLog.WriteLine strComputer & vbTab & UsersVal & VbCrlf
objAlertLog.Close
End Function
Sub Send_Message (sendTo)
sch = "http://schemas.microsoft.com/cdo/configuration/"
Set cdoConfig = CreateObject("CDO.Configuration")
With cdoConfig.Fields
.Item(sch & "sendusing") = 2 ' cdoSendUsingPort
.Item(sch & "smtpserver") = "mail.mycompany.com"
'.Item(sch & "smtpserverport") = 25 ' if you need to set a port
number
.update
End With
Set cdoMessage = CreateObject ("CDO.Message")
With cdoMessage
Set .Configuration = cdoConfig
.From = "Alerts-@xxxxxxxxxxxxx"
.To = sendTo
.Subject = strSubj
.TextBody = strContents
.Send
End With
Set cdoConfig = nothing
Set cdoMessage = nothing
End Sub ' Send_Message
Sub RemoveFolders
'~~~ Delete (the previous day's folders) all folders which
'~~~ have three underscores in the folder name.
strCRI ="___"
For Each subfolder In Folder.Subfolders
IF Instr(subfolder.name, strCRI) Then
s = Subfolder.path
objShell.Run "%comspec% /c RD /S /Q " & s, 0
End IF
Next
Wscript.Sleep 1800000
End Sub
Set objShell = nothing
Set objFSO = nothing
Set Folder = nothing
Set ListFile = nothing
Set objReg = nothing
Set objWMIService = nothing
Set colSettings = nothing
Set f = nothing
Set BoxLog = nothing
Set objFlag = nothing
Set objAlertLog = nothing
.
- Follow-Ups:
- Re: The necessity of Setting Objects to Nothing
- From: Anthony Jones
- Re: The necessity of Setting Objects to Nothing
- Prev by Date: Remote Assistance
- Next by Date: Re: Copy a recordset (multiple rows) into an array
- Previous by thread: Remote Assistance
- Next by thread: Re: The necessity of Setting Objects to Nothing
- Index(es):
Relevant Pages
|
|