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

发布于 2024-07-19 08:38:53 字数 159 浏览 2 评论 0原文

我想让 Windows 2003 服务器触发一个脚本,以触发另一台 Windows Server 2008 计算机中的另一个脚本。

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

有人对此有什么建议吗?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

瞄了个咪的 2024-07-26 08:38:53

来自 SysInternals 的 psexec

psexec from SysInternals

铁憨憨 2024-07-26 08:38:53

查看 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.

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.
皇甫轩 2024-07-26 08:38:53

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

: 将 cygwin 安装到远程电脑

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

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'

肥爪爪 2024-07-26 08:38:53

说到 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 / 退出状态传输到本地 shell 进行检查
    (将远程执行集成到本地脚本逻辑中的杀手级功能和常见要求)

  • Cygwin/OpenSSH 提供标准 POSIX shell 环境
    (高效的时间投资、基础工具、跨平台准备、兼容习惯等)

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

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


提示

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

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

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

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
遇见了你 2024-07-26 08:38:53

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

我提供的是一个脚本,它需要
参数...在本例中需要 4。
1)服务器:如果你通过-server它将
只在一台服务器上执行此操作 2) 列表:您
可以提供服务器列表文件。
3) 服务:您所使用的服务名称
想要修改 4) Verbose: 不是
此处使用。

我确实犯了一些错误
在以下代码中进行了更改。 使用
将代码剪切/粘贴到名为的文件中
设置-RemoteService.ps1。 确保
设置你的执行策略来运行
脚本...默认情况下不会。 你
通过使用来做到这一点
设置执行策略 cmdlet。 PS>
将执行策略“RemoteSigned”设置为
运行你做的脚本 PS>
C:\PathToScript\Set-RemoteService.ps1
-list c:\ServerList.txt -service "DHCP"

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

if($Verbose){$VerbosePreference =
“继续”} if($list) {
foreach($srv in (获取内容$list))
{
$query = "从 Win32_Service 中选择 *,其中 Name='$service'"
$myService = get-WmiObject -query $query -computer $srv
$myService.ChangeStartMode("自动")
$myService.Start()
} } if($服务器) {
$query = "从 Win32_Service 中选择 *,其中 Name='$service'"
$myService = get-WmiObject -query $query -computer $server
$myService.ChangeStartMode("自动")
$myService.Start() }

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文