'******************************************************************************************* ' Mom Single Server Maint Scheduler Script ' ' 'Script submitted 4/24/2006 by Jeremy Schuller, Principal Financial Group ' ' ' uses the maintmode.exe from the SDK to enable me to schedule server for maint mode in advance leveraging the windows scheduling components ' 'The .csv file referenced contains a comma delimited list of agent-managed computers 'You can get this data from the Admin Console by expanding Administration, expand Computers, right click on Agent-managed Computers and choose Export list. 'Be sure to update UNC and local paths to fit your environment ' ' '******************************************************************************************** Option Explicit On Error Resume Next Dim ServerName Dim RunTime Dim RunDate Dim GetDate Dim ws Dim Command1 Dim Command2 Dim Command3 Dim LoopBox1 Dim LoopBox2 Dim LoopBox3 Dim LoopBox4 Dim lf Dim message Dim s Dim Title Dim fso Dim tf1 Dim tf2 Dim tf3 Dim ErrorTest Dim ServiceError Dim cnnSearch ' mom stuff Dim objFile Dim strline Dim MomOk Dim DomainName Dim MaintTime Dim MaintTimeOk Dim strWork Dim LoopBoxServer Dim LoopBoxTime lf = Chr(13) + Chr(10) Title = "MOM Maintenance Mode Scheduler" Set fso = CreateObject("Scripting.FileSystemObject") Set ws = WScript.CreateObject("WScript.Shell") function dtime if minute(time) > 54 then dtime = hour(time) + 1 &":" &"0" &minute(time) + 5 - 60 elseif minute(time) < 5 then dtime = hour(time) &":" &"0" &minute(time) + 5 else dtime = hour(time) &":" &minute(time) + 5 end if end function function ServiceStatus() message = "Name: " & s.DisplayName & lf if s.Status = 1 then message = message + "Status: Stopped." elseif s.Status = 2 then message = message + "Status: Starting..." elseif s.Status = 3 then message = message + "Status: Stopping..." elseif s.Status = 4 then message = message + "Status: Started." elseif s.Status = 5 then message = message + "Status: Continuing..." elseif s.Status = 6 then message = message + "Status: Pausing..." elseif s.Status = 7 then message = message + "Status: Paused." elseif s.Status = 8 then message = message + "Status: Error." end if message = message + lf &"Start Type: " if s.StartType = 2 then message = message + "Automatic" elseif s.StartType = 3 then message = message + "Manual" elseif s.StartType = 4 then message = message + "Disabled" end if ServiceStatus = message end function Do while LoopBox4 <> vbNo '******************************* ' Reset LoopBox4 Variables '******************************* RunTime = "" GetDate = "" RunDate = "" ' **************************************** ' Get ServerName Information and Validate '****************************************** '******************************* ' Reset LoopBoxServer Variables '******************************* LoopBoxServer = "" ServerName = "" MomOk = False DomainName = "" strWork = "" Do While (LoopBoxServer <> vbYes And ServerName = "" And MomOk <> True) ServerName = InputBox("Please enter the NAME of the server to schedule:", Title) If ServerName = "" then LoopBoxServer = Msgbox ("Would you like to exit the script?", vbYesno + Vbexclamation, Title) end if If LoopBoxServer = vbYes Then WScript.quit End If ' **************************** ' Check to see if MOM Managed ' **************************** Set objFile = fso.OpenTextFile("\\pfgdsmMAS009\H$\MOM\MOMMaintMode\AllAgents.csv",1) Do until (objFile.AtEndOfStream Or MomOk = True) strLine = objFile.ReadLine strWork = Split(strLine,",") If Ucase(strWork(0)) = Ucase(ServerName) Then MomOk = True DomainName = strWork(1) 'Wscript.Echo "The server name is " & strWork(0) 'Wscript.Echo "The domain name is " & strWork(1) End If Loop ' checking MOMMGMT02 AllAgents.csv file objFile.Close Set objFile = Nothing '****************************************** 'Send Message to User if Server Not Found '****************************************** If MomOk <> True Then Msgbox "Spelling of " &ServerName & " incorrect or " &ServerName & " Is Not managed by MOM " &lf &lf &"Please try again.", vbOkonly + vbExclamation, Title ServerName = "" err.number = 0 MomOk = False End If Loop ' Server Name - Validation loop '****************************************** ' Get Maintenance Time in minutes ' Information And Validate '****************************************** '***************************** ' Reset LoopBoxTime Variables '***************************** LoopBoxTime = "" MaintTime = "" MaintTimeOK = False err.number = 0 Do While (LoopBoxTime <> vbYes And MaintTime = "" And MaintTimeOk <> True) MaintTime = InputBox("Please enter Maintenance Mode time needed in MINUTES. Minimum is 6 minutes." &lf &lf & "A maximum value of 99999 (five 9's) Is PERMANENT maintenance mode.", Title) If MaintTime = "" then LoopBoxTime = Msgbox ("Would you like to exit the script?", vbYesno + Vbexclamation, Title) end if If LoopBoxTime = vbYes Then WScript.quit end if '***************************** ' Check for numeric input > 5 '***************************** If IsNumeric(MaintTime) Then If CInt(MaintTime) > 5 Then MaintTimeOk = True Else Msgbox "Time value must be greater than 5. " &lf &lf & "Please try again.", vbOkonly + vbExclamation, Title MaintTime = "" err.number = 0 MaintTimeOk = False End If Else Msgbox "Time value must be numeric." &lf &lf & "Please try again.", vbOkonly + vbExclamation, Title MaintTime = "" err.number = 0 MaintTimeOk = False End If ' If MaintTime is Integer Loop ' LoopBoxTime If (MomOk And MaintTimeOk) Then '****************************************** ' Check scheduler Status on MOM Mgmt Server '****************************************** Set s = GetObject("WinNT://pfgdsmMAS009/schedule,service") ErrorTest = s.Status If err.number <> 0 Then msgbox "Could not connect to PFGDSMMAS009" &lf &lf &"Please try again.", vbOkonly + vbExclamation, Title ServerName = "" err.number = 0 End If '**************************************************** ' Try and start the Scheduler service if not running '**************************************************** do while s.Status <> 4 and ServiceError <> vbCancel ServiceError = msgbox ("The Task Scheduler service is not running on PFGDSMMAS009." &lf &lf &"Please press OK to attempt to start or continue the service." &lf &lf &ServiceStatus, VbOkCancel + VBExclamation, Title) if s.Status = 1 then s.Start elseif s.Status = 2 then elseif s.Status = 3 then elseif s.Status = 5 then elseif s.Status = 6 then elseif s.Status = 7 then s.Continue elseif s.Status = 8 then msgbox "Cannot start the Task Scheduler service on PFGDSMMAS009",VbOk+ VBCritical, Title WScript.Quit end if if s.Status = 4 then msgbox "The Task Scheduler service is running on PFGDSMMAS009" &lf &lf &"Press OK to continue", VBOkOnly + vbInformation, Title end if Loop ' s.Status <> 4 if ServiceError = vbCancel then WScript.Quit end if '******************** ' Schedule the Job '******************** msgbox "Press OK to create files on PFGDSMMAS009 for " & Servername, Vbokonly + vbInformation, Title Set tf1 = fso.CreateTextFile("\\PFGDSMMAS009\H$\mom\MomMaintmode\" &ServerName &"_maint.bat", True) tf1.WriteLine ("@Echo off") tf1.WriteLine ("h:") tf1.WriteLine ("cd \Mom") tf1.WriteLine ("cd MomMaintmode") tf1.WriteLine ("maintmode -a " &DomainName & " " &ServerName & " " &MaintTime) tf1.WriteLine ("Echo " &ServerName & " > logs\" &ServerName & ".log") tf1.WriteLine ("Echo Last Maint Mode Set On %date% at %time% for " &MaintTime & " minutes. >> logs\" &ServerName & ".log") tf1.WriteLine ("del " &ServerName &"_maint.bat") tf1.Close '************************* ' Get the Date to execute '************************** Do while LoopBox2 <> vbYes and GetDate = "" GetDate = InputBox("Please enter the day of the month for maint mode to begin (i.e. 14):" & lf & lf &"The default value is today.", Title,Day(date)) If GetDate = "" then LoopBox2 = Msgbox ("Would you like to exit the script?", vbYesNo + VbExclamation, Title) end if Loop ' LoopBox2 if LoopBox2 = vbYes then WScript.quit end if RunDate = "/next:" + GetDate '******************************** ' Get the Time of Day to execute '********************************* Do while LoopBox3 <> vbYes and RunTime = "" RunTime = InputBox("Please enter the time for maint mode to start in 24 hour format (i.e. 14:30):" & lf & lf &"The default time will execute the command in 5 minutes.", Title, dtime) If RunTime = "" then LoopBox3 = Msgbox ("Would you like to exit the script?", vbYesNo + VbExclamation, Title) end if Loop ' LoopBox3 if LoopBox3 = vbYes then WScript.quit end if '******************************** ' Schedule on MomMgmt02 '********************************* Command3 = ws.run("at \\PFGDSMMAS009 " &RunTime &" " &RunDate & " H:\MOM\MomMaintMode\"&ServerName &"_maint.bat",0,"True") if Command3 = 0 then Msgbox UCase(ServerName) &" scheduled for " &RunTime &" on the next " &GetDate &".", vbOkonly + VbInformation, Title else Msgbox "Failed to create schedule for " &UCase(ServerName) &" at " &RunTime &" on the next " &GetDate &".", vbOkonly + VbCritical, Title end if LoopBox4 = Msgbox ("Would you like to schedule another server?", vbYesno + Vbquestion, Title) End If 'If MomOk And MaintTimeOk Loop ' LoopBox4