为什么Node.js会忽略我的PowerShell ExecutionPolicy设置?
我有一个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');
当我运行它(不pipe.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
我已经validationget-executionpolicy
返回unrestricted
,所以给了什么?
奖金问题一旦我在IIS中托pipe此过程,我将如何解决这个相同的问题?
更新这不起作用: var child = child_process.spawn('powershell.exe', ['-ExecutionPolicy bypass', '.\\deploy.ps1']);
这也不是:设置executionpolicy在C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe
var child = child_process.spawn('powershell.exe', ['.\\deploy.ps1']);
,然后做var child = child_process.spawn('powershell.exe', ['.\\deploy.ps1']);
但是这可以在命令行上运行: powershell -ExecutionPolicy Bypass .\deploy.ps1
这听起来像是你试图在不同的执行环境(x86 / x64)中运行PowerShell脚本,而不是将执行策略设置为无限制的。
简单的解决方法是将-ExecutionPolicy Bypass
添加到PowerShell命令行。 这将绕过执行策略并执行脚本。