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!

Is there a way to change the user id when scripting a scheduled task?

Status
Not open for further replies.

pellet

Programmer
Oct 23, 2008
55
US
First of all, thank you all for the knowledge I receive when reading Tek-Tips! Without all of you, I would not have as much knowledge about scripting as I do.

My question is, I created a script that backs up certain files to our network and put the script on the local computer. I then created a script that creates a scheduled task every hour that runs my backup script. Here is the scheduled task script:
------------------------------
Set objNewJob = objWMIService.Get("Win32_ScheduledJob")
errJobCreated = objNewJob.Create _
("C:\Backup.vbs", "********010000.000000-300", _
True , 1 Or 2 OR 4 Or 8 OR 16 Or 32 Or 64, , , JobID)
------------------------------
The above script is set to run at 1:00 AM and I have one for every hour.

The problem is the backup.vbs script runs just fine when it is run manually. When the task scheduler runs it, I get an error that it cannot find the network path. If I change the AT Service account to the user ID and password of the current logged in user, the task scheduler works great and kicks off the backup.vbs without a problem.

When scheduled tasks are created using the script above it creates the scheduled task as NT AUTHORITY/SYSTEM. I believe that since the task is running as NT AUTHORITY/SYSTEM and not the user, I am getting the error.

I would like to know if there is a way to script either:

1. - A way to use the current logged in username and password into the scheduled task itself, OR

2. - A way to change the AT Service Account using a script to grab the current logged in username and password.

Manually changing the AT Service Account is not really an option on all computers. I would prefer method #1 above, but #2 would work too.

I checked out some other threads here and also did some research but nothing seems to show the ability to run the scheduled tasks as anything other than NT AUTHORITY/SYSTEM.

Any help or thoughts of different ways to schedule the tasks is greatly appreciated.

Thanks in advance! Have a great day.

Pellet
 
I have bookmarked this question. I have to do the nearly the exact same thing. I have a public use PC that stores files on the C: drive. I need a scheduled task that weekly moves the file to a back up drive on the network. The problem is that in order for the scheduled task to work, I need to have someone logged on with a valid password in the scheduled task.

If you find out how to do this, I would definately appreciate hearing about it.
 
Christineeve (Programmer), instead of bookmarking the post why dont you provide a solution?

i believe the windows scheduler service is disabled at the req. of security teams because it ends up getting abused and leads to security issues, elevated rights for the account etc, so proceed with caution.

does a thread running under the system context have the ability to see a mapped drive? (i.e. one which gets mapped when the user logs on and therefore you can piggy back off that?)
 
With this VB script (sub routine) you can define the user and the password. If you are not working within a domain and don't have equal useraccounts, then you will have more changing in the script. It is designed to run local and the user must have the rights to create a scheduled task. The scheduled task I create with this script runs every 10 minutes, starting now (or 1 minute before midnight if the script is startet within the last 10 minutes of a day).

Sub CreateTask (CreateDelete)
' the parameter "Create" will create or check a scheduled task and in all other cases the scheduled task will be deleted
Dim NewTime, NewDate, LaunchCommand, OwnSystem
Dim EventDescription, EventDescription2, OwnUser, OwnPassword

Set EventShell = Wscript.CreateObject("Wscript.Shell")
Set EventNetwork = Wscript.CreateObject("Wscript.Network")
ComputerName = "."
OwnSystem = EventNetwork.ComputerName
OwnUser = EventNetwork.UserDomain & "\" & EventNetwork.UserName
' it is not possible to read out the password, therefor you have to assign the correct password or send it with
' the Sub call as an other parameter
If LCase (EventNetwork.UserName) = "testuser" Then OwnPassword = "test"
' if this will run on a special computer then use a different user
If LCase (OwnSystem) = "test19" Then
OwnUser = "test19\Administrator"
OwnPassword = "testadm"
End If

Set WshShell = CreateObject("WScript.Shell")
If LCase (CreateDelete) = "create" Then
NewDate = setFileDate()
If Hour (Now) = 23 And Minute (Now) > 48 Then
NewTime = "23:59:00"
Else
NewTime = FormatDateTime(DateAdd("n",10,Now),vbShortTime) & ":00"
End If
If Not FSO.FileExists ("C:\Windows\Tasks\Test.job") Then
LaunchCommand = "SchTasks /create /tn ""Test"" /tr ""C:\Test.cmd"" /sc minute /mo 10 /st " & NewTime & " /sd " & NewDate & " /s " & OwnSystem & " /ru " & OwnUser
Set oExec = WshShell.Exec(LaunchCommand & " /rp " & OwnPassword)
EventDescription = "The Test task creation "
Else
EventDescription = "The check of the Test task "
End If
Else
LaunchCommand = "SchTasks /delete /tn ""Check RAID"" /f"
Set oExec = WshShell.Exec(LaunchCommand)
EventDescription = "The Test task deletion "
End If

EventDescription2 = EventNetwork.UserDomain & "\" & EventNetwork.ComputerName & " by user " & EventNetwork.UserDomain & "\" & EventNetwork.UserName & ". The command used was:" & vbCrLf & vbCrLf & LaunchCommand
If Err = 0 Then
EventDescription = EventDescription & "was successful on " & EventDescription2
EventShell.LogEvent Success, EventDescription
Else
EventDescription = EventDescription & "failed on " & EventDescription2
EventShell.LogEvent Failure, EventDescription
End If
End Sub

Function setFileDate()
Dim theday, themo, theyear

theyear = DatePart("yyyy", Date())
themo = DatePart("m", Date())
'check for length, pad zero if needed
If len(themo) < 2 Then themo = "0" & themo End If
theday = DatePart("d", Date())
'check for length, pad zero if needed
If len(theday) < 2 Then theday = "0" & theday
setFileDate = theday & "/" & themo & "/" & theyear
End Function 'setFileDate
 
Er ... What OS are you using? In theory XP SP1 and later have addressed this by changing the default task account to NetworkServiceHost, which allows network access, rather than LocalSystem (i.e. NT AUTHORITY/SYSTEM), which does not.

Sadly you cannot specify the user account or password through use of Win32_ScheduledJob, which represents the somewhat limited functionality of the older AT command rather than the current SchTasks ,as illustrated by derpate above.

If you were on Vista (or Server 2008 or Windows 7) you could use the scriptable Task Scheduler 2.0 library - CreateObject("Schedule.Service"), fairly simple example here[link] - which is much more capable.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top