Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Create Schedule Sample

 
Logged in as: Guest
arrSession:exec spGetSession 2,16,28317
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> Post a VBScript >> Create Schedule Sample
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Create Schedule Sample - 11/24/2005 4:55:08 PM   
  yrh4401

 

Posts: 4
Score: 0
Joined: 11/24/2005
Status: offline
'**************************************************************************************************
'* File           : Schedule.vbs
'* Created Date   : 2004/12/22
'* Author         : Ray Yen
'* Version        : 1.0
'*
'* Main Function:  Create Schedule
'*
'* Usage: For Example : cscript Schedule.vbs
'
'Note : Win32_ScheduledJob
'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_scheduledjob.asp
'http://msdn.microsoft.com/library/en-us/wmisdk/wmi/create_method_in_class_win32_scheduledjob.asp?frame=true
'uint32 Create(
'  string Command,
'  datetime StartTime,
'  boolean RunRepeatedly,
'  uint32 DaysOfWeek,
'  uint32 DaysOfMonth,
'  boolean InteractWithDesktop,
'  uint32 JobId
');
'**************************************************************************************************
Dim wmiLocator, wmiNameSpace
Dim objTasks, Inst,retVal
Dim objWMIService,colItems
Dim strComputer
Dim strDate,strTime
Dim strYear,strMonth,strDay,strHour,strMinute,strSecond
Dim strMessage
Dim strStartDateTime 'Job start date & time
Dim strCommand
Dim strDaysofMonth
Dim strTestMode
strTestMode = True      'Show Debug Message or not!
Const OverwriteExisting = True
CONST ForReading    = 1   
CONST ForWriting    = 2   
CONST ForAppending    = 8  
Dim TabStop
Dim NewLine
Dim srcFolder
Dim destFolder
Dim strLogFileName
strLogFileName = "C:\NSC_Schedule.Log"


'Check the Log file exist or not. If not create a new file and create a new schedule
If Not fnChkFileExist(strLogFileName) Then

  'Call fnCreateFile(strLogFileName)
  Call Main()    
  Call ChangeATServiceAccount("fareast\rayyen","Winword2003")
End If

Wscript.Quit
'******************************
' Main Function
'******************************
Sub Main()

' Object used to get the Cimv2 Namespace
Set wmiLocator = CreateObject("WbemScripting.SWbemLocator")

If err.number <> 0 Then
 PrintDebug "Error creating locator object" & vbCRLF & err.number & ": " & err.description
Else
 PrintDebug "Success creating locator object"
End If

' Win32 Provider lives in root\cimv2 namespace.
Set wmiNameSpace = wmiLocator.ConnectServer ("", "root\cimv2")

If err.number <> 0 Then
 PrintDebug "Error connecting to cimv2 namespace" & vbCRLF & err.number & ": " & err.description
Else
 PrintDebug "Success connecting to cimv2 namespace" 
End If

Set objTask = wmiNameSpace.Get("Win32_ScheduledJob")

If err.number <> 0 Then
 PrintDebug "Error getting instances of Schduled Job" & vbCRLF & err.number & ": " & err.description
Else
 PrintDebug "Success getting instances of Schduled Job"  
End If

'strCommand = "C:\winnt\notepad.exe"
'strCommand = "\\gtsctcwsssvr\share\data\runcommand2.vbs"

'Method 1 : Use logon script to copy the cmd to client c:\
'strCommand = "C:\RunSchedule.Cmd"

'Method 2 : Remote call the cmd file 
strCommand = "\\gtsctcwsssvr\share\data\RunSchedule.Cmd"

'strStartDateTime = "20040828153000.000000+480"
strStartDateTime = "********193000.000000+480"
strDaysofMonth = 1073741824

retVal = objTask.Create(strCommand,strStartDateTime,True,1 or 2 or 4 or 8 or 16 or 32 or 64,,True,JobID)

Select Case retVal
Case 0
 strMessage = "The request is accepted."
Case 1
 strMessage = "The request is not supported."
Case 2
 strMessage = "The user does not have the necessary access."
Case 8
 strMessage = "Interactive Process."
Case 9
 strMessage = "The directory path to the service executable file cannot be found."
Case 21
 strMessage = "Invalid parameters have been passed to the service."
Case 22
 strMessage = "The account that this service runs under is invalid or lacks the permissions to run the service."
End Select

