繁体   English   中英

如何在Windows中远程执行脚本?

[英]How can I remotely execute a script in Windows?

我想让一个Windows 2003服务器触发一个脚本来在另一个Windows Server 2008计算机中触发另一个脚本。

我被告知Powershell可以做到这一点,这很好,但我需要更具体的细节。

有没有人对此有任何提示?

谢谢!

来自SysInternals的psexec

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

AT命令安排命令和程序在指定的时间和日期在计算机上运行。 必须运行Schedule服务才能使用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.

最简单的使用方式将分两步进行

一个。 将cygwin安装到远程PC

运行ssh hudson @ mcs'/cygdrive/c/path_to_script.bat'

谈到PsExec ,我强烈建议改用Cygwin / OpenSSH。

SSH具有多种优势(超过PsExec工具甚至是定制服务)。
例如,尝试使用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将(!)远程stdout / stderr / exit状态传输到本地 shell进行检查
    (将远程执行集成到本地脚本逻辑中的杀手级功能和常见要求)

  • Cygwin / OpenSSH提供标准的 POSIX shell环境
    (有效的时间投入,基本工具,跨平台准备,兼容的习惯等)

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

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


小费

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

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

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

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

我提供的是一个带参数的脚本......在这种情况下它需要4. 1)服务器:如果你传递-server它只会做那个服务器2)列表:你可以提供服务器的列表文件。 3)服务:您要修改的服务的名称4)详细:此处不使用。

我确实有一些错误,我在下面的代码中更改了。 使用剪切/粘贴代码到名为Set-RemoteService.ps1的文件中。 确保将executionpolicy设置为运行脚本...默认情况下不会。 您可以使用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)

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()}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM