4

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?

4
  • You can just use node. Possible duplicate: stackoverflow.com/a/10181488/197472 Commented Mar 24, 2016 at 20:43
  • @Barryman9000, can I append the code after my build task? Commented Mar 24, 2016 at 21:01
  • you can just run another task after build that runs the powershell/node code Commented Mar 24, 2016 at 21:18
  • @Barryman9000, then how to add it in gulp? Commented Mar 28, 2016 at 12:35

1 Answer 1

11

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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

My PowerShell script needs some parameters as arguments. Not sure how to deal with it.
You can just add those param in your task. See my edit
I mean arguments such as function Publish-AspNetDocker { [cmdletbinding(SupportsShouldProcess = $true)] param( [Parameter(Mandatory = $true, Position = 0)] [AllowNull()] $publishProperties, [Parameter(Mandatory = $true, Position = 1)] $packOutput, [Parameter(Mandatory = $false, Position = 2)] $pubxmlFile )
That is great, sorry for my weakness on PS. So how to put the args in exec('Powershell.exe -executionpolicy remotesigned -File file.ps1'?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.