Can't execute powershell commands from the code in the docker container or azure webjob.

Deleted
Not applicable

Hello,

 

I have a question about powershell core in the docker container or in the azure webapp.
I develop some webjob(.net core app) that tries to execute powershell commands using the Microsoft.PowerShell.Commands.Diagnostics, Microsoft.PowerShell.SDK, Microsoft.WSMan.Management packages(version 6.2.0).
In my local environment(I've installed powershell 6 and I have powershell 5 as default) the webjob works correct without any issues. Then I try to deploy the webjob in the linux docker container. The dockerfile has some instruction for installing powershell core 6.2.0 in the container. So, docker container(OS Debian 9) with my webjob runs with installed powershell core. Then my code tries to execute the following command:
using (PowerShell ps = PowerShell.Create())
{
   ps.AddScript("Get-PackageProvider -ListAvailable");
   var results = ps.Invoke();
}

 

and after that I get the following error:

Unhandled Exception: System.AggregateException: One or more errors occurred. (The term 'Get-PackageProvider' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.) ---> System.Management.Automation.CommandNotFoundException: The term 'Get-PackageProvider' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
at System.Management.Automation.CommandDiscovery.LookupCommandInfo(String commandName, CommandTypes commandTypes, SearchResolutionOptions searchResolutionOptions, CommandOrigin commandOrigin, ExecutionContext context)
at System.Management.Automation.CommandDiscovery.LookupCommandInfo(String commandName, CommandOrigin commandOrigin, ExecutionContext context)
at System.Management.Automation.CommandDiscovery.LookupCommandInfo(String commandName, CommandOrigin commandOrigin)
at System.Management.Automation.CommandDiscovery.LookupCommandProcessor(String commandName, CommandOrigin commandOrigin, Nullable`1 useLocalScope)
at System.Management.Automation.ExecutionContext.CreateCommand(String command, Boolean dotSource)
at System.Management.Automation.PipelineOps.AddCommand(PipelineProcessor pipe, CommandParameterInternal[] commandElements, CommandBaseAst commandBaseAst, CommandRedirection[] redirections, ExecutionContext context)
at System.Management.Automation.PipelineOps.InvokePipeline(Object input, Boolean ignoreInput, CommandParameterInternal[][] pipeElements, CommandBaseAst[] pipeElementAsts, CommandRedirection[][] commandRedirections, FunctionContext funcContext)
at System.Management.Automation.Interpreter.ActionCallInstruction`6.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
--- End of inner exception stack trace ---

 

The same issue I have in azure. When I run webjob I get that error.
I've already googled a lot of sources and tried a lot of different solutions but any of them didn't help me.
Could you help me, please?
Why this issue isn't reproduce in my local environment but only in azure and container?

Thanks!

1 Reply
I have the same issue. Were you able to fix it?