1

I have a Powershell script which I have invoked from a C# method. While doing it locally, I get a prompt where I have to select Yes or No and I select YES and things work as expected. But when pushed to server, it fails, I would like to know how can I select default as YES in my code so that the same goes in server and I don't have to worry about. here is my C# method and powershell script which is invoked after which i get a prompt.

public void CreateDatabaseFromPowerShell()
        {
            string path = Path.Combine( Root, @"TestData\CosmosScript.ps1" );
            Runspace runspace = RunspaceFactory.CreateRunspace();
            runspace.Open();
            Pipeline pipeline = runspace.CreatePipeline();
            pipeline.Commands.AddScript( path );
            Collection<PSObject> results = pipeline.Invoke();
            runspace.Close();           
        }

----Import-Module "$env:ProgramFiles\Azure Cosmos DB Emulator\PSModules\Microsoft.Azure.CosmosDB.Emulator"
----Start-CosmosDbEmulator  
ZZZSharePoint
  • 1,163
  • 1
  • 19
  • 54

1 Answers1

0

When you use the PowerShell SDK, interactive prompts cannot be responded to (programmatically), so any PowerShell command executed via the SDK that tries to ask the user for interactive input fails.

Here's a simple demonstration from PowerShell:

PS> ($ps = [PowerShell]::Create()).AddCommand('Read-Host').Invoke()

Exception calling "Invoke" with "0" argument(s):
"A command that prompts the user failed because the host program or the command type 
does not support user interaction [...]"

Your only option is to avoid such prompts, with command parameters such as -Force and -Confirm:$false - check the syntax of the command of interest to see which parameter applies (or possibly both).

Note that while Import-Module does have a -Force parameter (but no -Confirm parameter), it never requests interactive input itself. (The module being imported may trigger a prompt, however, such as the system's UAC prompt to confirm the intent to run with elevation (as admin) - see this answer.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • Please see my detailed explanation [here](https://stackoverflow.com/a/68180541/45375). If something is still unclear, please ask there. – mklement0 Jun 29 '21 at 14:39