Jump to content

Setting A Task Remotely Using Windows Task Scheduler


Recommended Posts

Posted

you could create the task from a vbscript.

You could you your CMD file or run bginfo directly and just have the switches set in the task. This will check for the existence of a scheduled task that has bginfo.exe in the task first before creating a new one so you don't end up with multiples. As configured it runs every morning at 3 am.

option explicit
const EVENT_SUCCESS = 0
const EVENT_ERROR = 1
const EveryDay = 127
Dim BGINFO
Dim strComputer,objWMIService

ptrBGINFO = false
ptrSpyBot = false
ptrSpyBotUpdate = false

'if not used as a startup script replace . with desired computername
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

GetCurrentJobs
ScheduleSpywareTasks
wscript.quit


Sub ScheduleSpywareTasks
Dim errJobCreated, ScheduleErrors, objNewJob, JobID
On Error Resume Next
ScheduleErrors = False

Set objNewJob = objWMIService.Get("Win32_ScheduledJob")

if ptrBGINFO = false then
'replace BGINFO SWITCHES with your desired switches, be sure to leave a space in front of the first switch. Also change the path to BGINFO.
errJobCreated = objNewJob.Create (Chr(34) & "\\mydomain\netlogon\bginfo.exe" & Chr(34) & " BGINFO SWITCHES", "********000000.000000" & TimeZoneOffset, True , EveryDay, , , JobID)
If Err.number <> 0 Then ScheduleErrors = True
end if

If ScheduleErrors = True Then
WshShell.LogEvent EVENT_ERROR, "An error occured trying to schedule BGInfo job"
Else
WshShell.LogEvent EVENT_SUCCESS, "Scheduling of BGInfo job successful"
End If
End Sub



Sub GetCurrentJobs
'define variables for use with script
Dim objJob, colScheduledJobs
'clear errors
On Error Resume Next
Set colScheduledJobs = objWMIService.ExecQuery("Select * from Win32_ScheduledJob")
For Each objJob in colScheduledJobs
if instr(objJob.Command, "bginfo.exe") > 0 then
ptrBGINFO = true
end if
Next
End Sub


Function TimeZoneOffset
'Read time zone offset value from Registry.
Dim oShell, TZOffset
Set oShell = CreateObject("WScript.Shell")
TZOffset = oShell.RegRead("HKLM\SYSTEM\CurrentControlSet\Control\TimeZoneInformation\ActiveTimeBias")
TZOffset = Right(1000 + TZOffset, 3)
TimeZoneOffset = -TZOffset
End Function


Posted
Option Explicit

Const EVENT_SUCCESS = 0

Const EVENT_ERROR = 1

Const EveryDay = 127

Dim BGINFO

Dim strComputer, objWMIService

Dim ptrBGINFO

Dim ptrSpybot

Dim ptrSpybotUpdate

ptrBGINFO = False

ptrSpybot = False

ptrSpybotUpdate = False

'if not used as a startup script replace . with desired computername

strComputer = "D0202396"

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

GetCurrentJobs

ScheduleSpywareTasks

wscript.Quit

Sub ScheduleSpywareTasks()

Dim errJobCreated, ScheduleErrors, objNewJob, JobID

On Error Resume Next

ScheduleErrors = False

Set objNewJob = objWMIService.Get("Win32_ScheduledJob")

If ptrBGINFO = False Then

'replace BGINFO SWITCHES with your desired switches, be sure to leave a space in front of the first switch. Also change the path to BGINFO.

errJobCreated = objNewJob.Create(Chr(34) & "\\mickey\groups\background\bginfo.exe" & Chr(34) & " drh.bgi /timer:0", "********000000.000000" & TimeZoneOffset, True, EveryDay, , , JobID)

If Err.Number <> 0 Then ScheduleErrors = True

End If

If ScheduleErrors = True Then

WshShell.LogEvent EVENT_ERROR, "An error occured trying to schedule BGInfo job"

Else

