user1108948
user1108948

Reputation:

How to invoke executing a PowerShell script from gulp?

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?

Upvotes: 4

Views: 7968

Answers (1)

Duderino9000
Duderino9000

Reputation: 2597

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

Upvotes: 11

Related Questions