ZZZSharePoint
ZZZSharePoint

Reputation: 1351

Use multiple Powershell commands in C#

In the method shown here, I would like to use multiple PowerShell cmdlets. How can I do that? Like in a PowerShell window, I do a Import Module and then run a command Start-Db. How can I do the same in this method? I am able to run Import-module command successfully, but how can I add another command?

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

Upvotes: 2

Views: 797

Answers (2)

marsze
marsze

Reputation: 17055

Use AddStatement to add multiple commands:

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

Upvotes: 6

BrainOverflow
BrainOverflow

Reputation: 34

This is an extremely lazy hack to the issue and you will likely get a far better answer before I will even be near a PC...but why not just write the script to a powershell file?

You can then execute it and keep it for future use...or if need be for any reason, delete it after the command to execute the script returns using a function that waits for it to complete?

Upvotes: 0

Related Questions