WshShell.LogEvent EVENT_SUCCESS, "Scheduling of BGInfo job successful"

End If

End Sub

Sub GetCurrentJobs()

'define variables for use with script

Dim objJob, colScheduledJobs

'clear errors

On Error Resume Next

Set colScheduledJobs = objWMIService.ExecQuery("Select * from Win32_ScheduledJob")

For Each objJob In colScheduledJobs

If InStr(objJob.Command, "bginfo.exe") > 0 Then

ptrBGINFO = True

End If

Next

End Sub

Function TimeZoneOffset()

'Read time zone offset value from Registry.

Dim oShell, TZOffset

Set oShell = CreateObject("WScript.Shell")

TZOffset = oShell.RegRead("HKLM\SYSTEM\CurrentControlSet\Control\TimeZoneInformation\ActiveTimeBias")

TZOffset = Right(1000 + TZOffset, 3)

TimeZoneOffset = -TZOffset

End Function

That is what I had to change to get it to work. Few questions though. I highlighted my changes in bold so their easy to spot incase I'm wrong.

1 Why is there SpyBot stuff in the script? This a remodeled script? (If its not doing anything I won't worry about it, just have to ask)

2 If I run it off my computer. It works. I can schedule the task on my PC using the "." everything is perfectly okay. If I try to do it on someone elses computer renaming the "." to their "ComputerName" the script works like normal. But it tells me it cannot find the configuration file. Same thing happens if I leave it "." and run it from their PC.

Basically no matter how I schedule it, it works on my PC but no one elses.

The files are shared on a server with everyone having full control of the folder for trouble shooting purposes. Nothing changes. here is the real tricky part. I can log into any other computer and it fails. But from my system it works.

This is why I started using the Batch file to launch it in the first place. I had to use a mapped network drive and the batch file to get rid of the error about the configuration file.

Posted

<snip>
Dim ptrBGINFO
Dim ptrSpybot
Dim ptrSpybotUpdate

ptrBGINFO = False
ptrSpybot = False
ptrSpybotUpdate = False
<snip>

Sorry, I edited it from a script we use for scheduling spybot and ad-aware.

you can delete the ptrSpyBot, and ptrSpybotUpdate lines.

The rest looks right.

Posted

@Eyeball - have fun with it.

@constantine - I think I found the issue. Or at least when I did a full test after a reboot and making sure I had no active network folder connections. When you create a task with either the AT command or my script by default create the task to run as the SYSTEM account which cannot connect to the network location. If I manually create the task or do it with schtasks.exe and specify my domain account it works just fine.

the problem with schtasks.exe is that you then either need a generic account to use to connect to it. or copy the files down to the local machines and run it locally.

Posted

couldnt he just create a new active directory user specifically for running scheduled tasks and have the tasks run with those credentials?

im not sure i get why adding it with schtasks.exe and specifying domain admin as the user account wont work :blink: or is there some issue with using this account that i am not aware of?

Posted (edited)

Now that I've had time to think about this. How were your permissions set up on your test area? Was it set to Everyone could come in? or mabey just Domain Users?

I gave the Everyone group full control just to see what would happen.

**EDIT**

I should also mention I'm not using the NetLogon folder. I'm just using a shared folder on a server.

Edited by Constantine
Posted

on my test setup i was just running a local program c:\windows\system32\mspaint.exe and you want to run a remote one, i guess thats how the 2 setups differ,

could you make a script to copy bginfo.bat to each users c: drive and then have the scheduled task run c:\bginfo.bat at scheduled intervals under the domain\administrator account?

Posted

I kind of tried that.

That might work, I can get the batch file on each computer.

My issue at the time was, that I was unable to create the task to run, mainly because the AT command was not working and just flat out copying the task wasn't working at all. Now that I have learned quite a bit more. I think if I modify his script to create the task to launch the batch file, and also copy the batch file locally. It might actually work, if nothing else schtasks might. I'll try both and let you know what happens.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...