如何在 Windows 中远程执行脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/861148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I remotely execute a script in Windows?
提问by Frew Schmidt
I would like to have a Windows 2003 server fire a script to fire another script in a separate Windows Server 2008 computer.
我想让 Windows 2003 服务器触发脚本以在单独的 Windows Server 2008 计算机中触发另一个脚本。
I have been told that Powershell can do that, and that's fine, but I need more specific details.
有人告诉我 Powershell 可以做到这一点,这很好,但我需要更具体的细节。
Does anyone have any tips for this?
有没有人对此有任何提示?
Thanks!
谢谢!
回答by cdonner
Look into the syntax for the AT command. You can use it to schedule a process to run on a remote machine.
查看 AT 命令的语法。您可以使用它来安排在远程机器上运行的进程。
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 命令安排命令和程序在指定的时间和日期在计算机上运行。计划服务必须正在运行才能使用 AT 命令。
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.
回答by pkm
easiest way that is use will be in two steps
最简单的使用方法将分两步
a. installing cygwin to remote pc
一种。将 cygwin 安装到远程电脑
b. run ssh hudson@mcs '/cygdrive/c/path_to_script.bat'
湾 运行 ssh hudson@mcs '/cygdrive/c/path_to_script.bat'
回答by uvsmtid
Speaking about PsExec
, I would stronglysuggest to use Cygwin/OpenSSH instead.
说到PsExec
,我强烈建议改用 Cygwin/OpenSSH。
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 具有多种优势(相对于工具PsExec
,甚至是定制服务)。
例如,尝试使用 withPsExec
并实现这些bash
/ssh
命令行的作用:
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 localshell for inspection
(killer feature and common requirement to integrate remote execution into logic of local scripts)Cygwin/OpenSSH provides standardPOSIX shell environment
(efficient time investment, fundamental tools, cross-platform ready, compatible habits, etc.)You can still/always run all nativeWindows application
(including automatic execution of*.bat
files bycmd
processor)You can configure password-less auth using public keys
(think about unattended automated tasks)
SSH 将 (!) 远程 stdout/stderr/exit 状态传输到本地shell 以进行检查
(杀手功能和将远程执行集成到本地脚本逻辑中的常见要求)Cygwin/OpenSSH 提供标准的POSIX shell 环境
(高效的时间投入、基础工具、跨平台就绪、兼容习惯等)您仍然可以/始终运行所有原生Windows 应用程序
(包括处理器自动执行*.bat
文件cmd
)您可以使用公钥配置无密码身份验证
(想想无人值守的自动化任务)
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).
我最初遇到了一个要求:
后台sshd
服务必须在用户的图形会话中执行应用程序
(以使应用程序窗口出现在桌面环境中)。
The acceptable solutionfor me was running sshd
service directlyin user's GUI session
(start automatically when user logs in, follow the link to see configuration file changes):
在可接受的解决方案,我正在运行sshd
的服务直接在用户的GUI会话
(自动启动,当用户登录时,按照链接查看配置文件更改):
/usr/sbin/sshd -f /home/user/sshd_config
回答by Jonathan Parker
The accepted solution from http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/Q_22959948.htmlis:
从http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/Q_22959948.html接受的解决方案是:
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() }
我提供的是一个带参数的脚本......在这种情况下它需要 4。 1)服务器:如果你传递 -server 它只会做那个服务器 2)列表:你可以提供一个服务器列表文件。3) Service:要修改的服务名称 4) Verbose:此处不使用。
我确实在以下代码中更改了一些错误。使用将代码剪切/粘贴到名为 Set-RemoteService.ps1 的文件中。确保将您的 executionpolicy 设置为运行脚本......默认情况下不会。您可以使用 set-executionpolicy cmdlet 来完成此操作。PS> Set-Executionpolicy "RemoteSigned" 运行你做的脚本 PS> C:\PathToScript\Set-RemoteService.ps1 -list c:\ServerList.txt -service "DHCP"
######################## 参数($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() }