jdhitsolutions / PSScriptTools

:wrench: :hammer: A set of PowerShell functions you might use to enhance your own functions and scripts or to facilitate working in the console. Most should work in both Windows PowerShell and PowerShell 7, even cross-platform. Any operating system limitations should be handled on a per command basis. The Samples folder contains demonstration script files
MIT License
901 stars 112 forks source link

Get-ModuleCommand should (optionally?) look at Value in ExportedCommands #112

Closed cdhunt closed 1 year ago

cdhunt commented 3 years ago

Is your feature request related to a problem? Please describe. I have a module that uses a DefaultCommandPrefix to disambiguate from built-in cmdlet names. Get-ModuleCommand operates on the key which is the command name before applying the prefix. This causes it to query info on the built-in command versus my command.

Example:

DefaultCommandPrefix = 'my' New-Service vs New-myService

I want info on New-myService

Describe the solution you'd like If Key -ne Value, use Value in Get-ModuleCommand.ps1#L53

Describe alternatives you've considered I don't see anywhere that DefaultCommandPrefix is exposed to the runtime so you can programmatically check.

I believe you could also always just use Value instead of Key.

Additional context

Get-Module myModule| Select-Object -ExpandProperty ExportedCommands

Key                      Value
---                      -----
Get-Client               Get-myClient
Get-Config               Get-myConfig
Get-CurrentContext       Get-myCurrentContext
Get-Namespace            Get-myNamespace
Get-Service              Get-myService
New-Namespace            New-myNamespace
New-Service              New-myService
New-ServicePort          New-myServicePort
New-ServiceSpec          New-myServiceSpec
Remove-Namespace         Remove-myNamespace
Remove-Service           Remove-myService
Set-Namespace            Set-myNamespace
Set-Service              Set-myService
Get-ModuleCommand

   ModuleName: myModule

Name                     Alias      Synopsis
----                     -----      --------
Get-Service              gsv        Gets the services on the computer.
New-Service                         Creates a new Windows service.
Remove-Service                      Removes a Windows service.
Set-Service                         Starts, stops, and suspends a service, and changes its properties.
cdhunt commented 3 years ago

After poking around ExportedCommands for a few minutes I can't track down what is generating the name with prefix. The prefix or prefixed name doesn't seem to be in CmdletInfo. Pesky FormatData.

jdhitsolutions commented 3 years ago

What version of PowerShell are you running this under? I modified a module to use a default prefix and this works in 5.1.

image

It also works in PS 7 if the module is loaded

image

But there is a problem if it isn't loaded.

image

But only in PowerShell 7. Windows PowerShell works fine.

cdhunt commented 3 years ago

Yeah, I'm running in 7.

jdhitsolutions commented 3 years ago

This looks like a PowerShell 7 bug with Get-Module. Investigating.

jdhitsolutions commented 3 years ago

Microsoft agrees that this is a bug in PowerShell 7. The workaround for us appears to be first import the module then run Get-ModuleCommand.

jdhitsolutions commented 2 years ago

This is still a bug in PowerShell 7.2.

jdhitsolutions commented 1 year ago

Is this still an issue for you? It has been a while since this was opened.

cdhunt commented 1 year ago

I'm not currently using this module. I can't say.

jdhitsolutions commented 1 year ago

I think I'm going to close the issue then. We can always re-open it or start anew.