Home

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

%3CLINGO-SUB%20id%3D%22lingo-sub-482040%22%20slang%3D%22en-US%22%3ECan't%20execute%20powershell%20commands%20from%20the%20code%20in%20the%20docker%20container%20or%20azure%20webjob.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-482040%22%20slang%3D%22en-US%22%3E%3CP%3EHello%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20a%20question%20about%20powershell%20core%20in%20the%20docker%20container%20or%20in%20the%20azure%20webapp.%3CBR%20%2F%3EI%20develop%20some%20webjob(.net%20core%20app)%20that%20tries%20to%20execute%20powershell%20commands%20using%20the%20Microsoft.PowerShell.Commands.Diagnostics%2C%20Microsoft.PowerShell.SDK%2C%20Microsoft.WSMan.Management%20packages(version%206.2.0).%3CBR%20%2F%3EIn%20my%20local%20environment(I've%20installed%20powershell%206%20and%20I%20have%20powershell%205%20as%20default)%20the%20webjob%20works%20correct%20without%20any%20issues.%20Then%20I%20try%20to%20deploy%20the%20webjob%20in%20the%20linux%20docker%20container.%20The%20dockerfile%20has%20some%20instruction%20for%20installing%20powershell%20core%206.2.0%20in%20the%20container.%20So%2C%20docker%20container(OS%20Debian%209)%20with%20my%20webjob%20runs%20with%20installed%20powershell%20core.%20Then%20my%20code%20tries%20to%20execute%20the%20following%20command%3A%3CBR%20%2F%3Eusing%20(PowerShell%20ps%20%3D%20PowerShell.Create())%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3Bps.AddScript(%22Get-PackageProvider%20-ListAvailable%22)%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3Bvar%20results%20%3D%20ps.Invoke()%3B%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eand%20after%20that%20I%20get%20the%20following%20error%3A%3C%2FP%3E%3CP%3E%3CEM%3EUnhandled%20Exception%3A%20System.AggregateException%3A%20One%20or%20more%20errors%20occurred.%20(The%20term%20'Get-PackageProvider'%20is%20not%20recognized%20as%20the%20name%20of%20a%20cmdlet%2C%20function%2C%20script%20file%2C%20or%20operable%20program.%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3ECheck%20the%20spelling%20of%20the%20name%2C%20or%20if%20a%20path%20was%20included%2C%20verify%20that%20the%20path%20is%20correct%20and%20try%20again.)%20---%26gt%3B%20System.Management.Automation.CommandNotFoundException%3A%20The%20term%20'Get-PackageProvider'%20is%20not%20recognized%20as%20the%20name%20of%20a%20cmdlet%2C%20function%2C%20script%20file%2C%20or%20operable%20program.%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3ECheck%20the%20spelling%20of%20the%20name%2C%20or%20if%20a%20path%20was%20included%2C%20verify%20that%20the%20path%20is%20correct%20and%20try%20again.%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3Eat%20System.Management.Automation.CommandDiscovery.LookupCommandInfo(String%20commandName%2C%20CommandTypes%20commandTypes%2C%20SearchResolutionOptions%20searchResolutionOptions%2C%20CommandOrigin%20commandOrigin%2C%20ExecutionContext%20context)%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3Eat%20System.Management.Automation.CommandDiscovery.LookupCommandInfo(String%20commandName%2C%20CommandOrigin%20commandOrigin%2C%20ExecutionContext%20context)%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3Eat%20System.Management.Automation.CommandDiscovery.LookupCommandInfo(String%20commandName%2C%20CommandOrigin%20commandOrigin)%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3Eat%20System.Management.Automation.CommandDiscovery.LookupCommandProcessor(String%20commandName%2C%20CommandOrigin%20commandOrigin%2C%20Nullable%601%20useLocalScope)%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3Eat%20System.Management.Automation.ExecutionContext.CreateCommand(String%20command%2C%20Boolean%20dotSource)%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3Eat%20System.Management.Automation.PipelineOps.AddCommand(PipelineProcessor%20pipe%2C%20CommandParameterInternal%5B%5D%20commandElements%2C%20CommandBaseAst%20commandBaseAst%2C%20CommandRedirection%5B%5D%20redirections%2C%20ExecutionContext%20context)%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3Eat%20System.Management.Automation.PipelineOps.InvokePipeline(Object%20input%2C%20Boolean%20ignoreInput%2C%20CommandParameterInternal%5B%5D%5B%5D%20pipeElements%2C%20CommandBaseAst%5B%5D%20pipeElementAsts%2C%20CommandRedirection%5B%5D%5B%5D%20commandRedirections%2C%20FunctionContext%20funcContext)%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3Eat%20System.Management.Automation.Interpreter.ActionCallInstruction%606.Run(InterpretedFrame%20frame)%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3Eat%20System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame%20frame)%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E---%20End%20of%20inner%20exception%20stack%20trace%20---%3C%2FEM%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20same%20issue%20I%20have%20in%20azure.%20When%20I%20run%20webjob%20I%20get%20that%20error.%3CBR%20%2F%3EI've%20already%20googled%20a%20lot%20of%20sources%20and%20tried%20a%20lot%20of%20different%20solutions%20but%20any%20of%20them%20didn't%20help%20me.%3CBR%20%2F%3ECould%20you%20help%20me%2C%20please%3F%3CBR%20%2F%3EWhy%20this%20issue%20isn't%20reproduce%20in%20my%20local%20environment%20but%20only%20in%20azure%20and%20container%3F%3C%2FP%3E%3CP%3EThanks!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-482040%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3E.Net%20Core%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EAzure%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Highlighted
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!

Related Conversations
Tabs and Dark Mode
cjc2112 in Discussions on
48 Replies
Extentions Synchronization
Deleted in Discussions on
3 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies
How to Prevent Teams from Auto-Launch
chenrylee in Microsoft Teams on
30 Replies
flashing a white screen while open new tab
Deleted in Discussions on
14 Replies
Security Community Webinars
Valon_Kolica in Security, Privacy & Compliance on
13 Replies