7
7
using System . Collections ;
8
8
using System . Collections . Generic ;
9
9
using System . Collections . ObjectModel ;
10
+ using System . IO ;
10
11
11
12
using Microsoft . Azure . Functions . PowerShellWorker . Utility ;
12
13
using Microsoft . Azure . WebJobs . Script . Grpc . Messages ;
@@ -20,14 +21,15 @@ namespace Microsoft.Azure.Functions.PowerShellWorker.PowerShell
20
21
internal class PowerShellManager
21
22
{
22
23
readonly static string s_TriggerMetadataParameterName = "TriggerMetadata" ;
23
- readonly static bool s_UseLocalScope = true ;
24
24
25
25
RpcLogger _logger ;
26
26
PowerShell _pwsh ;
27
27
28
- PowerShellManager ( PowerShell pwsh , RpcLogger logger )
28
+ internal PowerShellManager ( RpcLogger logger )
29
29
{
30
- _pwsh = pwsh ;
30
+ var initialSessionState = InitialSessionState . CreateDefault ( ) ;
31
+ initialSessionState . ExecutionPolicy = Microsoft . PowerShell . ExecutionPolicy . Unrestricted ;
32
+ _pwsh = PowerShell . Create ( initialSessionState ) ;
31
33
_logger = logger ;
32
34
33
35
// Setup Stream event listeners
@@ -40,36 +42,19 @@ internal class PowerShellManager
40
42
_pwsh . Streams . Warning . DataAdding += streamHandler . WarningDataAdding ;
41
43
}
42
44
43
- public static PowerShellManager Create ( RpcLogger logger )
45
+ internal void InitializeRunspace ( )
44
46
{
45
- // Set up initial session state
46
- var initialSessionState = InitialSessionState . CreateDefault ( ) ;
47
- if ( Platform . IsWindows )
48
- {
49
- initialSessionState . ExecutionPolicy = Microsoft . PowerShell . ExecutionPolicy . Unrestricted ;
50
- }
51
- var pwsh = PowerShell . Create ( initialSessionState ) ;
52
-
53
- // Build path to the Azure Functions binding helper module
54
- string modulePath = System . IO . Path . Join (
55
- AppDomain . CurrentDomain . BaseDirectory ,
56
- "Azure.Functions.PowerShell.Worker.Module" ,
57
- "Azure.Functions.PowerShell.Worker.Module.psd1" ) ;
58
-
59
47
// Add HttpResponseContext namespace so users can reference
60
48
// HttpResponseContext without needing to specify the full namespace
61
- pwsh . AddScript ( $ "using namespace { typeof ( HttpResponseContext ) . Namespace } ")
62
- . AddStatement ( )
63
- // Import the Azure Functions binding helper module
64
- . AddCommand ( "Import-Module" )
65
- . AddParameter ( "Name" , modulePath )
66
- . AddParameter ( "Scope" , "Global" )
67
- . InvokeAndClearCommands ( ) ;
68
-
69
- return new PowerShellManager ( pwsh , logger ) ;
49
+ _pwsh . AddScript ( $ "using namespace { typeof ( HttpResponseContext ) . Namespace } ") . InvokeAndClearCommands ( ) ;
50
+
51
+ // Prepend the path to the internal Modules folder to the PSModulePath
52
+ var modulePath = Environment . GetEnvironmentVariable ( "PSModulePath" ) ;
53
+ var additionalPath = Path . Join ( AppDomain . CurrentDomain . BaseDirectory , "Modules" ) ;
54
+ Environment . SetEnvironmentVariable ( "PSModulePath" , $ "{ additionalPath } { Path . PathSeparator } { modulePath } ") ;
70
55
}
71
56
72
- public Hashtable InvokeFunction (
57
+ internal Hashtable InvokeFunction (
73
58
string scriptPath ,
74
59
string entryPoint ,
75
60
Hashtable triggerMetadata ,
@@ -88,22 +73,19 @@ public Hashtable InvokeFunction(
88
73
if ( entryPoint != "" )
89
74
{
90
75
parameterMetadata = _pwsh
91
- . AddScript ( $@ ". { scriptPath } ", s_UseLocalScope )
76
+ . AddScript ( $@ ". { scriptPath } ")
92
77
. AddStatement ( )
93
- . AddCommand ( "Get-Command" , s_UseLocalScope ) . AddParameter ( "Name" , entryPoint )
78
+ . AddCommand ( "Get-Command" , useLocalScope : true ) . AddParameter ( "Name" , entryPoint )
94
79
. InvokeAndClearCommands < FunctionInfo > ( ) [ 0 ] . Parameters ;
95
80
96
- _pwsh
97
- . AddScript ( $@ ". { scriptPath } ", s_UseLocalScope )
98
- . AddStatement ( )
99
- . AddCommand ( entryPoint , s_UseLocalScope ) ;
81
+ _pwsh . AddCommand ( entryPoint , useLocalScope : true ) ;
100
82
101
83
}
102
84
else
103
85
{
104
- parameterMetadata = _pwsh . AddCommand ( "Get-Command" , s_UseLocalScope ) . AddParameter ( "Name" , scriptPath )
86
+ parameterMetadata = _pwsh . AddCommand ( "Get-Command" , useLocalScope : true ) . AddParameter ( "Name" , scriptPath )
105
87
. InvokeAndClearCommands < ExternalScriptInfo > ( ) [ 0 ] . Parameters ;
106
- _pwsh . AddCommand ( scriptPath , s_UseLocalScope ) ;
88
+ _pwsh . AddCommand ( scriptPath , useLocalScope : true ) ;
107
89
}
108
90
}
109
91
@@ -127,40 +109,32 @@ public Hashtable InvokeFunction(
127
109
Collection < PSObject > pipelineItems = _pwsh . InvokeAndClearCommands < PSObject > ( ) ;
128
110
foreach ( var psobject in pipelineItems )
129
111
{
130
- _logger . LogInformation ( psobject . ToString ( ) ) ;
112
+ _logger . LogInformation ( $ "FROM FUNCTION: { psobject . ToString ( ) } " ) ;
131
113
}
132
114
133
115
returnObject = pipelineItems [ pipelineItems . Count - 1 ] ;
134
116
}
135
117
136
- var result = _pwsh . AddCommand ( "Get-OutputBinding" , s_UseLocalScope ) . InvokeAndClearCommands < Hashtable > ( ) [ 0 ] ;
118
+ var result = _pwsh . AddCommand ( "Azure.Functions.PowerShell.Worker.Module\\ Get-OutputBinding" , useLocalScope : true )
119
+ . AddParameter ( "Purge" )
120
+ . InvokeAndClearCommands < Hashtable > ( ) [ 0 ] ;
137
121
138
122
if ( returnObject != null )
139
123
{
140
124
result . Add ( "$return" , returnObject ) ;
141
125
}
142
- ResetRunspace ( ) ;
143
126
return result ;
144
127
}
145
- catch ( Exception e )
128
+ finally
146
129
{
147
130
ResetRunspace ( ) ;
148
- throw ;
149
131
}
150
132
}
151
133
152
- void ResetRunspace ( )
134
+ private void ResetRunspace ( )
153
135
{
154
136
// Reset the runspace to the Initial Session State
155
137
_pwsh . Runspace . ResetRunspaceState ( ) ;
156
-
157
- // TODO: Change this to clearing the variable by running in the module
158
- string modulePath = System . IO . Path . Join ( AppDomain . CurrentDomain . BaseDirectory , "Azure.Functions.PowerShell.Worker.Module" , "Azure.Functions.PowerShell.Worker.Module.psd1" ) ;
159
- _pwsh . AddCommand ( "Import-Module" )
160
- . AddParameter ( "Name" , modulePath )
161
- . AddParameter ( "Scope" , "Global" )
162
- . AddParameter ( "Force" )
163
- . InvokeAndClearCommands ( ) ;
164
138
}
165
139
}
166
140
}
0 commit comments