5
5
6
6
using System ;
7
7
using System . Threading . Tasks ;
8
+ using System . Management . Automation ;
8
9
using System . Management . Automation . Runspaces ;
9
10
10
- using Azure . Functions . PowerShell . Worker . Messaging ;
11
+ using CommandLine ;
12
+ using Microsoft . Azure . Functions . PowerShellWorker . Messaging ;
11
13
using Microsoft . Azure . Functions . PowerShellWorker . PowerShell . Host ;
12
14
using Microsoft . Azure . Functions . PowerShellWorker . Requests ;
13
15
using Microsoft . Azure . Functions . PowerShellWorker . Utility ;
@@ -29,12 +31,17 @@ static void InitPowerShell()
29
31
30
32
s_runspace = RunspaceFactory . CreateRunspace ( host ) ;
31
33
s_runspace . Open ( ) ;
32
- s_ps = System . Management . Automation . PowerShell . Create ( InitialSessionState . CreateDefault ( ) ) ;
34
+ s_ps = System . Management . Automation . PowerShell . Create ( ) ;
33
35
s_ps . Runspace = s_runspace ;
34
36
35
- s_ps . AddScript ( "$PSHOME" ) ;
36
- //s_ps.AddCommand("Set-ExecutionPolicy").AddParameter("ExecutionPolicy", ExecutionPolicy.Unrestricted).AddParameter("Scope", ExecutionPolicyScope.Process);
37
- s_ps . Invoke < string > ( ) ;
37
+ if ( Platform . IsWindows )
38
+ {
39
+ s_ps . AddCommand ( "Set-ExecutionPolicy" )
40
+ . AddParameter ( "ExecutionPolicy" , "Unrestricted" )
41
+ . AddParameter ( "Scope" , "Process" )
42
+ . Invoke ( ) ;
43
+ s_ps . Commands . Clear ( ) ;
44
+ }
38
45
39
46
// Add HttpResponseContext namespace so users can reference
40
47
// HttpResponseContext without needing to specify the full namespace
@@ -44,21 +51,23 @@ static void InitPowerShell()
44
51
45
52
public async static Task Main ( string [ ] args )
46
53
{
47
- StartupArguments startupArguments = StartupArguments . Parse ( args ) ;
54
+ WorkerArguments arguments = null ;
55
+ Parser . Default . ParseArguments < WorkerArguments > ( args )
56
+ . WithParsed ( ops => arguments = ops )
57
+ . WithNotParsed ( err => Environment . Exit ( 1 ) ) ;
48
58
49
59
// Initialize Rpc client, logger, and PowerShell
50
- s_client = new FunctionMessagingClient ( startupArguments . Host , startupArguments . Port ) ;
60
+ s_client = new FunctionMessagingClient ( arguments . Host , arguments . Port ) ;
51
61
s_logger = new RpcLogger ( s_client ) ;
52
62
InitPowerShell ( ) ;
53
63
54
64
// Send StartStream message
55
65
var streamingMessage = new StreamingMessage ( ) {
56
- RequestId = startupArguments . RequestId ,
57
- StartStream = new StartStream ( ) { WorkerId = startupArguments . WorkerId }
66
+ RequestId = arguments . RequestId ,
67
+ StartStream = new StartStream ( ) { WorkerId = arguments . WorkerId }
58
68
} ;
59
69
60
70
await s_client . WriteAsync ( streamingMessage ) ;
61
-
62
71
await ProcessEvent ( ) ;
63
72
}
64
73
@@ -105,4 +114,22 @@ static async Task ProcessEvent()
105
114
}
106
115
}
107
116
}
108
- }
117
+
118
+ internal class WorkerArguments
119
+ {
120
+ [ Option ( "host" , Required = true , HelpText = "IP Address used to connect to the Host via gRPC." ) ]
121
+ public string Host { get ; set ; }
122
+
123
+ [ Option ( "port" , Required = true , HelpText = "Port used to connect to the Host via gRPC." ) ]
124
+ public int Port { get ; set ; }
125
+
126
+ [ Option ( "workerId" , Required = true , HelpText = "Worker ID assigned to this language worker." ) ]
127
+ public string WorkerId { get ; set ; }
128
+
129
+ [ Option ( "requestId" , Required = true , HelpText = "Request ID used for gRPC communication with the Host." ) ]
130
+ public string RequestId { get ; set ; }
131
+
132
+ [ Option ( "grpcMaxMessageLength" , Required = true , HelpText = "gRPC Maximum message size." ) ]
133
+ public int MaxMessageLength { get ; set ; }
134
+ }
135
+ }
0 commit comments