5
5
6
6
using Microsoft . PowerShell . EditorServices . Utility ;
7
7
using System ;
8
- using System . IO ;
9
8
using System . Linq ;
10
9
using System . Management . Automation . Runspaces ;
11
10
using System . Threading ;
@@ -44,6 +43,21 @@ public class AnalysisService : IDisposable
44
43
45
44
#endregion // Private Fields
46
45
46
+
47
+ #region Properties
48
+
49
+ /// <summary>
50
+ /// Gets or sets the path to a settings file (.psd1)
51
+ /// containing PSScriptAnalyzer settings.
52
+ /// </summary>
53
+ public string SettingsPath
54
+ {
55
+ get ;
56
+ set ;
57
+ }
58
+
59
+ #endregion
60
+
47
61
#region Constructors
48
62
49
63
/// <summary>
@@ -56,6 +70,7 @@ public AnalysisService(IConsoleHost consoleHost, string settingsPath = null)
56
70
{
57
71
try
58
72
{
73
+ this . SettingsPath = settingsPath ;
59
74
this . analysisRunspace = RunspaceFactory . CreateRunspace ( InitialSessionState . CreateDefault2 ( ) ) ;
60
75
this . analysisRunspace . ThreadOptions = PSThreadOptions . ReuseThread ;
61
76
this . analysisRunspace . Open ( ) ;
@@ -219,17 +234,28 @@ private IEnumerable<PSObject> GetDiagnosticRecords(ScriptFile file)
219
234
220
235
if ( this . scriptAnalyzerModuleInfo != null )
221
236
{
222
- using ( var ps = System . Management . Automation . PowerShell . Create ( ) )
237
+ using ( var powerShell = System . Management . Automation . PowerShell . Create ( ) )
223
238
{
224
- ps . Runspace = this . analysisRunspace ;
239
+ powerShell . Runspace = this . analysisRunspace ;
225
240
Logger . Write (
226
241
LogLevel . Verbose ,
227
242
String . Format ( "Running PSScriptAnalyzer against {0}" , file . FilePath ) ) ;
228
243
229
- diagnosticRecords = ps . AddCommand ( "Invoke-ScriptAnalyzer" )
230
- . AddParameter ( "ScriptDefinition" , file . Contents )
231
- . AddParameter ( "IncludeRule" , IncludedRules )
232
- . Invoke ( ) ;
244
+ powerShell
245
+ . AddCommand ( "Invoke-ScriptAnalyzer" )
246
+ . AddParameter ( "ScriptDefinition" , file . Contents ) ;
247
+
248
+ // Use a settings file if one is provided, otherwise use the default rule list.
249
+ if ( ! string . IsNullOrWhiteSpace ( this . SettingsPath ) )
250
+ {
251
+ powerShell . AddParameter ( "Settings" , this . SettingsPath ) ;
252
+ }
253
+ else
254
+ {
255
+ powerShell . AddParameter ( "IncludeRule" , IncludedRules ) ;
256
+ }
257
+
258
+ diagnosticRecords = powerShell . Invoke ( ) ;
233
259
}
234
260
}
235
261
0 commit comments