Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Problems with scripting a Scheduled Task in XP Pro

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,25600
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Problems with scripting a Scheduled Task in XP Pro
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Problems with scripting a Scheduled Task in XP Pro - 9/6/2005 8:46:38 AM   
  thantos_kalev2001


Posts: 26
Score: 0
Joined: 8/31/2005
Status: offline
I have a vbscript that backs up Outlook pst files on a local harddrive to a network location.  The script closes outlook, runs DIR to determine the .pst paths, copies the pst files to a network share, and then reopens outlook.  In a nutshell, the script works great.
Now, my next task is to use vbscript to create a scheduled task for the pst backup script.  I am using the following code in a logon script to initially create the scheduled task:

Set objShell = Wscript.CreateObject("Wscript.Shell")
objShell.run("cmd /c schtasks.exe /create /SC DAILY /ST 21:50:00 /TN Pst-Backup /RU domain_name/user ID /RP password /TR c:\backup-pst.vbs")

The script creates the Scheduled Task, but it will not run (or, if it runs, it only does so briefly).  The account that I have specified is a domain account that should have rights on the local computer as well as the network share that the pst files will be copied to.  I have also checked the Event log files, but there are no errors. I have also ensured that the backup-pst.vbs file exists in the proper directory.   I suspect that the problem lies in the Scheduled Task permissions, as the script works perfectly if I just run it from the command line.

Has anyone had a problem like this before (namely, scripting Scheduled Tasks) and, if so, could you point me to some other resources?

Any help would be greatly appreciated!!!

Thanks,

Thantos
 
 
Post #: 1
 
 RE: Problems with scripting a Scheduled Task in XP Pro - 9/8/2005 6:56:54 AM   
  didorno

 

Posts: 361
Score: 0
Joined: 2/12/2005
From:
Status: offline
Is your problem maybe the /C after cmd ? Try
objShell.run("cmd /K schtasks.exe /c  etcetera.

Regards.

_____________________________

Regular Expression ? I (L+o{1,}v{1,3}e\s)+[iI]t!$

(in reply to thantos_kalev2001)
 
 
Post #: 2
 
 RE: Problems with scripting a Scheduled Task in XP Pro - 9/8/2005 7:25:28 AM   
  thantos_kalev2001


Posts: 26
Score: 0
Joined: 8/31/2005
Status: offline
Thanks for the reply.
I tried that, but no luck.
I did have better luck using the following code (and AT.EXE):
Set objNewJob = objWMIService.Get("Win32_ScheduledJob")
errJobCreated = objNewJob.Create _
   ("c:\script-name.vbs", "********084500.000000-420", _
       True , 1 OR 2 OR 4 OR 8 OR 16, , , JobID)
Wscript.Echo errJobCreated


This works: the taks is scheduled and the script runs.  However, the script runs with the credentials of the NETWORK SERVICE account.  So, when Outlook reopens
(I use this code:
 objShell.run """C:\Program Files\Microsoft Office\OFFICE11\outlook.exe""", 6, True
), it tries to create a new profile for the NETWORK SERVICE account, instead of using the logged on users outlook profile...Weird

So, I am trying to find a way around that.  I wish I could find some good resources, or get some advice, on using certain logon/user credentials when using vbscript to create Schedule Tasks.   Google has helped, but I am still looking...

Thanks,

Thantos

(in reply to didorno)
 
 
Post #: 3
 
 RE: Problems with scripting a Scheduled Task in XP Pro - 9/9/2005 12:30:53 AM   
  mbouchard


Posts: 1863
Score: 14
Joined: 5/15/2003
From: USA
Status: offline
Does your schtasks command work correctly when you try it out side of the script?

_____________________________

Mike

For useful Scripting links see the Read Me First stickey!

Always remember Search is your friend.

(in reply to thantos_kalev2001)
 
 
Post #: 4
 
 RE: Problems with scripting a Scheduled Task in XP Pro - 9/9/2005 5:50:18 AM   
  thantos_kalev2001


Posts: 26
Score: 0
Joined: 8/31/2005
Status: offline
Yes, it works like a charm.

The best I can do is to use the winShceduler to create a task that will run via the NETWORK service, which does have rights to write the pst files to a network location.
However, since the script tries to use the NETWORK service account to open outlook (rather than the logged on user's account), I will just have to leave outlook closed, unless I can find a way to revert to the logged on user's account to open outlook--which I need to do without entering a password.

Thantos

(in reply to mbouchard)
 
 
Post #: 5
 
 RE: Problems with scripting a Scheduled Task in XP Pro - 9/11/2005 11:39:28 PM  1 votes
  mbouchard


Posts: 1863
Score: 14
Joined: 5/15/2003
From: USA
Status: offline
Take a look at my post here http://www.visualbasicscript.com/m_25643/tm.htm

Using the RunAsSet from AutoIt may help, you could do this.
1) have your script on the local PC, essentially have 2 sections picked by a particular Arguement
2) Have your AutoIT EXE, which would be the one set to run from the scheduled task.  You would probably want to call the exe using the Users Rights.  This way outlook would open correctly.

