代码之家  ›  专栏  ›  技术社区  ›  Evan Carroll

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

  •  9
  • Evan Carroll  · 技术社区  · 15 年前

    我想让Windows2003服务器在单独的WindowsServer2008计算机上启动一个脚本来启动另一个脚本。

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

    有人对此有什么建议吗?

    谢谢!

    5 回复  |  直到 9 年前
        1
  •  14
  •   benPearce    15 年前

    psexec 来自系统内部

        2
  •  6
  •   cdonner    15 年前

    查看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.
    
        3
  •  3
  •   pkm    11 年前

    最简单的使用方法有两个步骤

    a.将Cygwin安装到远程PC

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

        4
  •  1
  •   uvsmtid    11 年前

    谈论 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状态为 地方的 检查用外壳
      (杀手特性和将远程执行集成到本地脚本逻辑中的常见要求)

    • cygwin/openssh提供 标准 POSIX外壳环境
      (高效的时间投资、基本工具、跨平台就绪、兼容习惯等)

    • 你仍然可以/总是跑步 全乡土 Windows应用程序
      (包括自动执行 *.bat 文件通过 cmd 处理器)

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


    小费

    最初我有一个要求:
    背景 sshd 服务必须在用户的图形会话中执行应用程序
    (使应用程序窗口出现在桌面环境中)。

    这个 acceptable solution 因为我在跑步 SSHD 服务 直接地 在用户的GUI会话中
    (用户登录时自动启动,按照链接查看配置文件更改):

    /usr/sbin/sshd -f /home/user/sshd_config
    
        5
  •  0
  •   Jonathan Parker    15 年前

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

    我提供的是一个脚本 参数。。。在这种情况下,需要4个。 1)服务器:如果通过-服务器,它将 只做一个服务器2)列表:你 可以提供服务器的列表文件。 3)服务:您的服务名称 要修改4)verbose:不是 用在这里。

    我确实犯了一些错误 以下代码已更改。使用 将代码剪切/粘贴到名为 set-remoteservice.ps1.确保 将ExecutionPolicy设置为运行 脚本…默认情况下不会。你 通过使用 设置ExecutionPolicy Cmdlet。PS & GT; 将ExecutionPolicy“RemoteSigned”设置为 运行执行的脚本ps> C:\pathtoscript\set-remoteservice.ps1 -列表C:\serverlist.txt-服务“dhcp”

    #########################参数($server,$list,$service,[开关]$verbose)

    如果($verbose)$verbosepreference= “继续”如果($list){ foreach($srv-in(获取内容$list))。 { $query=“select*from win32_service,其中name='$service'” $myService=get wmiobject-查询$query-计算机$srv $myservice.changestartmode(“自动”) $myservice.start()。 }}如果($server){ $query=“select*from win32_service,其中name='$service'” $myService=get wmiobject-查询$query-计算机$server $myservice.changestartmode(“自动”) $myservice.start()

    推荐文章