简体   繁体   English

如何在不使用完整路径的情况下远程永久执行命令?

[英]How to execute forever command remotely without using full path?

I am trying to execute “forever” command remotely using powershell but I am getting error 我正在尝试使用Powershell远程执行“永远”命令,但出现错误

'forever' is not recognized as an internal or external command “永远”不被识别为内部或外部命令

Here is what I am doing. 这是我在做什么。 I have the node js script "MyNodeScript.js" which executes the forever command. 我有执行永久命令的节点js脚本“ MyNodeScript.js”。 The script is on the WebServer "MyWebServer1" . 该脚本位于WebServer “ MyWebServer1”上 Node.Js and Forever is installed on MyWebServer1 globally. Node.Js和Forever全局安装在MyWebServer1上。

    var exec = require('child_process').exec;
    var _ = require('underscore');
    var winston = require('winston');
    var async = require('async');

    var mycommand = 'forever -p ./logs --sourceDir ../wf_manager --workingDir ../wf_manager start -a --uid "ServiceApp" ServiceApp.js'

    function start(callback) {  
        async.waterfall([
            function (cb) {
                executeCommand(mycommand, false, cb);
            }
        ], function done(err) {
            if (err) {
                winston.error('failed to start all instances by forever:' + err);
            } else {
                winston.info('successfully started all instances by forever');
            }
            callback();
        });
    }

    function executeCommand(command, skip, callback) {
        async.waterfall([
            function (cb) {
                exec(command, cb);
            }
        ], function done(err) {
            if (err) {
                if (skip) {
                    // skip the error
                    callback(null);
                } else {
                    callback(err);
                }
            } else {
                callback(null);
            }
        });
    }

    module.exports = {
        executeCommand: executeCommand,    
        start: start
    }

    start(function(){});

On the same MyWebServer1 under same folder i have a powershell script "MyPowerShellScript.ps1" which call this node script. 在同一文件夹下的同一MyWebServer1上 ,有一个Powershell脚本“ MyPowerShellScript.ps1”,该脚本称为该节点脚本。 The powershell script has only one line Powershell脚本只有一行

node D:\myfolder\maintenance\MyNodeScript.js

I can run this powershell script locally on MyWebServer1 and it works fine. 我可以在MyWebServer1上本地运行此powershell脚本,它运行良好。 But when i try to execute this powershell script as below from remote machine 但是当我尝试从远程计算机执行如下所示的powershell脚本时

 invoke-command -computername MyWebServer1 -filepath \\MyWebServer1\MyFolder\maintenance\MyPowerShellScript.ps1

i am getting error 我遇到错误

error: failed to start all instances by forever:Error: Command failed: C:\\Windows\\system32\\cmd.exe /s /c "forever -p ./logs --sourceDir ../wf_manager --workingDir ../wf_manager start -a --uid "ServiceApp" ServiceApp.js" 错误:永远无法启动所有实例:错误:命令失败:C:\\ Windows \\ system32 \\ cmd.exe / s / c“ forever -p ./logs --sourceDir ../wf_manager --workingDir ../wf_manager开始-a --uid“ ServiceApp” ServiceApp.js“
+ CategoryInfo : NotSpecified: (error: failed t...ServiceApp.js":String) [], RemoteException + CategoryInfo:未指定:(错误:失败... ServiceApp.js“:String)[],RemoteException
+ FullyQualifiedErrorId : NativeCommandError + FullyQualifiedErrorId:NativeCommandError
+ PSComputerName : MyWebServer1 + PSComputerName:MyWebServer1
'forever' is not recognized as an internal or external command, operable program or batch file. “永远”不被识别为内部或外部命令,可操作程序或批处理文件。

Note that i can execute the script remotely without any error if i update "MyNodeScript.js" and use full physical path for the forever command 请注意,如果我更新“ MyNodeScript.js”并对forever命令使用完整的物理路径,则可以远程执行脚本而不会出现任何错误。

 var mycommand = 'C:\\Users\\admin\\AppData\\Roaming\\npm\\forever -p ./logs --sourceDir ../wf_manager --workingDir ../wf_manager start -a --uid "ServiceApp" ServiceApp.js'

However i would like to use just forever command. 但是我只想使用永远的命令。 The path is already added as Environment variable on MyWebServer1 该路径已作为环境变量添加到MyWebServer1上

Try calling the ps1-file like this: 尝试像这样调用ps1-文件:

Invoke-Command -Scriptblock {powershell.exe -command {d:\blah\MyPowershellscript.ps1 } }

Via -filepath your specify a local scriptfile that should be executed remotely. 通过-filepath您可以指定应远程执行的本地脚本文件。 As your script is present on the target system, you can omit that. 由于您的脚本位于目标系统上,因此可以忽略该脚本。

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

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