Is it possible to call powershell command from within Cake task
Asked Answered
H

1

6

I'm switching from Psake build tool to Cake (https://cakebuild.net/). I'm creating a scripts for quite big solution (30+ projects) with (currently) 9 different deploys. In Psake it was trivial to call PowerShell commands, since entire script has been running in powershell.

But now I have a problem. I have to call some commands that are running in PS, but I can't find a way to execute them.

Some examples:

taskkill /IM iisexpress.exe
Invoke-Command -ComputerName <testing server> -ScriptBlock {import-module WebAdministration; Stop-Website <'name of website'>}
Invoke-Command -ComputerName <testing server> -ScriptBlock {import-module WebAdministration; Start-Website <'name of website'>}

I've tried with StartAndReturnProcess (example: StartAndReturnProcess("taskkill /IM iisexpress.exe");), but without success. I've also found Cake.IIS plugin, which would probbably solve my issues with starting and stopping IIS, but there are also smoe other PS commands that I'd like to invoke.

Is it possible to simple call a PowerShell command from Task? Something like this:

Task("DoSomeMagic")
    .Does(() =>
{

    ExecutePowerShellCommad("taskkill /IM iisexpress.exe");
    ExecutePowerShellCommad("Invoke-Command -ComputerName <testing server> -ScriptBlock {import-module WebAdministration; Stop-Website <'name of website'>}");

    //ToDo: other magic
});

Thanx for any answer and best regards, Martin

Hadleigh answered 27/11, 2017 at 16:46 Comment(0)
N
7

Have you seen the Cake.PowerShell addin?

Example usage can be found here, but to provide an example (taken from readme):

#addin "Cake.Powershell"

Task("Powershell-Script")
    .Description("Run an example powershell command with parameters")
    .Does(() =>
{
    StartPowershellScript("Write-Host", args =>
        {
            args.AppendQuoted("Testing...");
        });
});

There are more complicated examples in the readme depending on exactly what you are trying to achieve.

Naivete answered 27/11, 2017 at 19:16 Comment(4)
Woot! Glad to hear it :-)Naivete
Thanx. I'm quite ashamed that I didn't find this addin before I asked. :|Hadleigh
No need to be ashamed. Perhaps we need to change something to make them more discoverable.Naivete
Unfortunately as of now this only works with max ver Cake 2.3.0. Cake 3.0.0 does not seem to be supported. See: cakebuild.net/extensions/cake-powershellGas

© 2022 - 2024 — McMap. All rights reserved.