# Solved: Run program from a different computer (Batch file Q)



## mt2002

Hey everyone,

Is it possible to run a program that is on another pc over a network? I am trying to create a small batch script that I can run to change a setting on a networked pc. It works fine on my pc, but I keep getting a "This is not a valid Win32 application" followed by "access denied" when running it from a different computer...

Is there a way? If not, is there another method of changing the system timezone without running rundll32.exe (Which is where my problem is at following this line to change it)...


Code:


pushd //the_pc_name's_c_drive/

// error here from rundll32
RunDLL32 shell32.dll,Control_RunDLL TIMEDATE.cpl,,/Z %a%

Thanks for any help


----------



## draceplace

This script will run calc.exe on a remote computer. It writes a log, check for errors. You should be able to copy any program to the remote pc then run it....

'/ /
'///////////////////////////////////////////////////////////////////////

'/ Variable Declarations
'///////////////////////
Const ForWriting = 2
Const ForAppending = 8
Const NormalWindow = 5
Set fs = CreateObject("Scripting.FileSystemObject")
LogFile = "C:\ascripts\new1\notepad.csv"
Dim CurrentDate, CurrentTime
CurrentDate = Date
CurrentTime = Time
DateTime = CurrentDate & "," & CurrentTime

On Error Resume Next

'/ Main
'//////
Set f = fs.Opentextfile(LogFile, ForAppending, True)
'Set f = fs.Opentextfile(LogFile, ForWriting, True)
'/f.WriteLine "ComputerName,Date,Time,Process ID"
'/remote_machine_number = "itd-pc13"
remote_machine_number = InputBox("Enter the computer name. ")
remote_machine_name = remote_machine_number
Set objLocator = CreateObject("WbemScripting.SWbemLocator")
Set objService = objLocator.ConnectServer(remote_machine_name, "Root\DEFAULT")
If Err.Number <> 0 Then
f.WriteLine remote_machine_name & ", Error " & Err.Number & "," & Err.Description
WScript.Echo "Error No Contact!!." 
Err.Clear
Else

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & remote_machine_name & "\root\cimv2:Win32_Process")
Set objConfig = objStartup.SpawnInstance_
objConfig.ShowWindow = NormalWindow
Set objProcess = GetObject("winmgmts:root\cimv2:Win32_Process")
errReturn = objProcess.Create("calc.exe", null, objConfig, intProcessID)

If errReturn = 0 Then
Wscript.Echo "calc.exe was started with a process ID of " & intProcessID & "."
Else
Wscript.Echo "calc.exe could not be started due to error " & errReturn & "."
End If
End If
'/Next
f.Close
WScript.Echo "All Done."


----------



## mt2002

Sorry, I need it in a batch script. I dont know how to run or work with vbscript (I think that is what code you posted is, anyways).

If you tell me how to work with your code or modify it I would be glad to use it 

Thanks for your reply though


----------



## draceplace

Copy and paste it in a text file and rename the extension .vbs. double click and it will run. The ' are comments. You can hard code the computer name in there but it is set up to prompt you for the computer name use your own computer name for starts. (You could read in a file of computer names if you wanted to do a bunch.

Also change this LogFile = "C:\ascripts\new1\notepad.csv"
to be something local for you or comment out the reference to it.

VBscript is really more fun\powerfull than batch. It took me a while to wean myself off of batch but 5 years later I'll never go back. The hardest part is to not understand what the objects are technically doing (inquiring minds want to know!!). Just accept it when it works. There are a lot of resources when you google to find code like you need.

PSpad here is a decent free editor (instead of working in pure text) I would like to find a better one or learn how to use this one better. VbsEdit I like but the free version is not as good as it used to be.


----------



## TheOutcaste

Download PSTools from Sysinternals

Then use PSExec:



Code:


Set a=Pacific Standard Time
psexec \\Computername control.exe timedate.cpl,,/Z %a%

Only other option would be to use Reg.exe and write the correct values to HKLM\SYSTEM\CurrentControlSet\Control\TimeZoneInformation
I don't know how to refresh it remotely, but a reboot, or logoff will do it.

