1

我有一个Node.js应用程序,我试图在其中调用PowerShell

var app = require('express')(),
    child_process = require('child_process');

app.post('/deploy', function(req, res) {

    var errors = '';
    var child = child_process.spawn('powershell.exe', ['deploy.ps1']);

    child.stderr.on('data', function(data) {
        errors += data;
    });

    child.stderr.on('end', function() {
        if (errors) {
            console.log('Error:');
            console.log(errors);
        }
    })

    child.on('exit', function(code) {
        console.log('Powershell Script finished');
        if (!!code)
            console.log('I think it failed');
        else
            console.log('I think it worked.')
    });

    child.stdin.end();

    res.end('');
});

app.listen(3000);
console.log('Listening on port 3000');

当我运行它时(无论 .ps1 文件是否存在),我得到以下信息:

File C:\Users\jkodroff.INTERNAL.000\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 cannot be loaded because
the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details.
At line:1 char:2
+ . <<<<  'C:\Users\jkodroff.INTERNAL.000\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1'
    + CategoryInfo          : NotSpecified: (:) [], PSSecurityException
    + FullyQualifiedErrorId : RuntimeException

File C:\Users\jkodroff.INTERNAL.000\Code\gitpulldeploy.js\deploy.ps1 cannot be loaded because the execution of scripts is
disabled on this system. Please see "get-help about_signing" for more details.
At line:1 char:13
+ .\deploy.ps1 <<<<
    + CategoryInfo          : NotSpecified: (:) [], PSSecurityException
    + FullyQualifiedErrorId : RuntimeException

我已经验证了get-executionpolicy返回unrestricted,那么给出了什么?

奖励问题一旦我在IIS中托管此进程,我将如何解决同样的问题?

更新 这不起作用: var child = child_process.spawn('powershell.exe', ['-ExecutionPolicy bypass', '.\\deploy.ps1']);

这也不是:将 executionpolicy 设置为unrestriectedin C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe,然后执行 var child = child_process.spawn('powershell.exe', ['.\\deploy.ps1']);

但这确实适用于命令行: powershell -ExecutionPolicy Bypass .\deploy.ps1

4

1 回答 1

1

听起来您正试图在与将执行策略设置为无限制的环境不同的执行环境 (x86/x64) 中运行 PowerShell 脚本。

简单的解决方法是添加-ExecutionPolicy Bypass到 PowerShell 命令行。这将绕过执行策略并执行脚本。

于 2013-11-13T22:53:15.710 回答