diff --git a/src/Azure.Functions.PowerShell.Worker.Messaging/FunctionMessagingClient.cs b/src/Azure.Functions.PowerShell.Worker.Messaging/FunctionMessagingClient.cs
index e1257581..7f91dc41 100644
--- a/src/Azure.Functions.PowerShell.Worker.Messaging/FunctionMessagingClient.cs
+++ b/src/Azure.Functions.PowerShell.Worker.Messaging/FunctionMessagingClient.cs
@@ -10,7 +10,7 @@
using Grpc.Core;
using Microsoft.Azure.WebJobs.Script.Grpc.Messages;
-namespace Azure.Functions.PowerShell.Worker.Messaging
+namespace Microsoft.Azure.Functions.PowerShellWorker.Messaging
{
public class FunctionMessagingClient : IDisposable
{
diff --git a/src/Azure.Functions.PowerShell.Worker/Azure.Functions.PowerShell.Worker.csproj b/src/Azure.Functions.PowerShell.Worker/Azure.Functions.PowerShell.Worker.csproj
index bfe63885..d27e5a4c 100644
--- a/src/Azure.Functions.PowerShell.Worker/Azure.Functions.PowerShell.Worker.csproj
+++ b/src/Azure.Functions.PowerShell.Worker/Azure.Functions.PowerShell.Worker.csproj
@@ -11,8 +11,9 @@
-
+
+
diff --git a/src/Azure.Functions.PowerShell.Worker/StartupArguments.cs b/src/Azure.Functions.PowerShell.Worker/StartupArguments.cs
deleted file mode 100644
index ee865564..00000000
--- a/src/Azure.Functions.PowerShell.Worker/StartupArguments.cs
+++ /dev/null
@@ -1,44 +0,0 @@
-//
-// Copyright (c) Microsoft. All rights reserved.
-// Licensed under the MIT license. See LICENSE file in the project root for full license information.
-//
-
-using System;
-
-namespace Microsoft.Azure.Functions.PowerShellWorker
-{
- public class StartupArguments
- {
- public int GrpcMaxMessageLength { get; set; }
- public string Host {get; set;}
- public int Port {get; set;}
- public string RequestId {get; set;}
- public string WorkerId {get; set;}
-
- public static StartupArguments Parse(string[] args)
- {
- if (args.Length != 10)
- {
- Console.WriteLine("usage --host --port --workerId --requestId --grpcMaxMessageLength ");
- throw new InvalidOperationException("Incorrect startup arguments were given.");
- }
-
- StartupArguments arguments = new StartupArguments();
- for (int i = 1; i < 10; i+=2)
- {
- string currentArg = args[i];
- switch (i)
- {
- case 1: arguments.Host = currentArg; break;
- case 3: arguments.Port = int.Parse(currentArg); break;
- case 5: arguments.WorkerId = currentArg; break;
- case 7: arguments.RequestId = currentArg; break;
- case 9: arguments.GrpcMaxMessageLength = int.Parse(currentArg); break;
- default: throw new InvalidOperationException();
- }
- }
-
- return arguments;
- }
- }
-}
\ No newline at end of file
diff --git a/src/Azure.Functions.PowerShell.Worker/Utility/RpcLogger.cs b/src/Azure.Functions.PowerShell.Worker/Utility/RpcLogger.cs
index 5243a8cc..aeaef298 100644
--- a/src/Azure.Functions.PowerShell.Worker/Utility/RpcLogger.cs
+++ b/src/Azure.Functions.PowerShell.Worker/Utility/RpcLogger.cs
@@ -5,7 +5,7 @@
using System;
-using Azure.Functions.PowerShell.Worker.Messaging;
+using Microsoft.Azure.Functions.PowerShellWorker.Messaging;
using Microsoft.Azure.WebJobs.Script.Grpc.Messages;
using Microsoft.Extensions.Logging;
diff --git a/src/Azure.Functions.PowerShell.Worker/Worker.cs b/src/Azure.Functions.PowerShell.Worker/Worker.cs
index 395cf27d..ea8e4429 100644
--- a/src/Azure.Functions.PowerShell.Worker/Worker.cs
+++ b/src/Azure.Functions.PowerShell.Worker/Worker.cs
@@ -5,9 +5,11 @@
using System;
using System.Threading.Tasks;
+using System.Management.Automation;
using System.Management.Automation.Runspaces;
-using Azure.Functions.PowerShell.Worker.Messaging;
+using CommandLine;
+using Microsoft.Azure.Functions.PowerShellWorker.Messaging;
using Microsoft.Azure.Functions.PowerShellWorker.PowerShell.Host;
using Microsoft.Azure.Functions.PowerShellWorker.Requests;
using Microsoft.Azure.Functions.PowerShellWorker.Utility;
@@ -29,12 +31,17 @@ static void InitPowerShell()
s_runspace = RunspaceFactory.CreateRunspace(host);
s_runspace.Open();
- s_ps = System.Management.Automation.PowerShell.Create(InitialSessionState.CreateDefault());
+ s_ps = System.Management.Automation.PowerShell.Create();
s_ps.Runspace = s_runspace;
- s_ps.AddScript("$PSHOME");
- //s_ps.AddCommand("Set-ExecutionPolicy").AddParameter("ExecutionPolicy", ExecutionPolicy.Unrestricted).AddParameter("Scope", ExecutionPolicyScope.Process);
- s_ps.Invoke();
+ if (Platform.IsWindows)
+ {
+ s_ps.AddCommand("Set-ExecutionPolicy")
+ .AddParameter("ExecutionPolicy", "Unrestricted")
+ .AddParameter("Scope", "Process")
+ .Invoke();
+ s_ps.Commands.Clear();
+ }
// Add HttpResponseContext namespace so users can reference
// HttpResponseContext without needing to specify the full namespace
@@ -44,21 +51,23 @@ static void InitPowerShell()
public async static Task Main(string[] args)
{
- StartupArguments startupArguments = StartupArguments.Parse(args);
+ WorkerArguments arguments = null;
+ Parser.Default.ParseArguments(args)
+ .WithParsed(ops => arguments = ops)
+ .WithNotParsed(err => Environment.Exit(1));
// Initialize Rpc client, logger, and PowerShell
- s_client = new FunctionMessagingClient(startupArguments.Host, startupArguments.Port);
+ s_client = new FunctionMessagingClient(arguments.Host, arguments.Port);
s_logger = new RpcLogger(s_client);
InitPowerShell();
// Send StartStream message
var streamingMessage = new StreamingMessage() {
- RequestId = startupArguments.RequestId,
- StartStream = new StartStream() { WorkerId = startupArguments.WorkerId }
+ RequestId = arguments.RequestId,
+ StartStream = new StartStream() { WorkerId = arguments.WorkerId }
};
await s_client.WriteAsync(streamingMessage);
-
await ProcessEvent();
}
@@ -105,4 +114,22 @@ static async Task ProcessEvent()
}
}
}
-}
\ No newline at end of file
+
+ internal class WorkerArguments
+ {
+ [Option("host", Required = true, HelpText = "IP Address used to connect to the Host via gRPC.")]
+ public string Host { get; set; }
+
+ [Option("port", Required = true, HelpText = "Port used to connect to the Host via gRPC.")]
+ public int Port { get; set; }
+
+ [Option("workerId", Required = true, HelpText = "Worker ID assigned to this language worker.")]
+ public string WorkerId { get; set; }
+
+ [Option("requestId", Required = true, HelpText = "Request ID used for gRPC communication with the Host.")]
+ public string RequestId { get; set; }
+
+ [Option("grpcMaxMessageLength", Required = true, HelpText = "gRPC Maximum message size.")]
+ public int MaxMessageLength { get; set; }
+ }
+}