有没有可能从Cake任务中调用PowerShell命令?

6
我正在从 Psake 构建工具转换到 Cake (https://cakebuild.net/)。我正在为一个相当大的解决方案(30+ 个项目)创建脚本,并且有(目前)9 种不同的部署。在 Psake 中,调用 PowerShell 命令非常简单,因为整个脚本都在 PowerShell 中运行。
但现在我有一个问题。我必须调用一些在 PowerShell 中运行的命令,但我找不到执行它们的方法。
以下是一些例子:
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'>}

我尝试使用StartAndReturnProcess(例如:StartAndReturnProcess(“taskkill /IM iisexpress.exe”);),但没有成功。 我还发现了Cake.IIS插件,它可能会解决我启动和停止IIS的问题,但我还想调用其他一些PS命令。

是否可以从任务中简单地调用PowerShell命令? 像这样:

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

感谢您的答复,此致敬礼,马丁。
1个回答

7

你见过 Cake.PowerShell 插件吗?

这里可以找到示例用法 here,但为了举个例子(来自 readme):

#addin "Cake.Powershell"

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

根据您所要实现的具体目标,在自述文件中还有更复杂的示例。


太棒了!很高兴听到这个 :-) - Gary Ewan Park
谢谢。 我很惭愧,在提问之前我没有找到这个插件。 :| - Martin Trampus
不需要感到羞耻。也许我们需要改变一些东西,使它们更易被发现。 - Gary Ewan Park
很遗憾,目前此功能仅适用于最大版本的Cake 2.3.0。似乎不支持Cake 3.0.0。请参见:https://cakebuild.net/extensions/cake-powershell/ - bytedev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接