1

I am calling powershell cmdlet from my below C# method and it fails at line Install-Module Name CosmosDb with the below error

"Exception calling "ShouldContinue" with "2" argument(s): "A command that prompts the user failed because the host program or the command type does not support user interaction. The host was attempting to request confirmation with the following message: PowerShellGet requires NuGet provider version '2.8.5.201' or newer to interact with NuGet-based repositories. The NuGet provider must be available in 'C:\Program Files\PackageManagement\ProviderAssemblies' or 'C:\Users\UserName\AppData\Local\PackageManagement\ProviderAssemblies'. You can also install the NuGet provider by running 'Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force'. Do you want PowerShellGet to install and import the NuGet provider now?"

C# Code:

{            
            InitialSessionState initialSessionState = InitialSessionState.CreateDefault();
            initialSessionState.ExecutionPolicy = ExecutionPolicy.Unrestricted;
            using Runspace runspace = RunspaceFactory.CreateRunspace( initialSessionState );
            string path = Path.Combine( CosmosDataFixture.Root , @"TestData\GetrRecording.ps1" );
            runspace.Open();
            Pipeline pipeline = runspace.CreatePipeline();
            pipeline.Commands.AddScript( path );
            Collection<PSObject> results = pipeline.Invoke();
            runspace.Close();
}

Powershell cmdlets in my ps1 script file

ECHO 'Y'|Import-Module "$env:ProgramFiles\Azure Cosmos DB Emulator\PSModules\Microsoft.Azure.CosmosDB.Emulator"
Install-Module PowershellGet -Force -Scope CurrentUser
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.208 -Force 
Install-Module -Name CosmosDB -Scope CurrentUser -Force

I needed 1st and last line here but error prompt me to add the two in middl

ZZZSharePoint
  • 1,163
  • 1
  • 19
  • 54

1 Answers1

0

As explained in this answer, you cannot use PowerShell commands that request interactive input from the user when you use the PowerShell SDK, so your only option is to avoid such interactive prompts, which, depending on the specific command, requires -Force or -Confirm:$false (though note that neither parameter may be supported).

  • Install-PackageProvider supports -Confirm, so you should try -Confirm:$false (possibly in combination with -Force) to see if that programmatically confirms the normally interactive prompt.

  • Import-Module supports -Force, but it doesn't itself ever prompt the user; instead -Force signals the intent to force-reload an already-imported module.

    • What can happen, however (as suggested by your feedback) is that module being imported tries to create an elevated (run-as-admin) process, which invariably makes the system put up a UAC (User Account Control) prompt ("Do you want to allow this app to make changes to your device?") to confirm the intent to perform this security-sensitive operation.

      • By design, this UAC prompt can not be answered programmatically, for security reasons.
    • Your only option is to run your program itself with elevation, such as from a PowerShell console that was started by right-clicking on the PowerShell icon and selecting Run as Administrator.


As for what you tried:

Note:

  • The following makes a general point about programmatically responding to PowerShell prompts. As stated above, it isn't actually Import-Module itself that puts up prompts, but in your case it is the specific module being imported; that prompt ("Do you want this app to make changes to your computer?") is a UAC prompt put up by the system and can by design not be answered programmatically.
ECHO 'Y' | Import-Module ...

invariably fails, because you fundamentally cannot answer PowerShell's interactive prompts using (PowerShell) pipeline input.

  • The only way to programmatically respond to interactive PowerShell prompts is if the PowerShell CLI is called and the responses are provided via stdin - see this answer.

Presumably you didn't see the failure (Import-Module: The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.), because it isn't directly reported by the .Invoke() return value (which only comprises success output); to check for errors (the error stream), use pipeline.Error

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • I tried to follow the other answer in your post but couldnt figure out how can i use stdin in my code? can you show that with my code? – ZZZSharePoint Jun 29 '21 at 15:14
  • If we're talking about the UAC prompt ("Do you want to allow this app to make changes to your device?"), there is no solution. It is a prompt shown by the _system_, not PowerShell, and it is designed _not_ to allow responding to _programmatically_. Hypothetically, you can disable UAC altogether, but that is strongly discouraged for security reasons. – mklement0 Jun 29 '21 at 15:20