I have not used RunAsSet via schduled tasks, but have used it to install an app while logged on using a restricted user account.


AutoIT Exe
1) Elevate the rights running.  These rights would have access to the network location.
2) call your script with an arugument .e. Run ("Some Script.vbs /elevate"), wait for script to quit.
3) reset RunAs
4) call script again without an arguement

In your Vbscript
1) check which argument is passed
    If Args.whatever = "elevate" then
       Do this section, close outlook, do your copy, etc.
       Once complete, quite
    Else
       Do this section, reopen outlook
    End If

_____________________________

Mike

For useful Scripting links see the Read Me First stickey!

Always remember Search is your friend.

(in reply to thantos_kalev2001)
 
 
Post #: 6
 
 RE: Problems with scripting a Scheduled Task in XP Pro - 9/14/2005 12:57:39 AM   
  thantos_kalev2001


Posts: 26
Score: 0
Joined: 8/31/2005
Status: offline
Thanks for the reply and the information on AutoIT--looks like a very useful program!
I am working on setting up a config script with AutoIT--rather than using Scheduled Tasks (there are just too many variables needed to get the task to run properly), I might just use a Logoff script that will search the local drives for .pst files and then copy them to a network location via the admin logon credentials supplied by an autoIT executable.   I will post my results once I am finished.

Thanks again!


Thantos

(in reply to mbouchard)
 
 
Post #: 7
 
 RE: Problems with scripting a Scheduled Task in XP Pro - 9/15/2005 5:52:11 AM   
  sfcg

 

Posts: 3
Score: 0
Joined: 9/15/2005
Status: offline
You've got the slash backwards in your domain/username the slash should be a backslash i.e

Here's yours:


Set objShell = Wscript.CreateObject("Wscript.Shell")
objShell.run("cmd /c schtasks.exe /create /SC DAILY /ST 21:50:00 /TN Pst-Backup /RU domain_name/user ID /RP password /TR c:\backup-pst.vbs")

Here's the way it should be.

Set objShell = Wscript.CreateObject("Wscript.Shell")
objShell.run("cmd /c schtasks.exe /create /SC DAILY /ST 21:50:00 /TN Pst-Backup /RU domain_name\user ID /RP password /TR c:\backup-pst.vbs")

Thats the standard for microsoft network authentication. I tried it and it worked fine that way. It's always the little things with this stuff eh?

(in reply to thantos_kalev2001)
 
 
Post #: 8
 
 RE: Problems with scripting a Scheduled Task in XP Pro - 9/15/2005 5:54:16 AM   
  thantos_kalev2001


Posts: 26
Score: 0
Joined: 8/31/2005
Status: offline
Thanks; I had that in my script already--just put the wrong slash in there when I removed the actual user name in order to post the code snippet.


Thantos

(in reply to sfcg)
 
 
Post #: 9
 
 RE: Problems with scripting a Scheduled Task in XP Pro - 9/15/2005 9:09:41 AM   
  kirrilian


Posts: 628
Score: 3
Joined: 3/15/2005
From:
Status: offline
just a guess, but when you call cmd /c dont you have to enclose the entire command with quotes? so you would need to triple quote your shell command

eg.

objShell.run("cmd /c ""schtasks.exe /create /SC DAILY /ST 21:50:00 /TN Pst-Backup /RU domain_name\user ID /RP password /TR c:\backup-pst.vbs""")

im curious to see if this works or not


_____________________________

Have you searched here ?
VBScript Fundamentals
My Site

(in reply to thantos_kalev2001)
 
 
Post #: 10
 
 RE: Problems with scripting a Scheduled Task in XP Pro - 4/10/2006 1:19:06 PM   
  rosado

 

Posts: 13
Score: 0
Joined: 4/8/2006
Status: offline
thantos_kalev2001, would you be able to post your script which closes outlook? I need to do something similar but I cant find a way to provide alternate credentials...

Thanks!

(in reply to thantos_kalev2001)
 
 
Post #: 11
 
 RE: Problems with scripting a Scheduled Task in XP Pro - 4/11/2006 12:01:54 AM   
  DiGiTAL.SkReAM


Posts: 1157
Score: 7
Joined: 9/6/2005
From: Florida, USA
Status: offline
Also, you might want to specify credentials for the scheduled tasks to run under by default.
Goto Control Panel > Scheduled tasks.
Click on Advanced > AT Service Account
Fill in the blanks.

