Node.js 如何从gulp调用执行PowerShell脚本?
我正在使用gulp构建和部署我们的应用程序Node.js 如何从gulp调用执行PowerShell脚本?,node.js,powershell,msbuild,visual-studio-2015,gulp,Node.js,Powershell,Msbuild,Visual Studio 2015,Gulp,我正在使用gulp构建和部署我们的应用程序 var msbuild = require('gulp-msbuild'); gulp.task('build', ['clean'], function () { return gulp.src('../../*.sln') .pipe(msbuild({ toolsVersion: 14.0, targets: ['Rebuild'], errorOnFail: true, p
var msbuild = require('gulp-msbuild');
gulp.task('build', ['clean'], function () {
return gulp.src('../../*.sln')
.pipe(msbuild({
toolsVersion: 14.0,
targets: ['Rebuild'],
errorOnFail: true,
properties: {
DeployOnBuild: true,
DeployTarget: 'Package',
PublishProfile: 'Development'
},
maxBuffer: 2048 * 1024,
stderr: true,
stdout: true,
fileLoggerParameters: 'LogFile=Build.log;Append;Verbosity=detailed',
}));
});
但是,在构建之后,我必须调用PowerShell脚本文件“publish.ps1”,我如何才能在gulp中调用它?我还没有测试过这一点,但是如果将这两个脚本文件结合起来,它看起来会像这样。只需运行默认任务,该任务使用运行序列来管理依赖项顺序
var gulp = require('gulp'),
runSequence = require('run-sequence'),
msbuild = require('gulp-msbuild'),
spawn = require("child_process").spawn,
child;
gulp.task('default', function(){
runSequence('clean', 'build', 'powershell');
});
gulp.task('build', ['clean'], function () {
return gulp.src('../../*.sln')
.pipe(msbuild({
toolsVersion: 14.0,
targets: ['Rebuild'],
errorOnFail: true,
properties: {
DeployOnBuild: true,
DeployTarget: 'Package',
PublishProfile: 'Development'
},
maxBuffer: 2048 * 1024,
stderr: true,
stdout: true,
fileLoggerParameters: 'LogFile=Build.log;Append;Verbosity=detailed',
}));
});
gulp.task('powershell', function(callback){
child = spawn("powershell.exe",["c:\\temp\\helloworld.ps1"]);
child.stdout.on("data",function(data){
console.log("Powershell Data: " + data);
});
child.stderr.on("data",function(data){
console.log("Powershell Errors: " + data);
});
child.on("exit",function(){
console.log("Powershell Script finished");
});
child.stdin.end(); //end input
callback();
});
编辑
使用参数调用powershell文件
var exec = require("child_process").exec;
gulp.task("powershell", function(callback) {
exec(
"Powershell.exe -executionpolicy remotesigned -File file.ps1",
function(err, stdout, stderr) {
console.log(stdout);
callback(err);
}
);
});
解决方案根目录中的Powershell文件.ps1
写主机“你好”
编辑2
好的,再试一次。能否将参数/参数放入file.ps1中
function Write-Stuff($arg1, $arg2){
Write-Output $arg1;
Write-Output $arg2;
}
Write-Stuff -arg1 "hello" -arg2 "See Ya"
编辑3
从gulp任务中传递参数::
gulp.task('powershell', function (callback) {
exec("Powershell.exe -executionpolicy remotesigned . .\\file.ps1; Write-Stuff -arg1 'My first param' -arg2 'second one here'" , function(err, stdout, stderr){
console.log(stdout);
callback(err)
});
});
更新file.ps1以删除
function Write-Stuff([string]$arg1, [string]$arg2){
Write-Output $arg1;
Write-Output $arg2;
}
您可以只使用node。可能重复:@Barryman9000,我可以在生成任务后附加代码吗?您可以在运行powershell/节点的
build
之后运行另一个任务code@Barryman9000,那么如何将其添加到gulp中?我的PowerShell脚本需要一些参数作为参数。不知道如何处理。您可以在任务中添加这些参数。请参阅我的editI mean参数,例如函数Publish AspNetDocker{[cmdletbinding(SupportsShouldProcess=$true)]param([Parameter(Mandatory=$true,Position=0)][AllowNull()]$publishProperties,[Parameter(Mandatory=$true,Position=1)]$packOutput,[Parameter(必需=$false,Position=2)]$pubxmlFile)
太好了,很抱歉我在PS上的弱点。那么如何将参数放入exec('Powershell.exe-executionpolicy remotesigned-File File.ps1'
?