diff --git a/src/FunctionLoader.cs b/src/FunctionLoader.cs
index 1611ccdd..3934e340 100644
--- a/src/FunctionLoader.cs
+++ b/src/FunctionLoader.cs
@@ -72,13 +72,20 @@ internal static void ClearLoadedFunctions()
/// Setup the well known paths about the FunctionApp.
/// This method is called only once during the code start.
///
- internal static void SetupWellKnownPaths(string functionAppRootPath, string managedDependenciesPath)
+ internal static void SetupWellKnownPaths(string functionAppRootPath, string managedDependenciesPath, bool functionAppRootIsUnknown)
{
+ var workerLevelModulesPath = Path.Join(AppDomain.CurrentDomain.BaseDirectory, "Modules");
+
+ if (functionAppRootIsUnknown)
+ {
+ FunctionModulePath = workerLevelModulesPath;
+ return;
+ }
+
FunctionAppRootPath = functionAppRootPath;
// Resolve module paths
var appLevelModulesPath = Path.Join(FunctionAppRootPath, "Modules");
- var workerLevelModulesPath = Path.Join(AppDomain.CurrentDomain.BaseDirectory, "Modules");
FunctionModulePath = $"{appLevelModulesPath}{Path.PathSeparator}{workerLevelModulesPath}";
// Add the managed dependencies folder path
diff --git a/src/Modules/AzureFunctions.PowerShell.SDK/0.0.3/AzureFunctions.AttributeDefinitions.ps1 b/src/Modules/AzureFunctions.PowerShell.SDK/0.0.3/AzureFunctions.AttributeDefinitions.ps1
new file mode 100644
index 00000000..0385b9e2
--- /dev/null
+++ b/src/Modules/AzureFunctions.PowerShell.SDK/0.0.3/AzureFunctions.AttributeDefinitions.ps1
@@ -0,0 +1,65 @@
+#
+# Copyright (c) Microsoft. All rights reserved.
+# Licensed under the MIT license. See LICENSE file in the project root for full license information.
+#
+
+class Function : Attribute {
+ [string]$Name
+}
+
+class HttpTrigger : Attribute {
+ [string]$AuthLevel
+ [string[]]$Methods
+ [string]$Route
+}
+
+class HttpOutput : Attribute {
+ [string]$Name
+}
+
+class TimerTrigger : Attribute {
+ [string]$Chron
+}
+
+class EventGridTrigger : Attribute {
+ EventGridTrigger() { }
+}
+
+class DurableClient : Attribute {
+ [string]$Name
+}
+
+class OrchestrationTrigger : Attribute {
+}
+
+class ActivityTrigger : Attribute {
+}
+
+class EventHubTrigger : Attribute {
+ [string]$EventHubName
+ [string]$ConsumerGroup
+ [string]$Cardinality
+ [string]$Connection
+}
+
+class EventHubOutput : Attribute {
+ [string]$Name
+ [string]$EventHubName
+ [string]$Connection
+}
+
+class InputBinding : Attribute {
+ [string]$Type
+ [string]$Name
+}
+
+class OutputBinding : Attribute {
+ [string]$Type
+ [string]$Name
+}
+
+class AdditionalInformation : Attribute {
+ [string]$BindingName
+ [string]$Name
+ $Value
+}
\ No newline at end of file
diff --git a/src/Modules/AzureFunctions.PowerShell.SDK/0.0.3/AzureFunctions.PowerShell.SDK.psd1 b/src/Modules/AzureFunctions.PowerShell.SDK/0.0.3/AzureFunctions.PowerShell.SDK.psd1
new file mode 100644
index 00000000..a5271816
Binary files /dev/null and b/src/Modules/AzureFunctions.PowerShell.SDK/0.0.3/AzureFunctions.PowerShell.SDK.psd1 differ
diff --git a/src/Modules/AzureFunctions.PowerShell.SDK/0.0.3/PSGetModuleInfo.xml b/src/Modules/AzureFunctions.PowerShell.SDK/0.0.3/PSGetModuleInfo.xml
new file mode 100644
index 00000000..8e5b640e
--- /dev/null
+++ b/src/Modules/AzureFunctions.PowerShell.SDK/0.0.3/PSGetModuleInfo.xml
@@ -0,0 +1,121 @@
+
+
+
+ Microsoft.PowerShell.Commands.PSRepositoryItemInfo
+ System.Management.Automation.PSCustomObject
+ System.Object
+
+
+ AzureFunctions.PowerShell.SDK
+ 0.0.3
+ Module
+ This module contains utilities for writing and parsing Azure Functions written in PowerShell and it is to be used within the PowerShell language worker
+ Microsoft Corporation
+ Francisco-Gamino
+ (c) 2022 Microsoft. All rights reserved.
+ 2023-06-05T03:55:02-06:00
+
+
+
+
+
+
+
+ System.Object[]
+ System.Array
+ System.Object
+
+
+ PSModule
+ PSEdition_Core
+
+
+
+
+ System.Collections.Hashtable
+ System.Object
+
+
+
+ Cmdlet
+
+
+
+ Get-FunctionsMetadata
+
+
+
+
+ Workflow
+
+
+
+
+
+
+ Function
+
+
+
+ Command
+
+
+
+ Get-FunctionsMetadata
+
+
+
+
+ DscResource
+
+
+
+ RoleCapability
+
+
+
+
+
+ # 0.0.3_x000D__x000A_ Initial Release.
+
+
+
+
+ https://www.powershellgallery.com/api/v2
+ PSGallery
+ NuGet
+
+
+ System.Management.Automation.PSCustomObject
+ System.Object
+
+
+ (c) 2022 Microsoft. All rights reserved.
+ This module contains utilities for writing and parsing Azure Functions written in PowerShell and it is to be used within the PowerShell language worker
+ False
+ # 0.0.3_x000D__x000A_ Initial Release.
+ True
+ True
+ 215
+ 261
+ 19534
+ 6/5/2023 3:55:02 AM -06:00
+ 6/5/2023 3:55:02 AM -06:00
+ 2/15/2024 7:12:43 AM -07:00
+ PSModule PSEdition_Core PSCmdlet_Get-FunctionsMetadata PSCommand_Get-FunctionsMetadata PSIncludes_Cmdlet
+ False
+ 2024-02-15T07:12:43Z
+ 0.0.3
+ Microsoft Corporation
+ false
+ Module
+ AzureFunctions.PowerShell.SDK.nuspec|AzureFunctions.AttributeDefinitions.ps1|AzureFunctions.PowerShell.SDK.dll|AzureFunctions.PowerShell.SDK.psd1
+ 1d075857-4919-4ede-bb43-bb193483a280
+ 7.2
+ Microsoft Corporation
+
+
+ D:\repos\azure-functions-powershell-worker\src\Modules\AzureFunctions.PowerShell.SDK\0.0.3
+
+
+
diff --git a/src/PowerShell/PowerShellManager.cs b/src/PowerShell/PowerShellManager.cs
index 5a0a4db7..499032b9 100644
--- a/src/PowerShell/PowerShellManager.cs
+++ b/src/PowerShell/PowerShellManager.cs
@@ -224,7 +224,7 @@ public Hashtable InvokeFunction(
try
{
- if(functionInfo.DurableFunctionInfo.IsOrchestrationFunction)
+ if (functionInfo.DurableFunctionInfo.IsOrchestrationFunction)
{
return durableFunctionsUtils.InvokeOrchestrationFunction();
}
diff --git a/src/RequestProcessor.cs b/src/RequestProcessor.cs
index ed29a40a..03cb0bb4 100644
--- a/src/RequestProcessor.cs
+++ b/src/RequestProcessor.cs
@@ -401,7 +401,7 @@ private StreamingMessage ProcessFunctionMetadataRequest(StreamingMessage request
rpcLogger.SetContext(request.RequestId, null);
_functionAppRootPath = request.FunctionsMetadataRequest.FunctionAppDirectory;
- response.FunctionMetadataResponse.FunctionMetadataResults.AddRange(WorkerIndexingHelper.IndexFunctions(_functionAppRootPath, rpcLogger));
+ response.FunctionMetadataResponse.FunctionMetadataResults.AddRange(WorkerIndexingHelper.IndexFunctions(_functionAppRootPath, rpcLogger, _firstPwshInstance));
return response;
}
@@ -574,7 +574,7 @@ private static void BindOutputFromResult(InvocationResponse response, AzFunction
private void SetupAppRootPathAndModulePath(string functionAppRootPath, string managedDependenciesPath)
{
- FunctionLoader.SetupWellKnownPaths(functionAppRootPath, managedDependenciesPath);
+ FunctionLoader.SetupWellKnownPaths(functionAppRootPath, managedDependenciesPath, false);
if (FunctionLoader.FunctionAppRootPath == null)
{
diff --git a/src/WorkerIndexing/WorkerIndexingHelper.cs b/src/WorkerIndexing/WorkerIndexingHelper.cs
index 87f2bc21..11abb8ce 100644
--- a/src/WorkerIndexing/WorkerIndexingHelper.cs
+++ b/src/WorkerIndexing/WorkerIndexingHelper.cs
@@ -26,7 +26,7 @@ internal class WorkerIndexingHelper
const string AzureFunctionsPowerShellSDKModuleName = "AzureFunctions.PowerShell.SDK";
private static readonly ErrorRecordFormatter _errorRecordFormatter = new ErrorRecordFormatter();
- internal static IEnumerable IndexFunctions(string functionAppRootPath, ILogger logger)
+ internal static IEnumerable IndexFunctions(string functionAppRootPath, ILogger logger, System.Management.Automation.PowerShell _firstPwshInstance)
{
if (string.IsNullOrWhiteSpace(functionAppRootPath))
{
@@ -35,6 +35,14 @@ internal static IEnumerable IndexFunctions(string functionA
List indexedFunctions = new List();
+ // A hacky way to add the worker modules into the env only
+ FunctionLoader.SetupWellKnownPaths(null, null, true);
+
+ _firstPwshInstance.AddCommand("Microsoft.PowerShell.Management\\Set-Content")
+ .AddParameter("Path", "env:PSModulePath")
+ .AddParameter("Value", FunctionLoader.FunctionModulePath)
+ .InvokeAndClearCommands();
+
// This is not the correct way to deal with getting a runspace for the cmdlet.
// Firstly, creating a runspace is expensive. If we are going to generate a runspace, it should be done on
@@ -55,11 +63,11 @@ internal static IEnumerable IndexFunctions(string functionA
// 3. Continue using a new runspace for invoking Get-FunctionsMetadata, but initialize it in worker init and
// point the PsModulePath to the module path bundled with the worker.
- InitialSessionState initial = InitialSessionState.CreateDefault();
- Runspace runspace = RunspaceFactory.CreateRunspace(initial);
- runspace.Open();
- System.Management.Automation.PowerShell _powershell = System.Management.Automation.PowerShell.Create();
- _powershell.Runspace = runspace;
+ //InitialSessionState initial = InitialSessionState.CreateDefault();
+ //Runspace runspace = RunspaceFactory.CreateRunspace(initial);
+ //runspace.Open();
+ //System.Management.Automation.PowerShell _powershell = System.Management.Automation.PowerShell.Create();
+ //_powershell.Runspace = runspace;
string outputString = string.Empty;
Exception exception = null;
@@ -70,9 +78,9 @@ internal static IEnumerable IndexFunctions(string functionA
try
{
- _powershell.AddCommand(GetFunctionsMetadataCmdletName).AddArgument(functionAppRootPath);
- results = _powershell.Invoke();
- cmdletExecutionHadErrors = _powershell.HadErrors;
+ _firstPwshInstance.AddCommand(GetFunctionsMetadataCmdletName).AddArgument(functionAppRootPath);
+ results = _firstPwshInstance.Invoke();
+ cmdletExecutionHadErrors = _firstPwshInstance.HadErrors;
}
catch (Exception ex)
{
@@ -88,7 +96,7 @@ internal static IEnumerable IndexFunctions(string functionA
}
else if (cmdletExecutionHadErrors)
{
- var errorCollection = _powershell.Streams.Error;
+ var errorCollection = _firstPwshInstance.Streams.Error;
var stringBuilder = new StringBuilder();
foreach (var errorRecord in errorCollection)
@@ -106,7 +114,7 @@ internal static IEnumerable IndexFunctions(string functionA
throw new Exception(errorMsg);
}
- _powershell.Commands.Clear();
+ _firstPwshInstance.Commands.Clear();
}
// TODO: The GetFunctionsMetadataCmdlet should never return more than one result. Make sure that this is the case and remove this code.
diff --git a/test/Unit/Modules/HelperModuleTests.cs b/test/Unit/Modules/HelperModuleTests.cs
index 315ca200..5dcab06e 100644
--- a/test/Unit/Modules/HelperModuleTests.cs
+++ b/test/Unit/Modules/HelperModuleTests.cs
@@ -49,7 +49,7 @@ static HelperModuleTests()
};
var funcLoadReq = new FunctionLoadRequest { FunctionId = "FunctionId", Metadata = rpcFuncMetadata };
- FunctionLoader.SetupWellKnownPaths(funcLoadReq.Metadata.Directory, managedDependenciesPath: null);
+ FunctionLoader.SetupWellKnownPaths(funcLoadReq.Metadata.Directory, managedDependenciesPath: null, false);
s_pwsh = Utils.NewPwshInstance();
s_funcInfo = new AzFunctionInfo(rpcFuncMetadata);
}
diff --git a/test/Unit/PowerShell/PowerShellManagerTests.cs b/test/Unit/PowerShell/PowerShellManagerTests.cs
index 726f5e4a..2a570321 100644
--- a/test/Unit/PowerShell/PowerShellManagerTests.cs
+++ b/test/Unit/PowerShell/PowerShellManagerTests.cs
@@ -80,7 +80,7 @@ static PowerShellManagerTests()
};
s_functionLoadRequest = new FunctionLoadRequest { FunctionId = "FunctionId", Metadata = rpcFunctionMetadata };
- FunctionLoader.SetupWellKnownPaths(s_functionLoadRequest.Metadata.Directory, managedDependenciesPath: null);
+ FunctionLoader.SetupWellKnownPaths(s_functionLoadRequest.Metadata.Directory, managedDependenciesPath: null, false);
}
// Have a single place to get a PowerShellManager for testing.