PrintDebug "Creating Schedule Job Result : " & strMessage
'JobID : The Return Value
PrintDebug "Creating Schedule Job ID : " & JobID
End  Sub
'******************************
'Use Sc query to get service list
'Change Method of the Win32_Service Class
'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/change_method_in_class_win32_service.asp
'uint32 Change(
'  string DisplayName,
'  string PathName,
'  uint32 ServiceType,
'  uint32 ErrorControl,
'  string StartMode,
'  boolean DesktopInteract,
'  string StartName,
'  string StartPassword,
'  string LoadOrderGroup,
'  string LoadOrderGroupDependencies,
'  string ServiceDependencies
');
'******************************
Sub ChangeATServiceAccount(strAccount,strPassword)
Dim strComputer
Dim objWMIService,colServiceList,objservice
strComputer = "." 

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")  
Set colServiceList = objWMIService.ExecQuery("Select * from Win32_Service")
 
For Each objservice in colServiceList
    
    PrintDebug VbCfLf
    PrintDebug "objService.Name : " & objService.Name
    PrintDebug "objService.DisplayName : " & objService.DisplayName
    PrintDebug "objService.Caption : " & objService.Caption
    PrintDebug "objService.Description : " & objService.Description
    PrintDebug "objService.PathName : " & objService.PathName
    PrintDebug "objService.ServiceType : " & objService.ServiceType
    PrintDebug "SobjService.StartMode : " & objService.StartMode
    PrintDebug "objService.DesktopInteract: " & objService.DesktopInteract
    PrintDebug "objService.StartName : " & objService.StartName
    PrintDebug "objService.State : " & objService.State
    PrintDebug "objService.Status : " & objService.Status
    PrintDebug "objService.ErrorControl : " & objService.ErrorControl
   
    'PrintDebug "objService.StartPassword : " & objService.StartPassword
    'PrintDebug "objService.LoadOrderGroup : " & objService.LoadOrderGroup
    'PrintDebug "objService.LoadOrderGroupDependencies : " & objService.LoadOrderGroupDependencies
    'PrintDebug "objService.ServiceDependencies : " & objService.ServiceDependencies
    PrintDebug VbCfLf
   
 'If objService.Startname = ".\Schedule" Then
 
 If objService.Name = "Schedule" Then
 
     PrintDebug "Got Schedule Service!"
    
  'errReturn = objService.Change(objService.DisplayName,objService.PathName, _
  '   objService.ServiceType,objService.ErrorControl,objService.StartMode, _
  '   objService.DesktopInteract,strAccount,strPassword)      
     'objService.LoadOrderGroup,objService.LoadOrderGroupDependencies,objService.ServiceDependencies)     
  errReturn = objService.Change( , , , , , _
     ,strAccount,strPassword)      
  PrintDebug "errReturn = " & errReturn
  
  Exit For
 End If
 
Next

If errReturn <> 0 Then
   Err.Clear 
   PrintDebug "Change Schedult Excute Account Failed!"
Else
   PrintDebug "Change Schedult Excute Account Successed!"
End If
End Sub
'******************************
'JobDeleteAllAT
'******************************
Function JobDeleteAllAT(ComputerName,User,Password)
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colScheduledTasks = objWMIService.ExecQuery _
 ("Select * from Win32_ScheduledJob")
For Each objTask in colScheduledTasks
 intJobID = objTask.JobID
 Set objInstance = objWMIService.Get _
  ("Win32_ScheduledJob.JobID=" & intJobID)
 objInstance.Delete
Next

End Function

'=============== Function Area ===========================
'
'=============== Function Area ===========================
Sub PrintScreen(strMessage)
Wscript.Echo strMessage
End Sub
Sub PrintDebug(strMessage)
If strTestMode=True Then
 Wscript.Echo strMessage
End If
End Sub
Sub WriteLine(strLine)
If IsObject(objLogFile) Then
 objLogFile.WriteLine strLine
End If 
End Sub

'******************************
' Create log file function
'******************************
Sub fnCreateFile(strFilePath) 
Dim objFSO
Dim objFile
Set objFSO = CreateObject("Scripting.FileSystemObject")     
     set objFile=objFSO.OpenTextFile(strFilePath ,ForAppending, true)
         objFile.WriteLine "ÕˆÎð„h³ý´Ë™n°¸£¡MIS¾´ÉÏ£¡"
Set objFSO=nothing
 
End Sub
'******************************
' File checking function
'******************************
Function fnChkFileExist(strFileName)
Dim objFSO
Dim blnFlag
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(strFileName) Then
 blnFlag = True
Else
 blnFlag = False
End If
Set objFSO=nothing

fnChkFileExist = blnFlag
End Function
'******************************
' File copy function
'******************************
Sub fnCopyFile(strSrcFile,strDestFolder) 
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
     objFSO.CopyFile strSrcFile,strDestFolder & "\",OverwriteExisting     
Set objFSO=nothing
 
End Sub
 
 
Post #: 1
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> Post a VBScript >> Create Schedule Sample Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts