@@ -711,9 +711,23 @@ private List<ExternalRule> GetExternalRule(string[] moduleNames)
711711 using ( System . Management . Automation . PowerShell posh =
712712 System . Management . Automation . PowerShell . Create ( state ) )
713713 {
714- posh . AddCommand ( "Get-Module" ) . AddParameter ( "Name" , moduleName ) . AddParameter ( "ListAvailable" ) ;
715- shortModuleName = posh . Invoke < PSModuleInfo > ( ) . First ( ) . Name ;
716-
714+ posh . AddCommand ( "Get-Module" ) ;
715+ Collection < PSModuleInfo > loadedModules = posh . Invoke < PSModuleInfo > ( ) ;
716+ foreach ( PSModuleInfo module in loadedModules )
717+ {
718+ string pathToCompare = moduleName ;
719+ if ( ! File . GetAttributes ( moduleName ) . HasFlag ( FileAttributes . Directory ) )
720+ {
721+ pathToCompare = Path . GetDirectoryName ( moduleName ) ;
722+ }
723+
724+ if ( pathToCompare == Path . GetDirectoryName ( module . Path ) )
725+ {
726+ shortModuleName = module . Name ;
727+ break ;
728+ }
729+ }
730+
717731 // Invokes Get-Command and Get-Help for each functions in the module.
718732 posh . Commands . Clear ( ) ;
719733 posh . AddCommand ( "Get-Command" ) . AddParameter ( "Module" , shortModuleName ) ;
@@ -974,17 +988,33 @@ public Dictionary<string, List<string>> CheckRuleExtension(string[] path, PathIn
974988 resolvedPath = basePath
975989 . GetResolvedPSPathFromPSPath ( childPath ) . First ( ) . ToString ( ) ;
976990 }
991+
992+ // Import the module
993+ InitialSessionState state = InitialSessionState . CreateDefault2 ( ) ;
994+ state . ImportPSModule ( new string [ ] { resolvedPath } ) ;
977995
978996 using ( System . Management . Automation . PowerShell posh =
979- System . Management . Automation . PowerShell . Create ( ) )
980- {
981- posh . AddCommand ( "Get-Module" ) . AddParameter ( "Name" , resolvedPath ) . AddParameter ( "ListAvailable" ) ;
982- PSModuleInfo moduleInfo = posh . Invoke < PSModuleInfo > ( ) . First ( ) ;
983-
984- // Adds original path, otherwise path.Except<string>(validModPaths) will fail.
985- // It's possible that user can provide something like this:
986- // "..\..\..\ScriptAnalyzer.UnitTest\modules\CommunityAnalyzerRules\CommunityAnalyzerRules.psd1"
987- if ( moduleInfo . ExportedFunctions . Count > 0 ) validModPaths . Add ( resolvedPath ) ;
997+ System . Management . Automation . PowerShell . Create ( state ) )
998+ {
999+ posh . AddCommand ( "Get-Module" ) ;
1000+ Collection < PSModuleInfo > loadedModules = posh . Invoke < PSModuleInfo > ( ) ;
1001+ foreach ( PSModuleInfo module in loadedModules )
1002+ {
1003+ string pathToCompare = resolvedPath ;
1004+ if ( ! File . GetAttributes ( resolvedPath ) . HasFlag ( FileAttributes . Directory ) )
1005+ {
1006+ pathToCompare = Path . GetDirectoryName ( resolvedPath ) ;
1007+ }
1008+
1009+ if ( pathToCompare == Path . GetDirectoryName ( module . Path ) )
1010+ {
1011+ if ( module . ExportedFunctions . Count > 0 )
1012+ {
1013+ validModPaths . Add ( resolvedPath ) ;
1014+ }
1015+ break ;
1016+ }
1017+ }
9881018 }
9891019 }
9901020 catch
0 commit comments