WMIC is supposed to be able to do this via one of these commands:
wmic /node:Computername OS Set CurrentTimeZone=_offsetfromGMT_
wmic /node:Computername ComputerSystem Set CurrentTimeZone=_offsetfromGMT_
I can't get these to work on the local system, let alone a remote system. Command says it was successful, but a query shows it hasn't changed.

HTH

Jerry


----------



## mt2002

draceplace said:


> Copy and paste it in a text file and rename the extension .vbs. double click and it will run. The ' are comments. You can hard code the computer name in there but it is set up to prompt you for the computer name use your own computer name for starts. (You could read in a file of computer names if you wanted to do a bunch


Thanks  I will be needing to know what remote_machine_number actually is as well as how to modify it to run he program I need though. ie, how do I run timedate.cpl and pass it parameters (like in my first post)? Also, can I log to the console rather then a file?



> Only other option would be to use Reg.exe and write the correct values to HKLM\SYSTEM\CurrentControlSet\Control\TimeZoneInformation
> I don't know how to refresh it remotely, but a reboot, or logoff will do it.


Im going to look into that. Thanks for the help 

I cannot install any software on any of the machines. I should probably have mentioned that in my first post. Sorry


----------



## TheOutcaste

Found a little bug with using Reg Add. It will update the time zone, but doesn't actually change the clock. I didn't notice at first as I have a startup script that syncs each PC's clock with the domain controller at boot. When I disabled the script, the time zone was still changed, but the time wasn't, until I used *Net Time* to sync it again. Using the Internet Time feature should work too, but that only syncs once a week.

Can you create a startup script that each PC would run at boot?. If so you can use Microsoft's script to refresh the TimeZone setting.
Search this page for "Step 2: Use a script to reload the TimeZoneInformation registry key"
Instead of merging the entire new database as shown in Step 3, just create a *.reg file that will merge the name of the timezone you want to set, then run the script. You could also modify the script to use a specified TimeZone name rather than searching for one.

If you are not on a domain, you could copy the files to each PC, then add a command to the *HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce* key to run the script at the next boot.

draceplace's script should do the trick without a reboot though, so that will be the best solution. From the MS script looks like you have to add the time change privilege to the process object though.

This is the script I used using Reg Add if you're curious:


Code:


Reg Add \\Fred\HKLM\SYSTEM\CurrentControlSet\Control\TimeZoneInformation /v Bias /t REG_DWORD /d 420 /F
Reg Add \\Fred\HKLM\SYSTEM\CurrentControlSet\Control\TimeZoneInformation /v StandardName /t REG_SZ /d "Mountain Standard Time" /F
Reg Add \\Fred\HKLM\SYSTEM\CurrentControlSet\Control\TimeZoneInformation /v StandardBias /t REG_DWORD /d 0 /F
Reg Add \\Fred\HKLM\SYSTEM\CurrentControlSet\Control\TimeZoneInformation /v StandardStart /t REG_BINARY /d 00000b00010002000000000000000000 /F
Reg Add \\Fred\HKLM\SYSTEM\CurrentControlSet\Control\TimeZoneInformation /v DaylightName /t REG_SZ /d "Mountain Daylight Time" /F
Reg Add \\Fred\HKLM\SYSTEM\CurrentControlSet\Control\TimeZoneInformation /v DaylightBias /t REG_DWORD /d -60 /F
Reg Add \\Fred\HKLM\SYSTEM\CurrentControlSet\Control\TimeZoneInformation /v DaylightStart /t REG_BINARY /d 00000300020002000000000000000000 /F
Reg Add \\Fred\HKLM\SYSTEM\CurrentControlSet\Control\TimeZoneInformation /v ActiveTimeBias /t REG_DWORD /d 420 /F
shutdown -F -R -M \\fred -T 10

HTH

Jerry


----------



## mt2002

Sorry, I cannot use any of those methods. I cannot create any start up scripts and I am on a domain.

Does anyone have any other solutions? Im also still curious about draceplace's script but I dont know how to modify it to meet my needs (I can learn VBScript, but I need to do this asap).

Thanks for the help so far


----------



## mt2002

Or, if there is a way (Through Windows) that can allow me to open up and change control panel settings from a remote pc that would also work fine


