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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Concatenate strings in WMI task scheduler

Status
Not open for further replies.

cluM09

Technical User
May 15, 2004
127
US
I have a script to schedule a task to run on a machine with a random time of the day that was defined with a variable, but I cannot concatenate the strings in the command line syntax for the WMI task scheduler. The code is listed below:

Const MONDAY = 1
Const TUESDAY = 2
Const WEDNESDAY = 4
Const THURSDAY = 8
Const FRIDAY = 16
Const SATURDAY = 32
Const SUNDAY = 64

Const EST = -300
Const CST = -360
Const MST = -420
Const PST = -480

strHour = 055400 'This time may vary.

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objNewJob = objWMIService.Get("Win32_ScheduledJob")
errJobCreated = objNewJob.Create _
("Notepad.exe", "********" & strHour & ".000000-" & CST, _ True , 1 Or 4 Or 16, , , JobID)
WScript.Echo errJobCreated

When I run this script, I get the error "SWbemObjectEx: Type mismatch."

Any help with this concatenation problem will be greatly appreciated.

Thanks!
 
[1]
>[tt]strHour = 055400 'This time may vary.[/tt]
[tt]strHour = [red]"[/red]055400[red]"[/red] 'This time may vary.[/tt]
(note: the same is EST, etc. You can keep it only because it is 3 figure integers. Also note the repercusion of the negative sign.)

[2]
>[tt]errJobCreated = objNewJob.Create _
("Notepad.exe", "********" & strHour & ".000000[highlight]-[/highlight]" & CST, [highlight]_[/highlight] True , 1 Or 4 Or 16, , , JobID)
[/tt]
[tt]errJobCreated = objNewJob.Create _
("Notepad.exe", "********" & strHour & ".000000" & CST, True , 1 Or 4 Or 16, , , JobID)[/tt]
(note: Do you know what "_" means? It is line continuation. If you use it, the following is on a new line.)
 
tsuji,

Thank you for the response!

You are correct with respect to the strHour. The problem lies in the strHour variable with a zero in front of the number 5, which will be seen as 55400 when inserted into the command line in the code.

The line ("Notepad.exe", "********" & strHour & ".000000-" & CST, _ True , 1 Or 4 Or 16, , , JobID) that I have up there shoud be:

("Notepad.exe", "********" & strHour & ".000000-" & CST, _
True , 1 Or 4 Or 16, , , JobID)

instead since I have the underscore character after CST,. It was my bad to post it that way.

When I place the double quotes on either side of the strHour value as strHour = "055400", it works.

Thank you for the help!
 
it didn't work for me. i think it is because i don't have an admin for it to run as. how can i make it run as an admin account
 
it says that it could not start (the task) this is the exact code i am using. i am new to this so i don't understand everything.

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

Set objNewJob = objWMIService.Get("Win32_ScheduledJob")

On Error Resume next

errJobCreated = objNewJob.Create _
("maintenance.cmd", "********123000.000000-300", _
True , 1 OR 2 OR 4 OR 8 OR 16 OR 32 OR 64, , , JobID)
Wscript.Echo errJobCreated
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top