Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


help with Win32_ScheduleJob and WMI

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

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> help with Win32_ScheduleJob and WMI
  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 >>
 help with Win32_ScheduleJob and WMI - 1/11/2007 8:54:49 AM   
  Juggz675


Posts: 8
Score: 0
Joined: 12/14/2006
Status: offline
Hi all,

I'm trying to schedule a task to execute, but I have some missing or incorrect syntax.
I can schedule it, but it is for the next day.

I would like to be able to run it once the task is scheduled.

Here is the code.

It needs some clean up , I just want to make it work correctly.

dim WshShell
set WshShell = WScript.CreateObject("WScript.Shell")

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
   & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objNewJob = objWMIService.Get("Win32_ScheduledJob")

'get time to run
dim ttm
ttm = Time()

'Strip out the seconds
Dim TimePart
TimePart = Left(ttm, Len(ttm) -3)

'strip out the time stuff
dim myhour
dim min
dim sec

myhour = left(TimePart, 2)
WScript.Echo("first Occ :-: " &  instr( 1, TimePart, ":") )
  
myhour = left( TimePart, instr(1, TimePart, ":") -1)

if myhour < 10 then
 myhour = "0" & myhour
end if

TimePart = right(TimePart, len(TimePart) - instr( 1, TimePart, ":") )

min = left( TimePart, instr(1, TimePart, ":") -1)

TimePart = right(TimePart, len(TimePart) - instr( 1, TimePart, ":") )
sec = TimePart'left( TimePart, instr(1, TimePart, ":") -1) 

TimeToRun = myhour & min+1 & sec

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
   
dim time_constraint
time_constraint = "********" & TimeToRun & ".000000-420"
    
Set objNewJob = objWMIService.Get("Win32_ScheduledJob")     
       errJobCreated = objNewJob.Create _
     ("sysocmgr /i:C:\Windows\inf\sysoc.inf /u:C:\Rescue21\install\reinsnmp.txt /r", _
      time_constraint, True , , , , JobID)
Wscript.Echo errJobCreated

Any help is greatly appreciated.
 
 
Post #: 1
 
 RE: help with Win32_ScheduleJob and WMI - 1/11/2007 11:32:00 AM   
  dm_4ever


Posts: 2722
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
Is the schedule going to run once or every day or only once?

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to Juggz675)
 
 
Post #: 2
 
 RE: help with Win32_ScheduleJob and WMI - 1/12/2007 12:32:56 AM   
  SAPIENScripter


Posts: 276
Score: 2
Joined: 11/1/2006
From: SAPIEN Technologies
Status: offline
Be aware that this class is really a legacy setting for AT.  Jobs created with this class aren't really a part of Scheduled Tasks. The documentation for this class is at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/Win32_ScheduledJob.asp.

_____________________________

Jeffery Hicks
Windows PowerShell MVP
SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com

Follow Me: http://www.twitter.com/JeffHicks

(in reply to dm_4ever)
 
 
Post #: 3
 
 RE: help with Win32_ScheduleJob and WMI - 1/12/2007 12:57:31 AM   
  gdewrance


Posts: 587
Score: 3
Joined: 3/16/2006
Status: offline
Here's a script I wrote for scheduled Tasks, hope it's useful


      

(in reply to Juggz675)
 
 
Post #: 4
 
 RE: help with Win32_ScheduleJob and WMI - 1/12/2007 1:12:04 AM   
  SAPIENScripter


Posts: 276
Score: 2
Joined: 11/1/2006
From: SAPIEN Technologies
Status: offline
Another approach, especially if you want the task to show up in Scheduled Tasks and be modifiable, is to simply call SCHTASKS.EXE from your VBScript (assuming you are running XP or 2003). I think it's much easier than working with the WMI class.  Run schtasks /create /? to see the syntax.

_____________________________

Jeffery Hicks
Windows PowerShell MVP
SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com

Follow Me: http://www.twitter.com/JeffHicks

(in reply to gdewrance)
 
 
Post #: 5
 
 RE: help with Win32_ScheduleJob and WMI - 1/12/2007 5:14:57 AM   
  Juggz675


Posts: 8
Score: 0
Joined: 12/14/2006
Status: offline
I've tried using the schtasks command.
It logs the task, but does not run correctly, ie, reinstalling snmp.

hummmmm

As I posted earlier, I can run the script locally and it is fine, using WMI.
I believe it is a security permission from the remote entry, which has the problem.
But not sure how to check the permissions...

(in reply to SAPIENScripter)
 
 
Post #: 6
 
 RE: help with Win32_ScheduleJob and WMI - 1/12/2007 9:51:46 AM   
  dm_4ever


Posts: 2722
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
I did a little cleaning and modifying.  Try this. Try specifying a time a few minutes ahead of your current. Make sure you modify the line

strDays = GetDOW(Array("monday","tuesday","wednesday","thursday","friday","saturday","sunday"))

to the specific day you want it to run or set it to : strDays = ""  OR strDays = GetDOW(Array("")) to run once. NOTE: If the time you specify is past the current time, it will schedule the Run Once for the next day.


      

< Message edited by dm_4ever -- 1/12/2007 9:59:37 AM >


_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to Juggz675)
 
 
Post #: 7
 
 RE: help with Win32_ScheduleJob and WMI - 1/15/2007 9:09:50 AM   
  Juggz675


Posts: 8
Score: 0
Joined: 12/14/2006
Status: offline
Thanks for the input.
I'll give it a try.

It appears to be a permission thing.
I can uninstall the service, start/stop the service, but when it comes time to reinstall it, forget it.

I believe when I try to connect to the remote host and run the script, I'm not being granted admin or system rights.
So maybe that is what I need to pursue.

(in reply to dm_4ever)
 
 
Post #: 8
 
 RE: help with Win32_ScheduleJob and WMI - 1/15/2007 10:02:29 AM   
  dm_4ever


Posts: 2722
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
One thing to remember when creating a scheduled task with WMI is that it will run it under "System". 

SCHTASKS.EXE (like SapienScripter suggested) will let you specify a username and password under which to run the scheduled task.

See the options available by typing the following at the command prompt:  schtasks /create /?

< Message edited by dm_4ever -- 1/16/2007 2:56:40 AM >


_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to Juggz675)
 
 
Post #: 9
 
 RE: help with Win32_ScheduleJob and WMI - 1/15/2007 4:59:08 PM   
  gdewrance


Posts: 587
Score: 3
Joined: 3/16/2006
Status: offline
quote:

I can uninstall the service, start/stop the service, but when it comes time to reinstall it, forget it.

Are you creating a Service or a Scheduled Job?

(in reply to Juggz675)
 
 
Post #: 10
 
 RE: help with Win32_ScheduleJob and WMI - 1/16/2007 8:36:48 AM   
  Juggz675


Posts: 8
Score: 0
Joined: 12/14/2006
Status: offline
quote:

schtasks /create /?

Already tried this also.  It does not want to run the command.


Trying to create a scheduled job, that only needs to run once.
Is there a difference between a scheduled task or scheduled job when running once?

< Message edited by Juggz675 -- 1/16/2007 8:38:36 AM >

(in reply to gdewrance)
 
 
Post #: 11
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> help with Win32_ScheduleJob and WMI 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