为什么Node.js会忽略我的PowerShell ExecutionPolicy设置?

时间:2013-11-13 22:48:09

标签: node.js powershell

我有一个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']);

这两个都没有: 在unrestriected中将executionpolicy设置为C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe,然后执行 var child = child_process.spawn('powershell.exe', ['.\\deploy.ps1']);

在命令行工作: powershell -ExecutionPolicy Bypass .\deploy.ps1

1 个答案:

答案 0 :(得分:1)

听起来您尝试在不同的执行环境(x86 / x64)中运行PowerShell脚本,而不是将执行策略设置为不受限制的环境。

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