我该如何在远程执行Windows脚本?

10
我希望让一个Windows 2003服务器运行一个脚本来激活另一台分离的Windows Server 2008计算机上的脚本。
我被告知PowerShell可以完成此操作,这很好,但我需要更加具体的细节。
是否有人对此有任何提示?
谢谢!

这个问题可能更适合在serverfault.com上提问。我已经标记为关闭原因; stackoverflow现在支持在不同网站之间移动问题。 - Greg Hewgill
5个回答

14

这似乎是最好的答案。我明天会在工作中尝试一下。 - Frew Schmidt

6

了解AT命令的语法。您可以使用它在远程计算机上安排进程运行。

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.

4
最简单的方法分为两步:
a. 安装Cygwin到远程电脑上
b. 运行 ssh hudson@mcs '/cygdrive/c/path_to_script.bat'

2

谈到PsExec,我强烈建议使用Cygwin / OpenSSH代替。

相比于像PsExec或自定义服务等工具,SSH具有多个优势。
例如,请尝试使用PsExec并实现这些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

祝你好运!

  • SSH传输将远程的标准输出、错误输出和退出状态传输给本地shell以供查看。
    (这是一个极好的功能,也是将远程执行集成到本地脚本逻辑的常见要求。)

  • Cygwin/OpenSSH提供了一个标准的POSIX shell环境。
    (高效的时间投资,基本的工具,跨平台就绪,兼容的习惯等等。)

  • 您仍然可以/总是可以运行所有本机的Windows应用程序。
    (包括使用cmd处理器自动执行*.bat文件。)

  • 您可以使用公钥配置无密码验证。
    (考虑一下无人值守的自动化任务。)


提示

起初我遇到了一个问题:背景下sshd服务必须在用户图形会话中执行应用程序,
(使应用程序窗口出现在桌面环境中)。

对我来说,可接受的解决方案是直接在用户的GUI会话中运行sshd服务
(当用户登录时自动启动,单击链接查看配置文件更改):

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

我实际上正在使用SSH,顺便说一下。问题是SSH无法启动GUI程序(即使该GUI程序没有GUI...) - Frew Schmidt
@Frew,这个提示中的链接专门解决了在Windows上远程运行GUI应用程序的问题。在我看来,在我需要处理它的上下文中,没有比这更简单和更和谐的解决方案了。如果所讨论的Windows主机存在纯服务器角色,则可能仍然需要在每次重新启动后自动登录此用户(以便桌面会话和sshd始终处于活动状态)。 - uvsmtid
我可能会再次研究它,但是我在此项目中遇到了相当严重的问题(自动测试NSIS安装程序),以至于我不得不完全放弃。 - Frew Schmidt
@Frew,事实上,我也需要远程GUI执行来进行(客户端)自动化测试。 - uvsmtid
好的,当我开始创建这个东西的安装程序(而不仅仅是更新程序)时,我会重新考虑的 :) - Frew Schmidt

0

来自http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/Q_22959948.html的已接受解决方案为:

我提供的是一个脚本,它需要参数... 在这种情况下,它需要4个参数。1)服务器:如果你传递-server,它只会对那个服务器进行操作。2)列表:你可以提供一个服务器列表文件。3)服务:你想要修改的服务名称。4)详细信息:在这里没有使用。
我在以下代码中更正了一些错误。将代码剪切/粘贴到名为Set-RemoteService.ps1的文件中。确保将执行策略设置为运行脚本... 默认情况下不会运行。你可以使用set-executionpolicy cmdlet来完成。PS> Set-Executionpolicy "RemoteSigned"来运行脚本,你需要输入 PS> C:\PathToScript\Set-RemoteService.ps1 -list c:\ServerList.txt -service "DHCP"
######################### Param($server,$list,$service,[switch]$verbose)
如果($Verbose){$VerbosePreference = "Continue"} 如果($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() } } 如果($server) { $query = "Select * from Win32_Service where Name='$service'" $myService = get-WmiObject -query $query -computer $server $myService.ChangeStartMode("Automatic") $myService.Start() }

该脚本启动远程计算机上的现有服务,但不运行任意脚本。 - Glen Little

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接