63,535 pts.
 VB Script to Create a Scheduled Task on a Remote Machine Using Alternate Credentials
This is a small VB script I created to remotely schedule a task on another machine, using a different user account from the one logged in the local computer. It was needed because PsExec (a sysinternals' tool we use a lot) didn't always work well in some situations where we needed to execute remote tasks on machines members of a different domain or workgroup, running different Windows versions. It takes the remote machine ip address, the username and password and the remote process as arguments, and schedule its execution with a 1 minute delay, using the remote machine's time. Here is the code:
   'Arguments: <ip address> <username> <password> <program to run>

Const MINUTES = "n"

'Connects to the remote machine's WMI using the provided credentials
Set Locator = CreateObject("WbemScripting.SWbemLocator")
Set Service = Locator.ConnectServer(Wscript.Arguments(0), "rootcimv2", Wscript.Arguments(1), Wscript.Arguments(2))
Set oProcess = Service.Get("Win32_ScheduledJob")

'Gets the remote time, and converts it to the appropriate format
Set colItems = Service.ExecQuery("Select * From Win32_LocalTime")
For Each objItem in colItems
    strTime = objItem.Hour & ":" & objItem.Minute & ":" & objItem.Second
    dtmTime = CDate(strTime)
Next
Set objSWbemDateTime = CreateObject("WbemScripting.SWbemDateTime")
objSWbemDateTime.SetVarDate(DateAdd(MINUTES, 1, dtmTime ))

'Creates the task, and checks the results.  Returns 1 if the task is created, 0 if the creation fails
errReturn = oProcess.Create(Wscript.Arguments(3), objSWbemDateTime.Value, False, 0, 0, True, intJobID)
If errReturn = 0 Then
    Wscript.Echo "1"
Else
    Wscript.Echo "0"
End If
Feel free to post any comments/doubts/suggestions in the discussion section below.

Software/Hardware used:
MS Windows, WMI
ASKED: October 17, 2011  11:36 PM
UPDATED: October 21, 2011  7:16 PM

Answer Wiki:
Hi, Nice to see the script. good share. Thanks Anil <a href="http://www.lepide.com/">LEPIDE</a>
Last Wiki Answer Submitted:  October 21, 2011  7:00 am  by  Voodooclanboy   1,125 pts.
All Answer Wiki Contributors:  Voodooclanboy   1,125 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


 

Carlosdl

I am trying this script but I get the message Access is Denied in vbscript line 7, this is the function Locator.ConnectServer.

I already checked the username and password and they are ok.

I searched the web for any environment adjustment that I needed to make in the destination computer, but only I found this link, but I don’t know if I am looking in the correct way.

Any suggestions?

Thanks

 2,790 pts.

 

Hi Mario.

Can you share how you are calling the script ?
Also, what is the local and remote machines’ OS ?

 63,535 pts.

 

Hi Carlos, here is the call

.remoteappexec.vbs 172.16.10.205 mariodlg xxxxxxx c:WindowsSystem32notepad.exe

I save the script as “remoteappexec.vbs” and “xxxxxxx” is the password for the user “mariodlg”, I’m trying to schedule notepad.exe execution as test.

Both machines have Windows 7 Professional installed.

The error message is :

Command sequence:
C:Usersmariodlgscriptsremoteappexec.vbs
Line: 7
Character: 2
Error: Access denied
Code: 80070005
Source: SWbemLocator

Thanks.

 2,790 pts.

 

I forgot to put double backslash:

.\remoteappexec.vbs 172.16.10.205 mariodlg xxxxxxx c:WindowsSystem32notepad.exe

 2,790 pts.

 

Try reviewing the WMI security configuration on the remote machine.

-Run dcomcnfg to open the “Component Services” console
-Expand “Component Services”->”Computers”->”My computer”->”DCOM config”
-Look for the “Windows Management and Instrumentation” item, and right-click on it
-Click on “Properties” and navigate to the “Security” tab.
-Edit the “Launch and activation” permissions, and make sure the appropriate user or group has the remote launch and remote activation permissions allowed.
-Edit the “Access” permissions and make sure the appropriate user or group has the remote access permission allowed.

-Try again.

Those permissions are the most common causes of “access denied” errors when using WMI.

Third party security tools (firewalls, host intrusion prevention systems, etc) could cause issues as well.

If the machine is part of a domain, I would also use <DOMAIN><username> to connect instead of just <username> (this may or may not make a difference).

 63,535 pts.

 

I forgot to escape the backslash too.

I meant <DOMAIN>\<username>

 63,535 pts.

 

Hi Carlos.

The remote machine had remote access disabled in WMI Configuration.
Now your script is working, thanks a lot.

Regards.

 2,790 pts.

 

Great.

Thanks for posting back, Mario.

 63,535 pts.