如何从gulp调用执行PowerShell脚本? [英] How to invoke executing a PowerShell script from gulp?

查看:1424
本文介绍了如何从gulp调用执行PowerShell脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  var msbuild = require('gulp-msbuild');我们正在使用gulp来构建和部署我们的应用程序。 
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,$ b $ stderr:true,
stdout:true,
fileLoggerParameters:'LogFile = Build.log; Append; Verbosity = detailed',
}));
});

然而,在构建之后,我必须调用PowerShell脚本文件publish.ps1,我该如何调用它在吞噬?

解决方案

我还没有测试过这个,但是如果你将两者结合起来,它会看起来像这样。

  var gulp = require('gulp')只运行缺省任务,该任务使用run-sequence来管理依赖项顺序。 ,
runSequence = require('run-sequence'),
msbuild = require('gulp-msbuild'),
spawn = require(child_process)。spawn,
儿童;
$ b gulp.task('default',function(){
runSequence('clean','build','powershell');
});
$ b gulp.task('build',['clean'],function(){
return gulp.src('../../*。sln')
.pipe(msbuild({
toolsVersion:14.0,
targets:['Rebuild'],
errorOnFail:true,
属性:{
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。 (Powershell Data:+ data);
});
child.stderr.on(data,function(data){
console.log(Powershell Errors:+ data);
});
child.on(退出,函数(){
console.log(Powershell脚本完成);
});
child.stdin.end(); //结束输入
回调();
});

编辑

<使用参数调用powershell文件

  var exec = require(child_process)。exec; 
$ b gulp.task(powershell,function(callback){
exec(
Powershell.exe -executionpolicy remotesigned -File file.ps1,
function (err,stdout,stderr){
console.log(stdout);
callback(err);
}
);
});

Powershell file.ps1位于解决方案的根目录中



Write-Host'hello'



编辑2



好的,再试一次。你可以把params /参数放在file.ps1中吗?

$ $ $ $ $ $ $ $ $ $ $ $ $写入输出$ arg1;
写入输出$ arg2;

Write-Stuff -arg1hello-arg2See Ya

编辑3



通过吞咽任务传递参数::

  gulp.task('powershell',function(callback){
exec(Powershell.exe -executionpolicy remotesigned。。\\file.ps1; Write-Stuff -arg1 '我的第一个参数'-arg2'第二个'here',函数(err,stdout,stderr){
console.log(stdout);
callback(err)
});
});

更新file.ps1以删除

函数Write-Stuff([string] $ arg1,[string] $ arg2){
Write-Output $ arg1;
写入输出$ arg2;
}


I am using gulp to build and deploy our application.

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',
    }));
});

However after build I have to call a PowerShell script file "publish.ps1", how can I call it in gulp?

解决方案

I haven't tested this but if you combine the two it would look something like this. just run the default task, which uses run-sequence to manage the dependency order.

    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();
    });

EDIT

Call a powershell file with parameters

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 file.ps1 in the root of your solution

Write-Host 'hello'

EDIT 2

OK, one more try. Can you put the params/arguments in file.ps1?

function Write-Stuff($arg1, $arg2){
    Write-Output $arg1;
    Write-Output $arg2;
}
Write-Stuff -arg1 "hello" -arg2 "See Ya"

EDIT 3

Pass the params from the gulp task::

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)
    });
});

Update file.ps1 to remove

function Write-Stuff([string]$arg1, [string]$arg2){
    Write-Output $arg1;
    Write-Output $arg2;
}

这篇关于如何从gulp调用执行PowerShell脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