Created
January 28, 2016 21:32
Checks a set of modules for conflicting command names.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Checks the exported commands in a set of modules looking for commands | |
that have naming conflicts. | |
.DESCRIPTION | |
When two modules export commands with the same name, there can be | |
unpredictable results when the unqualified name of the module is | |
used in scripts. | |
This command allows you to specify a set of modules to check for | |
commands that have the same name and reports the results. To | |
check against the set of modules currently loaded in the session, | |
you can pipe the output of Get-Module to it. | |
.EXAMPLE | |
Get-Module | Get-ModuleConflict | |
.EXAMPLE | |
Get-ModuleConflict * -Exclude Azure*,MSOnline* | |
.EXAMPLE | |
Get-ModuleConflict Microsoft.*,Einstein | |
.INPUTS | |
System.Management.Automation.PSModuleInfo[] | |
.OUTPUTS | |
System.Management.Automation.CommandInfo[] | |
#> | |
function Get-ModuleConflict { | |
[CmdletBinding(DefaultParameterSetName='ModuleInfo')] | |
[OutputType([System.Management.Automation.CommandInfo[]])] | |
param ( | |
# The name of one or more modules to check for conflicts. | |
# The modules will not be loaded, but the ExportedCommands of the | |
# module's manifest will be used to look for command names. | |
# You may specify multiple modules names and wildcards are allowed. | |
# To check all modules installed on the system, use *. | |
[ValidateNotNullOrEmpty()] | |
[Parameter(ParameterSetName='ModuleName', Mandatory=$true, Position=1)] | |
[String[]]$ModuleName, | |
# Specifies one or more module infos to check for conflicts. | |
# The most common use case here is to pipe input from Get-Module. | |
[ValidateNotNullOrEmpty()] | |
[Parameter(ParameterSetName='ModuleInfo', Mandatory=$true, ValueFromPipeline=$true)] | |
[System.Management.Automation.PSModuleInfo[]]$ModuleInfo, | |
# Optionally can be used to refine the set of modules to check by | |
# name. Wildcards are allowed. | |
[Parameter()] | |
[String[]]$Include, | |
# Optionally can be used to refine the set of modules to check by | |
# name. Wildcards are allowed. | |
[Parameter()] | |
[String[]]$Exclude | |
) | |
begin { | |
$Commands = @() | |
# When specifying modules by name, get them up front by | |
# calling Get-Module and selecting the exported commands. | |
if ($ModuleName.Length) { | |
$Commands += @(Get-Module -Name:$ModuleName -ListAvailable | ForEach-Object { $_.ExportedCommands.Values }) | |
} | |
} | |
process { | |
# When specifying modules by piping to ModuleInfo, collect | |
# the exported commands into the array. | |
if ($ModuleInfo.Length) { | |
$Commands += @($ModuleInfo.ExportedCommands.Values) | |
} | |
} | |
end { | |
$Commands | | |
# Process -Include filter | |
ForEach-Object { | |
if (!$Include.Length) { | |
Write-Output $_ | |
} | |
else { | |
foreach ($Pattern in $Include) { | |
if ($_.Source -like $Pattern) { | |
Write-Output $_ | |
Return | |
} | |
} | |
} | |
} | | |
# Process -Exclude filter | |
ForEach-Object { | |
if (!$Exclude.Length) { | |
Write-Output $_ | |
} | |
else { | |
foreach ($Pattern in $Exclude) { | |
if ($_.Source -like $Pattern) { | |
Return | |
} | |
} | |
Write-Output $_ | |
} | |
} | | |
Group-Object Name | | |
Where-Object Count -gt 1 | | |
Select-Object -ExpandProperty Group | |
} | |
} | |
<#PSScriptInfo | |
.VERSION 1.0 | |
.DESCRIPTION Checks a set of modules for conflicting command names. | |
.GUID 2a86f4a2-76b4-4869-b148-cdfb91d46776 | |
.AUTHOR Josh Einstein | |
.COMPANYNAME | |
.COPYRIGHT | |
.TAGS | |
.LICENSEURI | |
.PROJECTURI | |
.ICONURI | |
.EXTERNALMODULEDEPENDENCIES | |
.REQUIREDSCRIPTS | |
.EXTERNALSCRIPTDEPENDENCIES | |
.RELEASENOTES | |
#> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment