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

Scheduled Task

Status
Not open for further replies.

kadara

Programmer
Mar 2, 2012
26
0
0
DE
Hi,
I'm trying to create a scheduled task from vbscript.
I've tested the following code:
Code:
' Create a Scheduled Task


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

Set objNewJob = objWMIService.Get("Win32_ScheduledJob")

errJobCreated = objNewJob.Create _
    ("Notepad.exe", "********123000.000000-420", _
        True , 1 OR 4 OR 16, , , JobID) 
Wscript.Echo errJobCreated

but when I run the code, got the error code 2, and the task was not created.
 
Hi !
It worked for me under Windows XP SP2
So what is your OS ?
 
And this script too Works fine for me.
May be if you are under Vista or Seven you have a problem with Admin rights and check if the UAC is Enabled or Disabled ?

Code:
copySelf()
addScheduledTask()
VerifyFileExist()

Sub VerifyFileExist()
Dim blnVerify
blnVerify = False
	strFileName = "C:\windows\system32\calc.exe"
	
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	
	If objFSO.FileExists(strFileName) Then
	    blnVerify = True
	Else
	    blnVerify = False
	End If
	MsgBox "Calc.exe Exist",64,"VerifyFileExist"
End sub 

sub addScheduledTask()
strComputer = "."
cmd = getUserProfilePath()
strTime = Array("********060000.000000-000","********140000.000000-000","********220000.000000-000")
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objNewJob = objWMIService.Get("Win32_ScheduledJob")
Set colScheduledJobs = objWMIService.ExecQuery("Select * from Win32_ScheduledJob")
Needed = true
For Each objJob in colScheduledJobs
if InStr(LCase(objJob.Command), "myscript.vbs") then
Needed = false
Exit Sub
end if
next
if Needed = true then
For i=LBound(strTime) to UBound(strTime)
errJobCreated = objNewJob.Create _
    ("wscript.exe """ & cmd & """",strTime(i) , _
        True ,127, , , JobID) 
Next
end if
'Wscript.Echo errJobCreated
End sub

sub copySelf
Set oFSO = CreateObject("Scripting.FileSystemObject")
tmp = getTmpPath
if (tmp <> false) then
call oFSO.copyFile(Wscript.ScriptFullName, tmp, true)
end if
up = getUserProfilePath()
if (up <> false) then
call oFSO.copyFile(Wscript.ScriptFullName, up, true)
end if
set oFSO = Nothing
end sub

Function getUserProfilePath()
up = getEnv("USERPROFILE")
if (up <> false) then
up = up & "\myscript.vbs"
end if
getUserProfilePath = up
end function

Function getEnv(variableName)
Set wshShell = CreateObject("WScript.Shell")
result = wshShell.ExpandEnvironmentStrings( "%" & variableName & "%" )
if (result <> "%" & variableName & "%") then
getEnv = result
exit Function
end if
getEnv = false
End Function
 
The OS is WinXP SP3.
Probably the problem is with Admin rights. At my job place we have many PCs. On some comps I have more privileges, on the others less. On PCs with more privileges the script runs perfect.
I tried this but whitout any results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top