26
26
using System . Globalization ;
27
27
using System . Collections . Concurrent ;
28
28
using System . Threading . Tasks ;
29
+ using System . Collections . ObjectModel ;
29
30
30
31
namespace Microsoft . Windows . PowerShell . ScriptAnalyzer
31
32
{
@@ -545,18 +546,19 @@ private List<ExternalRule> GetExternalRule(string[] moduleNames)
545
546
using ( System . Management . Automation . PowerShell posh =
546
547
System . Management . Automation . PowerShell . Create ( state ) )
547
548
{
548
- string script = string . Format ( CultureInfo . CurrentCulture , "Get-Module - Name '{0}' -ListAvailable " , moduleName ) ;
549
- shortModuleName = posh . AddScript ( script ) . Invoke < PSModuleInfo > ( ) . First ( ) . Name ;
549
+ posh . AddCommand ( "Get-Module" ) . AddParameter ( " Name", moduleName ) . AddParameter ( "ListAvailable" ) ;
550
+ shortModuleName = posh . Invoke < PSModuleInfo > ( ) . First ( ) . Name ;
550
551
551
552
// Invokes Update-Help for this module
552
553
// Required since when invoking Get-Help later on, the cmdlet prompts for Update-Help interactively
553
554
// By invoking Update-Help first, Get-Help will not prompt for downloading help later
554
- script = string . Format ( CultureInfo . CurrentCulture , "Update-Help - Module '{0}' -Force " , shortModuleName ) ;
555
- posh . AddScript ( script ) . Invoke ( ) ;
556
-
555
+ posh . AddCommand ( "Update-Help" ) . AddParameter ( " Module", shortModuleName ) . AddParameter ( "Force" ) ;
556
+ posh . Invoke ( ) ;
557
+
557
558
// Invokes Get-Command and Get-Help for each functions in the module.
558
- script = string . Format ( CultureInfo . CurrentCulture , "Get-Command -Module '{0}'" , shortModuleName ) ;
559
- var psobjects = posh . AddScript ( script ) . Invoke ( ) ;
559
+ posh . Commands . Clear ( ) ;
560
+ posh . AddCommand ( "Get-Command" ) . AddParameter ( "Module" , shortModuleName ) ;
561
+ var psobjects = posh . Invoke ( ) ;
560
562
561
563
foreach ( PSObject psobject in psobjects )
562
564
{
@@ -570,10 +572,22 @@ private List<ExternalRule> GetExternalRule(string[] moduleNames)
570
572
//Only add functions that are defined as rules.
571
573
if ( param != null )
572
574
{
573
- script = string . Format ( CultureInfo . CurrentCulture , "(Get-Help -Name {0}).Description | Out-String" , funcInfo . Name ) ;
574
- string desc = posh . AddScript ( script ) . Invoke ( ) [ 0 ] . ImmediateBaseObject . ToString ( )
575
- . Replace ( "\r \n " , " " ) . Trim ( ) ;
575
+ posh . AddCommand ( "Get-Help" ) . AddParameter ( "Name" , funcInfo . Name ) ;
576
+ Collection < PSObject > helpContent = posh . Invoke ( ) ;
577
+
578
+ // Retrieve "Description" field in the help content
579
+ string desc = String . Empty ;
580
+
581
+ if ( ( null != helpContent ) && ( 1 == helpContent . Count ) )
582
+ {
583
+ dynamic description = helpContent [ 0 ] . Properties [ "Description" ] ;
576
584
585
+ if ( null != description )
586
+ {
587
+ desc = description . Value [ 0 ] . Text ;
588
+ }
589
+ }
590
+
577
591
rules . Add ( new ExternalRule ( funcInfo . Name , funcInfo . Name , desc , param . Name , param . ParameterType . FullName ,
578
592
funcInfo . ModuleName , funcInfo . Module . Path ) ) ;
579
593
}
@@ -784,8 +798,8 @@ public Dictionary<string, List<string>> CheckRuleExtension(string[] path, PathIn
784
798
using ( System . Management . Automation . PowerShell posh =
785
799
System . Management . Automation . PowerShell . Create ( ) )
786
800
{
787
- string script = string . Format ( CultureInfo . CurrentCulture , "Get-Module - Name '{0}' -ListAvailable " , resolvedPath ) ;
788
- PSModuleInfo moduleInfo = posh . AddScript ( script ) . Invoke < PSModuleInfo > ( ) . First ( ) ;
801
+ posh . AddCommand ( "Get-Module" ) . AddParameter ( " Name", resolvedPath ) . AddParameter ( "ListAvailable" ) ;
802
+ PSModuleInfo moduleInfo = posh . Invoke < PSModuleInfo > ( ) . First ( ) ;
789
803
790
804
// Adds original path, otherwise path.Except<string>(validModPaths) will fail.
791
805
// It's possible that user can provide something like this:
0 commit comments