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
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.
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.
% is an alias for the ForEach-Object cmdlet. An alias is just another name by which you can reference a cmdlet or function.
'; 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With