在C#中使用多个Powershell命令

c#

在此处显示的方法中,我想使用多个 PowerShell cmdlet。我怎样才能做到这一点?就像在 PowerShell 窗口中一样,我执行导入模块,然后运行命令Start-Db。我怎样才能在这种方法中做同样的事情?我能够Import-module成功运行命令,但是如何添加另一个命令?

[Fact]
public void TestGetRecordings()
{
    PowerShell ps = PowerShell.Create();
    ps.AddCommand( "Import-Module" ).AddArgument( "C:ProgramFilesAzure Cosmos DB EmulatorPSModulesMicrosoft.Azure.CosmosDB.Emulator" );
    ps.Invoke();
    ps.Commands.Clear();
}

回答

使用AddStatement添加多个命令:

ps
  .AddCommand("Import-Module").AddArgument("C:ProgramFilesAzure Cosmos DB EmulatorPSModulesMicrosoft.Azure.CosmosDB.Emulator")
  .AddStatement() // <= this is equivalent to writing ";" in PowerShell
  .AddCommand("Start-Db").AddArgument("...")
  .Invoke();


以上是在C#中使用多个Powershell命令的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>