1919namespace Microsoft . Azure . Functions . PowerShellWorker
2020{
2121 using System . Diagnostics ;
22- using System . IO ;
2322 using LogLevel = Microsoft . Azure . WebJobs . Script . Grpc . Messages . RpcLog . Types . Level ;
2423
2524 internal class RequestProcessor
2625 {
2726 private readonly MessagingStream _msgStream ;
27+ private readonly System . Management . Automation . PowerShell _firstPwshInstance ;
2828 private readonly PowerShellManagerPool _powershellPool ;
2929 private DependencyManager _dependencyManager ;
3030
@@ -37,9 +37,10 @@ internal class RequestProcessor
3737 private Dictionary < StreamingMessage . ContentOneofCase , Func < StreamingMessage , StreamingMessage > > _requestHandlers =
3838 new Dictionary < StreamingMessage . ContentOneofCase , Func < StreamingMessage , StreamingMessage > > ( ) ;
3939
40- internal RequestProcessor ( MessagingStream msgStream )
40+ internal RequestProcessor ( MessagingStream msgStream , System . Management . Automation . PowerShell firstPwshInstance )
4141 {
4242 _msgStream = msgStream ;
43+ _firstPwshInstance = firstPwshInstance ;
4344 _powershellPool = new PowerShellManagerPool ( ( ) => new RpcLogger ( msgStream ) ) ;
4445
4546 // Host sends capabilities/init data to worker
@@ -194,18 +195,12 @@ internal StreamingMessage ProcessFunctionLoadRequest(StreamingMessage request)
194195 _dependencyManager = new DependencyManager ( request . FunctionLoadRequest . Metadata . Directory , logger : rpcLogger ) ;
195196 var managedDependenciesPath = _dependencyManager . Initialize ( request , rpcLogger ) ;
196197
197- // Setup the FunctionApp root path and module path.
198- FunctionLoader . SetupWellKnownPaths ( functionLoadRequest , managedDependenciesPath ) ;
198+ SetupAppRootPathAndModulePath ( functionLoadRequest , managedDependenciesPath ) ;
199199
200- // Create the very first Runspace so the debugger has the target to attach to.
201- // This PowerShell instance is shared by the first PowerShellManager instance created in the pool,
202- // and the dependency manager (used to download dependent modules if needed).
203- var pwsh = Utils . NewPwshInstance ( ) ;
204- LogPowerShellVersion ( rpcLogger , pwsh ) ;
205- _powershellPool . Initialize ( pwsh ) ;
200+ _powershellPool . Initialize ( _firstPwshInstance ) ;
206201
207202 // Start the download asynchronously if needed.
208- _dependencyManager . StartDependencyInstallationIfNeeded ( request , pwsh , rpcLogger ) ;
203+ _dependencyManager . StartDependencyInstallationIfNeeded ( request , _firstPwshInstance , rpcLogger ) ;
209204
210205 rpcLogger . Log ( isUserOnlyLog : false , LogLevel . Trace , string . Format ( PowerShellWorkerStrings . FirstFunctionLoadCompleted , stopwatch . ElapsedMilliseconds ) ) ;
211206 }
@@ -493,10 +488,19 @@ private static void BindOutputFromResult(InvocationResponse response, AzFunction
493488 }
494489 }
495490
496- private static void LogPowerShellVersion ( RpcLogger rpcLogger , System . Management . Automation . PowerShell pwsh )
491+ private void SetupAppRootPathAndModulePath ( FunctionLoadRequest functionLoadRequest , string managedDependenciesPath )
497492 {
498- var message = string . Format ( PowerShellWorkerStrings . PowerShellVersion , Utils . GetPowerShellVersion ( pwsh ) ) ;
499- rpcLogger . Log ( isUserOnlyLog : false , LogLevel . Information , message ) ;
493+ FunctionLoader . SetupWellKnownPaths ( functionLoadRequest , managedDependenciesPath ) ;
494+
495+ if ( FunctionLoader . FunctionAppRootPath == null )
496+ {
497+ throw new InvalidOperationException ( PowerShellWorkerStrings . FunctionAppRootNotResolved ) ;
498+ }
499+
500+ _firstPwshInstance . AddCommand ( "Microsoft.PowerShell.Management\\ Set-Content" )
501+ . AddParameter ( "Path" , "env:PSModulePath" )
502+ . AddParameter ( "Value" , FunctionLoader . FunctionModulePath )
503+ . InvokeAndClearCommands ( ) ;
500504 }
501505
502506 #endregion
0 commit comments