How can I remotely execute a script in Windows?

回眸只為那壹抹淺笑 提交于 2020-01-09 20:13:00

问题


I would like to have a Windows 2003 server fire a script to fire another script in a separate Windows Server 2008 computer.

I have been told that Powershell can do that, and that's fine, but I need more specific details.

Does anyone have any tips for this?

Thanks!


回答1:


psexec from SysInternals




回答2:


Look into the syntax for the AT command. You can use it to schedule a process to run on a remote machine.

The AT command schedules commands and programs to run on a computer at a specified time and date. The Schedule service must be running to use the AT command.

AT [\\computername] [ [id] [/DELETE] | /DELETE [/YES]]
AT [\\computername] time [/INTERACTIVE]
    [ /EVERY:date[,...] | /NEXT:date[,...]] "command"

\\computername     Specifies a remote computer. Commands are scheduled on the
                   local computer if this parameter is omitted.
id                 Is an identification number assigned to a scheduled
                   command.
/delete            Cancels a scheduled command. If id is omitted, all the
                   scheduled commands on the computer are canceled.
/yes               Used with cancel all jobs command when no further
                   confirmation is desired.
time               Specifies the time when command is to run.
/interactive       Allows the job to interact with the desktop of the user
                   who is logged on at the time the job runs.
/every:date[,...]  Runs the command on each specified day(s) of the week or
                   month. If date is omitted, the current day of the month
                   is assumed.
/next:date[,...]   Runs the specified command on the next occurrence of the
                   day (for example, next Thursday).  If date is omitted, the
                   current day of the month is assumed.
"command"          Is the Windows NT command, or batch program to be run.



回答3:


easiest way that is use will be in two steps

a. installing cygwin to remote pc

b. run ssh hudson@mcs '/cygdrive/c/path_to_script.bat'




回答4:


Speaking about PsExec, I would strongly suggest to use Cygwin/OpenSSH instead.

SSH has multiple advantages (over tools like PsExec or even custom-made services).
For example, try to use with PsExec and implement what these bash / ssh command lines do:

ssh user@remotehost "find . -name something" 2> all.errors.txt
ssh user@remotehost "grep -r something ."
if [ "$?" == "0" ]
then
    echo "FOUND"
else
    echo "NOT FOUND"
fi

Good Luck!

  • SSH transfers (!) remote stdout / stderr / exit status to local shell for inspection
    (killer feature and common requirement to integrate remote execution into logic of local scripts)

  • Cygwin/OpenSSH provides standard POSIX shell environment
    (efficient time investment, fundamental tools, cross-platform ready, compatible habits, etc.)

  • You can still/always run all native Windows application
    (including automatic execution of *.bat files by cmd processor)

  • You can configure password-less auth using public keys
    (think about unattended automated tasks)


Tip

There was one requirement I had problems with initially:
background sshd service had to execute apps in user's graphical session
(to make application window appear in desktop environment).

The acceptable solution for me was running sshd service directly in user's GUI session
(start automatically when user logs in, follow the link to see configuration file changes):

/usr/sbin/sshd -f /home/user/sshd_config



回答5:


The accepted solution from http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/Q_22959948.html is:

What I provide was a script that takes parameters... In this case it takes 4. 1) Server: if you pass -server it will only do that one server 2) List: You can provide a list file of servers. 3) Service: Name of the service you want to modify 4) Verbose: is not used here.

I did have some mistakes that I changed in the following code. To use cut/paste the code into a file called Set-RemoteService.ps1. Make sure to set your executionpolicy to run scripts... it will not by default. You do that by using the set-executionpolicy cmdlet. PS> Set-Executionpolicy "RemoteSigned" to run the script you do PS> C:\PathToScript\Set-RemoteService.ps1 -list c:\ServerList.txt -service "DHCP"

######################### Param($server,$list,$service,[switch]$verbose)

if($Verbose){$VerbosePreference = "Continue"} if($list) { foreach($srv in (get-content $list)) { $query = "Select * from Win32_Service where Name='$service'" $myService = get-WmiObject -query $query -computer $srv $myService.ChangeStartMode("Automatic") $myService.Start() } } if($server) { $query = "Select * from Win32_Service where Name='$service'" $myService = get-WmiObject -query $query -computer $server $myService.ChangeStartMode("Automatic") $myService.Start() }



来源:https://stackoverflow.com/questions/861148/how-can-i-remotely-execute-a-script-in-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!