----------



## draceplace

I'm pretty sure this can be done with VBscript if you have the permissions. You can learn vb later.

1st step "I will be needing to know what remote_machine_number " you ask?

In the code above remote_machine_number, remote_machine_name, are the same thing. Like running %computername% from the cmd line. Also if you go to 'my computer ' properties\computer name tab you will see the FQDN of the computer.

Here is a simple script to start with..will give your computer name. Paste it into a file called pc_name01.vbs . If using note pad once this file is made as .txt then rename to .vbs when you right click you have the option to "open" or "edit". Open (or double click will run the script). You may need to set the default editor for .vbs to notepad and worry about finding an editor later if your on a time constraint. 

' VB Script Document
'option explicit
' VBScript to Display your ComputerName
' Created Jan 2003

Set WshNetwork = WScript.CreateObject("WScript.Network")
WScript.Echo "You Computer Name = " & WshNetwork.ComputerName
Set WshNetwork = nothing



then...Can you take that script an run calc.exe (you can use notepad.exe if you want) on another computer?


----------



## draceplace

Here's a simple time script I got from somewhere...
Change the Domain:AEM parameter

'vbscript document
Dim WSHNetwork
Dim WshShell
Dim WshProcessEnvironment

Set WSHNetwork = WScript.CreateObject("WScript.Network")
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshProcessEnvironment = WshShell.Environment("Process")

'This Sets the time

wshShell.Run "Net Time /Domain:AEM /set /yes",0,False


'Use this for errors be use caution
'On Error Resume Next


----------



## draceplace

mt2002 said:


> Or, if there is a way (Through Windows) that can allow me to open up and change control panel settings from a remote pc that would also work fine


You can do this with the "Remote Desktop Connection"? depending on how many machines you have to touch?


----------



## mt2002

I modified the script a little:



Code:


Const ForWriting = 2
Const ForAppending = 8
Const NormalWindow = 5
Set fs = CreateObject("Scripting.FileSystemObject")
Dim CurrentDate
CurrentDate = Date
DateTime = CurrentDate & "," & CurrentTime

On Error Resume Next

'-----------------------------------------------------------

remote_machine_number = InputBox("Enter the computer name. ")
remote_machine_name = remote_machine_number

Set objLocator = CreateObject("WbemScripting.SWbemLocator")
Set objService = objLocator.ConnectServer(remote_machine_name, "Root\DEFAULT")

If Err.Number <> 0 Then
	WScript.Echo "Error " & Err.Number & ": " & Err.Description
	Err.Clear
Else

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

	Set objConfig = objStartup.SpawnInstance_
	objConfig.ShowWindow = NormalWindow

	Set objProcess = GetObject("winmgmts:root\cimv2:Win32_Process")
	errReturn = objProcess.Create("calc", null, objConfig, intProcessID)

	If errReturn = 0 Then
		Wscript.Echo "Process started with a process ID of " & intProcessID & "."
	Else
		Wscript.Echo "Process could not be started due to error " & errReturn & "."
	End If
End If

WScript.Echo "Completed"

It works on my machine (I dont know if it works remotely yet). However if I run this script under a command prompt running under my admin account (using a runas command) it says that it is completed (calc.exe even starts) but calc.exe does not display its window :/

I dont think this is an error in the script though..

How would I modify this to run the control panel though? Or, more specifically, timedate.cpl? I have tried numerous methods but none worked...



> You can do this with the "Remote Desktop Connection"? depending on how many machines you have to touch?


I can but its slow (Both the program and the process  ) and I have a lot of computers so it would take quote some time...


----------



## TheOutcaste

When you start a program remotely it will not be visable on the remote PC, so if you were expecting the calculator to pop up it won't. You will see it running in Task Manager though -- in fact the only way to stop it is by ending the process in Task Manager.

I tested this script against an XP machine and it changed the TimeZone just fine, and the control.exe process ended.
I test it by mis-typing the Time Zone name, and it did nothing on the remote system, but also did not return an error. So you need to make sure you enter the name correctly.

It will echo a line showing the process ID that was started on the remote PC.
Save it with a vbs extension, then run it with cscript
*cscript remotetz.vbs*


