Author Posts

April 24, 2018 at 7:47 pm

I've got a weird issue with getting different output from commands when run as part of a function vs. running them directly on the command line. First, a little history. I've got a module that I keep updated with commands that are useful to our IT organization. As it's grown, our staff has trouble keeping up with all the commands that are available. In addition, I'm trying to start to break up the module into multiple files that get loaded, so we can logically separate our functions but have only one module to manage/update. Here's where it gets weird. If I run the following:

Get-Command | Where-Object { $_.Source -eq "PowerShell" }

Then I get the following output (truncated for easier viewing):

CommandType     Name                                               Version    Source
———–     —-                                               ——-    ——
Function        Close-ExchangeSession                              2.3.1      PowerShell
Function        Close-O365                                         2.3.1      PowerShell
Function        Close-O365Compliance                               2.3.1      PowerShell
Function        Close-O365Exchange                                 2.3.1      PowerShell
Function        Close-O365Sharepoint                               2.3.1      PowerShell
Function        Close-O365Skype                                    2.3.1      PowerShell
Function        Connect-ExchangeSession                            2.3.1      PowerShell
Function        Connect-O365                                       2.3.1      PowerShell
Function        Connect-O365Compliance                             2.3.1      PowerShell
Function        Connect-O365Exchange                               2.3.1      PowerShell
Function        Connect-O365Sharepoint                             2.3.1      PowerShell
Function        Connect-O365Skype                                  2.3.1      PowerShell
Function        Test-ExchangeSession                               2.3.1      PowerShell
Function        Test-O365ExchangeSession                           2.3.1      PowerShell
Function        test-test123                                       2.3.1      PowerShell
Function        Update-GitRepos                                    2.3.1      PowerShell
Function        Update-ORGCommands                                 2.3.1      PowerShell

Notice the one toward the bottom, test-test123 which has a version number (from the module manifest). Contrast that with the output from this function in my module:

function Get-ORGCommands {
    
	
    Get-Command | Where-Object { $_.Source -eq "PowerShell" }
}
CommandType     Name                                               Version    Source
———–     —-                                               ——-    ——
Function        Close-ExchangeSession                              2.3.1      PowerShell
Function        Close-O365                                         2.3.1      PowerShell
Function        Close-O365Compliance                               2.3.1      PowerShell
Function        Close-O365Exchange                                 2.3.1      PowerShell
Function        Close-O365Sharepoint                               2.3.1      PowerShell
Function        Close-O365Skype                                    2.3.1      PowerShell
Function        Connect-ExchangeSession                            2.3.1      PowerShell
Function        Connect-O365                                       2.3.1      PowerShell
Function        Connect-O365Compliance                             2.3.1      PowerShell
Function        Connect-O365Exchange                               2.3.1      PowerShell
Function        Connect-O365Sharepoint                             2.3.1      PowerShell
Function        Connect-O365Skype                                  2.3.1      PowerShell
Function        Test-ExchangeSession                               2.3.1      PowerShell
Function        Test-O365ExchangeSession                           2.3.1      PowerShell
Function        test-test123                                       0.0        PowerShell
Function        Update-GitRepos                                    2.3.1      PowerShell
Function        Update-ORGCommands                                 2.3.1      PowerShell

In that output, you'll notice that the test-test123 function is there, but doesn't have the version from the manifest file. This function is loaded in the module through the NestedModules statement:

NestedModules = @('.\test\test.psm1')

Any ideas as to why I'm getting different output from the function as opposed to running the same thing straight on the command line?

April 26, 2018 at 4:27 am

Just tried in PS 5.1 on Win 10 and cannot replicate this.

Have you tried

get-command -module "PowerShell"

April 26, 2018 at 4:10 pm

I have tried that version of Get-Command, and somehow it is worse when used as a function. On the command line it works correctly, as you'll see in the output below. From the function, it doesn't pull the commands from the nested module at all. Tested on Windows 10, PS version 5.1. Able to replicate on multiple machines (same behavior).

> Get-Command -Module PowerShell

CommandType     Name                                               Version    Source
———–     —-                                               ——-    ——
Function        Close-ExchangeSession                              2.3.1      PowerShell
Function        Close-O365                                         2.3.1      PowerShell
Function        Close-O365Compliance                               2.3.1      PowerShell
Function        Close-O365Exchange                                 2.3.1      PowerShell
Function        Close-O365Sharepoint                               2.3.1      PowerShell
Function        Close-O365Skype                                    2.3.1      PowerShell
Function        Connect-ExchangeSession                            2.3.1      PowerShell
Function        Connect-O365                                       2.3.1      PowerShell
Function        Connect-O365Compliance                             2.3.1      PowerShell
Function        Connect-O365Exchange                               2.3.1      PowerShell
Function        Connect-O365Sharepoint                             2.3.1      PowerShell
Function        Connect-O365Skype                                  2.3.1      PowerShell
Function        Test-ExchangeSession                               2.3.1      PowerShell
Function        Test-O365ExchangeSession                           2.3.1      PowerShell
Function        test-test123                                       2.3.1      PowerShell
Function        Update-GitRepos                                    2.3.1      PowerShell
Function        Update-ORGCommands                                 2.3.1      PowerShell



function Get-ORGCommands {
    
	
    #Get-Command | Where-Object { $_.Source -eq "PowerShell" }
    Get-Command -Module "PowerShell"
}

> Get-ORGCommands

CommandType     Name                                               Version    Source
———–     —-                                               ——-    ——
Function        Close-ExchangeSession                              2.3.1      PowerShell
Function        Close-O365                                         2.3.1      PowerShell
Function        Close-O365Compliance                               2.3.1      PowerShell
Function        Close-O365Exchange                                 2.3.1      PowerShell
Function        Close-O365Sharepoint                               2.3.1      PowerShell
Function        Close-O365Skype                                    2.3.1      PowerShell
Function        Connect-ExchangeSession                            2.3.1      PowerShell
Function        Connect-O365                                       2.3.1      PowerShell
Function        Connect-O365Compliance                             2.3.1      PowerShell
Function        Connect-O365Exchange                               2.3.1      PowerShell
Function        Connect-O365Sharepoint                             2.3.1      PowerShell
Function        Connect-O365Skype                                  2.3.1      PowerShell
Function        Test-ExchangeSession                               2.3.1      PowerShell
Function        Test-O365ExchangeSession                           2.3.1      PowerShell
Function        Update-GitRepos                                    2.3.1      PowerShell
Function        Update-ORGCommands                                 2.3.1      PowerShell