Give that a shot.  It is also possible that what you are trying to run requires something to be set in the profile, that is only being set when someone is logged into the machine.

As for your cmd string....
Try this:
objShell.run "%comspec% /c schtasks.exe /Create /RU domain_name\user_ID /RP password /SC Weekly /D ""MON, TUE, WED, THU, FRI"" /ST 21:50:00 /TN Pst-Backup /TR c:\backup-pst.vbs", 0, True

Your line had the command string inside of parantheses ().  This is only needed if you are grabbing the results, for example: iResults = oShell.Run("cmd")
Also, I would suggest that you create a .CMD batch file to run the .VBS file.  I've always had better success with that versus running the .VBS directly.

_____________________________

"Would you like to touch my monkey?" - Dieter (Mike Meyers)

"It is better to die like a tiger, than to live like a pussy."
-Master Wong, from Balls of Fury

(in reply to rosado)
 
 
Post #: 12
 
 RE: Problems with scripting a Scheduled Task in XP Pro - 4/11/2006 1:51:24 AM   
  kirrilian


Posts: 628
Score: 3
Joined: 3/15/2005
From:
Status: offline
i use schtasks in my scripts frequently and i know this works:

schedule = "SCHTASKS /Create /SC once /TN deployment /TR ""c:\aesdprj\deployment\esd_deployment.vbs"" /ST 18:00:00 /RU domain\user.name /RP password"
return = WshShell.Run(schedule, 1, True)

a couple gotchas, the date/time stamp must be exactly like schtasks wants it formatted! and if i remember correctly you have to quote the task to run, even if it doesnt have spaces.

_____________________________

Have you searched here ?
VBScript Fundamentals
My Site

(in reply to DiGiTAL.SkReAM)
 
 
Post #: 13
 
 RE: Problems with scripting a Scheduled Task in XP Pro - 5/11/2006 5:45:10 AM   
  UnderCoverGuy

 

Posts: 2
Score: 0
Joined: 5/11/2006
Status: offline


Here is what I use (slimmed down, of course).  Part of my script calls SCHTASKS.  I set this up to launch 2 minutes after the script is launched (actually, it begins at one and 5 seperate task launch in 1 minute intervals - but I didn't paste the entire script here, just a portion).  The other parts put something in the Event Log so that I know the task was created properly, INCLUDING the schtasks command-line <password not visable so the users don't know the password>.  My scripts actually launch through SMS and deploy apps to a number of PC's.  Replace the items in red with your information.

******************************

NewTime = FormatDateTime(DateAdd("n",2,Now),vbShortTime) & ":00"
NewDate = FormatDateTime(DateAdd("n",1,Now),vbShortDate)
'
If Mid(NewDate,3,1) <> "/" then NewDate = "0" & NewDate
If Mid(NewDate,6,1) <> "/" then NewDate = Left(NewDate,3) & "0" & Right(NewDate,6)

LaunchCommand = "SchTasks /create /tn ""Comment"" /tr ""C:\App.Exe"" /sc once /st " & NewTime & " /sd " & NewDate & " /ru " & InstallAccount & " /rp " & InstallPassword

Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec(LaunchCommand)

Set EventShell = Wscript.CreateObject("Wscript.Shell")
Set EventNetwork = Wscript.CreateObject("Wscript.Network")
ComputerName = "."

LaunchCommandWithoutPassword = "SchTasks /create /tn ""Comment"" /tr ""C:\Program Files\App.Exe"" /sc once /st " & NewTime & " /sd " & NewDate & " /ru " & InstallAccount & " /rp " & "<Password not displayed - See IT Support>"

If Err = 0 Then

EventDescription = "The App RUN task creation was successful on " & EventNetwork.UserDomain & "\" & EventNetwork.ComputerName & " by user " & EventNetwork.UserDomain & "\" & EventNetwork.UserName & ".  The command used to launch the task was:" & vbCrLf & vbCrLf & LaunchCommandWithoutPassword
EventShell.LogEvent Success, EventDescription
Else

EventDescription = "The App RUN task creation failed on " & EventNetwork.UserDomain & "\" & EventNetwork.ComputerName & " by user " & EventNetwork.UserDomain & "\" & EventNetwork.UserName & ".  The command used to launch the task was:" & vbCrLf & vbCrLf & LaunchCommandWithoutPassword

EventShell.LogEvent Failure, EventDescription

End If

Err.Clear

******************************

Hope this helps.  Be careful of the very specific syntax for using Time and Date calls.  Also, watch the quotes.

(in reply to kirrilian)
 
 
Post #: 14
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Problems with scripting a Scheduled Task in XP Pro Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts