Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to call powershell command from within Cake task

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

like image 677
Martin Trampus Avatar asked Nov 27 '17 16:11

Martin Trampus


People also ask

How do I run a command within a PowerShell script?

Running a PowerShell script from the Command Prompt If you would like to run a PowerShell script in CMD, you'll need to execute it by calling the PowerShell process with the -File parameter, as shown below: PowerShell -File C:\TEMP\MyNotepadScript. ps1. PowerShell -File C:\TEMP\MyNotepadScript.

How do I run a PowerShell command in DOS?

The PowerShell Command Line If you want to run a specific script, then either in the command line or in the Start Menu you can type "powershell.exe -File" followed by the file that contains the script.

What does %% mean in PowerShell?

% is an alias for the ForEach-Object cmdlet. An alias is just another name by which you can reference a cmdlet or function.

How do I call a PowerShell script from Javascript?

'; var shell=new ActiveXObject('WScript. Shell'); var std=shell. Exec("C:\\Windows\\System32\\WindowsPowerShell\\v1. 0\\powershell.exe -ExecutionPolicy Bypass -command \ $OutputEncoding = [Console]::outputEncoding = [System.


1 Answers

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.

like image 75
Gary Ewan Park Avatar answered Oct 17 '22 04:10

Gary Ewan Park