Node.js 如何使用Gulp在不同文件夹中调用Powershell脚本

Node.js 如何使用Gulp在不同文件夹中调用Powershell脚本,node.js,powershell,gulp,Node.js,Powershell,Gulp,我能够在没有任何问题的情况下实施。现在,我的任务(下面)从/gulpfile.js移动到/tasks/database publish.js。在这个任务中,我使用var exec=require('child_process').exec运行我编写的相对于新的gulp任务文件位于。/DbDeploy.PS1中的Powershell(PS1) 在这个任务中,我正在执行我的ps1 gulp.task(“zzz_Run-DB-Deply”, 函数(回调){ log('running db deploy

我能够在没有任何问题的情况下实施。现在,我的任务(下面)从
/gulpfile.js
移动到
/tasks/database publish.js
。在这个任务中,我使用
var exec=require('child_process').exec
运行我编写的相对于新的gulp任务文件位于
。/DbDeploy.PS1
中的Powershell(PS1)

在这个任务中,我正在执行我的ps1

gulp.task(“zzz_Run-DB-Deply”,
函数(回调){
log('running db deploy');
plugins.exec('Powershell.exe-executionpolicy remotesigned-File..\DbDeploy.ps1',
函数(err、stdout、stderr){
控制台日志(stdout);
回调(err);
});
});
如果我从
任务
目录在Powershell中运行
Powershell.exe-executionpolicy remotesigned-File..\DbDeploy.ps1
,它可以正常工作。但是,当我运行
zzz_run-DB-Deploy
任务时,我不断得到一个异常:

进程已终止,代码为0


我怀疑的是
。\DbDeploy.ps1
。我认为
\D
被用作转义字符。但我不确定。我做错了什么?

找到了解决方案。问题是
插件从
gulpfile.js
转移到
tasks/gulp task.js

为了给每个人提供一些上下文,最好将任务粘贴到这里。我认为我可以使用
plugins
变量将所有引用的插件发送到我的
tasks
文件夹。不幸的是,我错了

module.exports=函数(gulp、config、插件){
"严格使用",;
log('开始数据库迁移');
var exec=require('child_process')。exec;
吞咽任务(“zzz_Run-DB-Deply”,
函数(回调){
log('running db deploy');
exec(“Powershell.exe-executionpolicy remotesigned-File..\DbDeploy.ps1”,
函数(err、stdout、stderr){
控制台日志(stderr);
控制台日志(stdout);
控制台日志(err);
回调(err);
});
});
gulp.task(“构建数据库项目”,
函数(回调){
log('running build DB project');
var目标=[“构建”];
if(config.runCleanBuilds){
目标=[“清理”、“构建”];
}
返回gulp.src(“./Project/*.csproj”)
.pipe(plugins.foreach(函数(流、文件){
回流
.pipe(plugins.debug({title:“Building”}))
.pipe(plugins.msbuild({
目标:目标,,
配置:config.buildConfiguration,
logCommand:false,
冗长:“最小”,
史都:没错,
errorOnFail:true,
maxcpucount:config.maxcpucount,
toolsVersion:config.toolsVersion
}));
}));
});
}
为了解决这个问题,我添加了一个新的
exe
变量引用
var exec=require('child_process')。exec
此时,运行
exec(“Powershell.exe pwd”)
向我显示,我的工作目录实际上是我的项目的根目录,其中
gulpfile.js
DbDeploy.ps1
都处于活动状态。因此,我知道将文件路径从
。/DbDeploy.ps1
更改为
DbDeploy.ps

exec(“Powershell.exe-executionpolicy remotesigned-File DbDeploy.ps1”,
函数(err、stdout、stderr){
控制台日志(stderr);
控制台日志(stdout);
控制台日志(err);
回调(err);
});

Javascript要求在字符串中转义反斜杠。像这样
Powershell.exe-executionpolicy remotesigned-File..\\DbDeploy.ps1
Yes,完全正确!有用吗?有用,但我意识到那不是我的问题。在下面发布了答案。在字符串文本中使用未加转义的反斜杠仍然是错误的。你也应该解决这个问题。