55
66using Microsoft . PowerShell . EditorServices . Utility ;
77using System ;
8- using System . IO ;
98using System . Linq ;
109using System . Management . Automation . Runspaces ;
1110using System . Threading ;
@@ -44,6 +43,21 @@ public class AnalysisService : IDisposable
4443
4544 #endregion // Private Fields
4645
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+
4761 #region Constructors
4862
4963 /// <summary>
@@ -56,6 +70,7 @@ public AnalysisService(IConsoleHost consoleHost, string settingsPath = null)
5670 {
5771 try
5872 {
73+ this . SettingsPath = settingsPath ;
5974 this . analysisRunspace = RunspaceFactory . CreateRunspace ( InitialSessionState . CreateDefault2 ( ) ) ;
6075 this . analysisRunspace . ThreadOptions = PSThreadOptions . ReuseThread ;
6176 this . analysisRunspace . Open ( ) ;
@@ -219,17 +234,28 @@ private IEnumerable<PSObject> GetDiagnosticRecords(ScriptFile file)
219234
220235 if ( this . scriptAnalyzerModuleInfo != null )
221236 {
222- using ( var ps = System . Management . Automation . PowerShell . Create ( ) )
237+ using ( var powerShell = System . Management . Automation . PowerShell . Create ( ) )
223238 {
224- ps . Runspace = this . analysisRunspace ;
239+ powerShell . Runspace = this . analysisRunspace ;
225240 Logger . Write (
226241 LogLevel . Verbose ,
227242 String . Format ( "Running PSScriptAnalyzer against {0}" , file . FilePath ) ) ;
228243
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 ( ) ;
233259 }
234260 }
235261
0 commit comments