Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating Scheduled Task with WMI 2

Status
Not open for further replies.

markdmac

MIS
Dec 20, 2003
12,340
US
I am trying to create a scheduled task. My objective is to be able to pass the command, start time and days to execute as variables to a sub that builds the task. Below demonstrates what I am doing and in this case just passes the start time as a string. Problem is it keeps failing with error [red]SWbemObjectEx: Type mismatch[/red].

Inserting the same text that the function returns as a string it works. Can anyone spot what I am doing wrong here?



Code:
StartTime = "2400"
StartString = StartTxt(StartTime)
BuildTask(StartString)

Sub BuildTask(StartString)
                                strComputer = "."
                                Set objWMIService = GetObject("winmgmts:" _
                                    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
                                
                                Set objNewJob = objWMIService.Get("Win32_ScheduledJob")
                                
                                errJobCreated = objNewJob.Create _
                                    ("Notepad.exe", StartString, _
                                        False , 1 OR 4 OR 16, , , JobID) 
                                Wscript.Echo errJobCreated
End Sub

Function StartTxt(StartTime)
'First get the systems time bias
strComputer = "."
wmiQuery = "Select * from Win32_TimeZone"
Set objWMIService = GetObject("winmgmts:\\" & _
    strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery(wmiQuery)
For Each objItem in colItems
    TimeBias = objItem.Bias
Next
'Now build the time string
StartTxt = Chr(34)& "********" & StartTime & "00.000000" & TimeBias & Chr(34)
End Function

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
[1] StartTxt function

Try change these.
[tt]
Function StartTxt(StartTime)
'First get the systems time bias
strComputer = "."
wmiQuery = "Select * from Win32_TimeZone"
Set objWMIService = GetObject("winmgmts:\\" & _
strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery(wmiQuery)
For Each objItem in colItems
TimeBias = objItem.Bias
Next
[blue]if TimeBias>=0 then
TimeBias="+" & right("000 & TimeBias,3)
else
TimeBias="-" & right("000" & abs(TimeBias),3)
end if[/blue]
'Now build the time string
[highlight]'[/highlight]StartTxt = Chr(34)& "********" & StartTime & "00.000000" & TimeBias & Chr(34)
[blue]StartTxt = "********" & StartTime & "00.000000" & TimeBias [/blue]
End Function
[/tt]
[2] Sub BuildTask

[2.1] When you test it, you might want it desktop interactive for you to test. Then set the 6th parameter to true.
[2.2] If you have set the 2nd parameter to false, then the 3rd parameter is not effective, but, it is harmless though.
[2.3] Give a JobId a name if you let the parameter standout. Otherwise, just have an early close out of the parenthesis.

[3] Try take out the mental ambiguity of StartTime at 2400.
[tt] StartTime="2359"[/tt]
or
[tt] StartTime="0001"[/tt]
 
Amendment
The corresponding line contains a typo and should be read like this.
[tt] TimeBias="+" & right("000[red]"[/red] & TimeBias,3)[/tt]
 
Thank you tsuji!

When I first tested I got an error, I then changed the time from 2400 and sure enough it worked! Adding extra logic to move it a minute back if the user selects 2400. :)

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
Tsuji, here is what I now have. After adding code for the days to run I get a new error Type Mismatch error.

I believe that what is happening is even though the text is right (echoed for checking) what I believe is happening is the numbers are being interpretted as text.

Hope you can lend some more assistance. Thanks in advance.

Code:
StartTime = "2359"
StartString = StartTxt(StartTime)
WScript.Echo startstring

'Configure the days to run the script. Only change Yes/No values.
Rundays = RunTxt(Monday, "No", "1")
Rundays = Rundays & RunTxt(Tuesday, "Yes","2")
Rundays = Rundays & RunTxt(Wednesday, "No","4")
Rundays = Rundays & RunTxt(Thursday, "No","8")
Rundays = Rundays & RunTxt(Friday, "Yes","16")
Rundays = Rundays & RunTxt(Saturday, "No","32")
Rundays = Rundays & RunTxt(Sunday, "Yes","64")

If Left(Rundays,4) = " OR " Then
	Rundays = Right(Rundays,(Len(Rundays)-4))
End If

WScript.Echo Rundays


BuildTask StartString, Rundays

Sub BuildTask(StartString, Rundays)
		strComputer = "."
		Set objWMIService = GetObject("winmgmts:" _
		    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
		
		Set objNewJob = objWMIService.Get("Win32_ScheduledJob")
		
		errJobCreated = objNewJob.Create _
		    ("Notepad.exe", StartString, _
		        False , Rundays, , ,"Cleanup")
		Wscript.Echo errJobCreated
End Sub

Function StartTxt(StartTime)
If StartTime = "2400" Then StartTime = "2359"
'First get the systems time bias
strComputer = "."
wmiQuery = "Select * from Win32_TimeZone"
Set objWMIService = GetObject("winmgmts:\\" & _
    strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery(wmiQuery)
For Each objItem in colItems
    TimeBias = objItem.Bias
Next
if TimeBias>=0 then
    TimeBias="+" & right("000" & TimeBias,3)
else
    TimeBias="-" & right("000" & abs(TimeBias),3)
end If
'Now build the time string
StartTxt = "********" & StartTime & "00.000000" & TimeBias 
End Function


Function RunTxt(dayUnused,RunYesNo,DayVal)
	If Ucase(RunYesNo) = "YES" Then
	   		RunTxt = " OR " & DayVal
	Else 
		RunTxt = ""
	End If
End Function

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
Mark, if you want to build string like that (apart from slight deficiency), you have to make it evaluated, as that parameter is actually an integer resultant of some bitwise boolean operation.

I add some petit device to make it look more uniform at the start.
[tt]
'Configure the days to run the script. Only change Yes/No values.
[green]Rundays=vbEmpty[/green]
Rundays = [green]Rundays & [/green]RunTxt(Monday, "No", "1")
Rundays = Rundays & RunTxt(Tuesday, "Yes","2")
Rundays = Rundays & RunTxt(Wednesday, "No","4")
Rundays = Rundays & RunTxt(Thursday, "No","8")
Rundays = Rundays & RunTxt(Friday, "Yes","16")
Rundays = Rundays & RunTxt(Saturday, "No","32")
Rundays = Rundays & RunTxt(Sunday, "Yes","64")

'no need: look clumsy without clarity
'If Left(Rundays,4) = " OR " Then
' Rundays = Right(Rundays,(Len(Rundays)-4))
'End If

WScript.Echo Rundays [green]& vbcrlf & eval(Rundays)[/green]

BuildTask StartString, [red]eval([/red]Rundays[red])[/red]

Function RunTxt(dayUnused,RunYesNo,DayVal)
If Ucase(RunYesNo) = "YES" Then
RunTxt = " OR " & DayVal [red]& "[highlight] [/highlight]"[/red]
Else
RunTxt = ""
End If
End Function
[/tt]
But then, you pass eval(Rundays) to the BuildTask, whereas within the function, the same named variable is interpreted as an integer. This may cause confusion. Maybe better than to add a line like
[tt]Rundays=eval(Rundays)[/tt]
right after the string is built and keep the line
[tt]BuildTask StartString, Rundays[/tt]
(Or maybe make two different name for string and integer of the evaluated result. It is up to you and it is not too important.)
 
Further note:

Upon re-reading what I posted, the reason I add a space after the RunTxt with the case "Yes" is that I wanted to use RunTxt=vbEmpty for the case "No". But I finally option not to change too much, so I should have taken the add space out, (though keeping it does no harm).

Hence, function RunTxt(...) can be kept intact as the original realization.

Sorry for the distraction.
 
tsuji, how I wish the system would allow me to give you additional stars for your additional posts.

Aside from helping me to accomplish the goal, I also learned a few new tricks here as I had never seen vbEmpty before or used the Eval command before.

I am very greatful! Thank you!

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top