33// Licensed under the MIT license. See LICENSE file in the project root for full license information.
44//
55
6+ using Microsoft . Azure . Functions . PowerShellWorker . Utility ;
67using Microsoft . Azure . WebJobs . Script . Grpc . Messages ;
78using Newtonsoft . Json ;
89using System ;
910using System . Collections . Generic ;
11+ using System . Collections . ObjectModel ;
1012using System . IO ;
1113using System . Management . Automation ;
1214using System . Management . Automation . Runspaces ;
15+ using LogLevel = Microsoft . Azure . WebJobs . Script . Grpc . Messages . RpcLog . Types . Level ;
16+ using System . Text ;
17+ using Microsoft . Azure . Functions . PowerShellWorker . PowerShell ;
1318
1419namespace Microsoft . Azure . Functions . PowerShellWorker . WorkerIndexing
1520{
@@ -19,8 +24,10 @@ internal class WorkerIndexingHelper
1924 //const string GetFunctionsMetadataCmdletName = "AzureFunctions.PowerShell.SDK\\Get-FunctionsMetadata";
2025 const string GetFunctionsMetadataCmdletName = "Get-FunctionsMetadata" ;
2126 const string AzureFunctionsPowerShellSDKModuleName = "AzureFunctions.PowerShell.SDK" ;
27+ private static readonly ErrorRecordFormatter _errorRecordFormatter = new ErrorRecordFormatter ( ) ;
2228
23- internal static IEnumerable < RpcFunctionMetadata > IndexFunctions ( string baseDir )
29+ //internal static IEnumerable<RpcFunctionMetadata> IndexFunctions(string baseDir)
30+ internal static IEnumerable < RpcFunctionMetadata > IndexFunctions ( string baseDir , ILogger logger )
2431 {
2532 List < RpcFunctionMetadata > indexedFunctions = new List < RpcFunctionMetadata > ( ) ;
2633
@@ -50,24 +57,63 @@ internal static IEnumerable<RpcFunctionMetadata> IndexFunctions(string baseDir)
5057 System . Management . Automation . PowerShell _powershell = System . Management . Automation . PowerShell . Create ( ) ;
5158 _powershell . Runspace = runspace ;
5259
53- var modulePath = Path . Join ( AppDomain . CurrentDomain . BaseDirectory , "Modules" , AzureFunctionsPowerShellSDKModuleName ) ;
54- // Console.WriteLine("modulePath; {0}", modulePath);
55- // Console.WriteLine("Importing module...");
56- _powershell . AddCommand ( "Import-Module" ) . AddParameter ( "Name" , modulePath )
57- . AddParameter ( "Force" , true ) ;
58-
59- // Console.WriteLine("Call Get-FunctionsMetadata");
60- _powershell . AddCommand ( GetFunctionsMetadataCmdletName ) . AddArgument ( baseDir ) ;
6160 string outputString = string . Empty ;
62- foreach ( PSObject rawMetadata in _powershell . Invoke ( ) )
61+ Exception exception = null ;
62+ Collection < PSObject > results = null ;
63+ var cmdletExecutionHadErrors = false ;
64+ var exceptionThrown = false ;
65+ string errorMsg = null ;
66+
67+ try
68+ {
69+ _powershell . AddCommand ( GetFunctionsMetadataCmdletName ) . AddArgument ( baseDir ) ;
70+ results = _powershell . Invoke ( ) ;
71+ cmdletExecutionHadErrors = _powershell . HadErrors ;
72+ }
73+ catch ( Exception ex )
74+ {
75+ exceptionThrown = true ;
76+ exception = ex ;
77+ throw ;
78+ }
79+ finally
80+ {
81+ if ( exceptionThrown )
82+ {
83+ errorMsg = string . Format ( PowerShellWorkerStrings . ErrorsWhileExecutingGetFunctionsMetadata , exception . ToString ( ) ) ;
84+ }
85+ else if ( cmdletExecutionHadErrors )
86+ {
87+ var errorCollection = _powershell . Streams . Error ;
88+
89+ var stringBuilder = new StringBuilder ( ) ;
90+ foreach ( var errorRecord in errorCollection )
91+ {
92+ var message = _errorRecordFormatter . Format ( errorRecord ) ;
93+ stringBuilder . AppendLine ( message ) ;
94+ }
95+
96+ errorMsg = string . Format ( PowerShellWorkerStrings . ErrorsWhileExecutingGetFunctionsMetadata , stringBuilder . ToString ( ) ) ;
97+ }
98+
99+ if ( errorMsg != null )
100+ {
101+ logger . Log ( isUserOnlyLog : true , LogLevel . Error , errorMsg , exception ) ;
102+ throw new Exception ( errorMsg ) ;
103+ }
104+
105+ _powershell . Commands . Clear ( ) ;
106+ }
107+
108+ // TODO: The GetFunctionsMetadataCmdlet should never return more than one result. Make sure that this is the case and remove this code.
109+ foreach ( PSObject rawMetadata in results )
63110 {
64111 if ( outputString != string . Empty )
65112 {
66113 throw new Exception ( PowerShellWorkerStrings . GetFunctionsMetadataMultipleResultsError ) ;
67114 }
68115 outputString = rawMetadata . ToString ( ) ;
69116 }
70- _powershell . Commands . Clear ( ) ;
71117
72118 List < FunctionInformation > functionInformations = JsonConvert . DeserializeObject < List < FunctionInformation > > ( outputString ) ;
73119
0 commit comments