2

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

2 Answers 2

6

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();
Sign up to request clarification or add additional context in comments.

2 Comments

i also have this command "$cosmosDbContext = New-CosmosDbContext -Emulator " How can I put that in above method and then i have to execute this command "New-CosmosDbDatabase -Context $cosmosDbContext -Id 'NewlyCreatedDatabase' -AutoscaleThroughput 40000"
@ZZZSharePoint If you search for that you will find examples
0

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?

2 Comments

can you share a code example if you have one already
@ZZZSharePoint As I said I was away from my PC and on the way to sleep honestly or i would have brought up C# and found the proper code as above. Writing a file is simple in C# as is ShellExecute, I'm not much of a PowerShell user so i thought you had a basic knowledge of C# to have got that far. Sorry...seems you have gotten your answer anyways, though i do sometimes prefer the external file method. If your still interested in using that method, gladly. Let me know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.