Code:


'  Variable Declarations
Dim strRemotePC, objLocator, objService, objWMIService
Dim szTzKey, process, processid, result, strUpdateCommand
' Enter the timeZone you wish to set here. If the name contains spaces, it must be in quotes
szTzKey = "Mountain Standard Time"
On Error Resume Next
'  Main
strRemotePC = InputBox("Enter the computer name. ")
Set objLocator = CreateObject("WbemScripting.SWbemLocator")
Set objService = objLocator.ConnectServer(strRemotePC, "Root\DEFAULT")
If Err.Number <> 0 Then
   WScript.Echo "Unable to connect to " & strRemotePC & ", Error " & Err.Number & "," & Err.Description
   Err.Clear
 Else
 Set process = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strRemotePC & "\root\cimv2:Win32_process")
' Add time change privilege to the process object
 process.Security_.Privileges.AddAsString "SeSystemTimePrivilege",True
 strUpdateCommand = "control.exe timedate.cpl,,/Z" & szTzKey
 'Launch control.exe to refresh time zone information using the TZ key name obtained above 
 result = process.create(strUpdateCommand,Null,Null,processid)
 If result <> 0 Then
    Wscript.Echo "Error " & result & "Occurred"
    Wscript.Quit 0
    Else
    Wscript.Echo "Control.exe started with process ID of " & processid & "."
 End If
End If

HTH

Jerry


----------



## mt2002

> When you start a program remotely it will not be visable on the remote PC, so if you were expecting the calculator to pop up it won't. You will see it running in Task Manager though -- in fact the only way to stop it is by ending the process in Task Manager.


Thing is I tried it using my pc's name... (More specifically, 127.0.0.1). It would explain it though when running it on another machine though.

Im going to try your script and give it a shot. Thanks for the help


----------



## TheOutcaste

mt2002 said:


> However if I run this script under a command prompt running under my admin account (using a runas command) it says that it is completed (calc.exe even starts) but calc.exe does not display its window


When you use RunAs to start the command prompt, calc.exe will be started in the same account specified with RunAs. The visible Desktop is still the current user though, so it won't be visible. You can see it running in Task Manager in the account you used for the RunAs command.

Jerry


----------



## draceplace

I think IP address will work as well as pc name. If you can get a list of computernames (Active directory?) you can read in a text file or hard code a table in your vbs then a (Do while not EOF loop) and write errors to a log, comment out "Echo" s and let it run for as long as it takes.

Not really understanding why your running from the cmd line?

Good stuff Outcaste!


----------



## mt2002

> When you use RunAs to start the command prompt, calc.exe will be started in the same account specified with RunAs. The visible Desktop is still the current user though, so it won't be visible. You can see it running in Task Manager in the account you used for the RunAs command.


Ah, that makes alot of sense. The same problem happens with the new script...I cannot tell you if it works under my normal account as it doesnt have the permission to do it. That is why I was using runas under the command prompt to try running it under my admin account without needing to log off.

If I log on as an admin and run the script, should that fix this problem then? I suspect it should...


----------



## TheOutcaste

The only time you will see anything is if you run it on the local computer in the same account you are logged in as. You will not see anything on any remote computers. If you are looking at Task Manager on the Remote PC when the script is run, you will see control.exe added to the list of processes for a bit, then it will close, but you'll see nothing on the desktop of the Remote PC other than the time changing.

The same is true running it locally. From a limited account I get an error that I don't have permission to change time. Open a command prompt with RunAs using any Computer Admin account, and the Time Zone changes. Nothing appears on screen, I just see the clock suddenly change time.

It will even work for PC's that are not on the domain, as long as the user account you use for RunAs has the correct permissions on the Remote PC. For instance, with a limited user logged into a non domain PC (Workgroup name same as Domain), I can change the timezone with this script using RunAs with the Domain\Administrator account, as the password is the same as the Workgroup PC's local Administrator account.

HTH

Jerry


----------



## mt2002

It seems to be working. Thanks for the help 

It doesnt work on vista though  ...but lets hope I dont need to worry about that


----------



## mt2002

I think its good to go so Ill mark this solved.

Thanks alot for your help!


----------

