Skip to content

Get-Command: Returned CommandInfo object does not populate ScriptBlock property when -Name parameter is not used #8910

@bergmeister

Description

@bergmeister

Steps to reproduce

I created a Pester test for this

Describe "My test" {
    It "works" {
        (Get-Command -CommandType Function | Where-Object { $_.Name -eq 'Install-Module' }).ScriptBlock.Attributes | Should -Not -BeNullOrEmpty
        (Get-Command 'Install-Module').ScriptBlock.Attributes | Should -Not -BeNullOrEmpty 
    }
}

Expected behavior

The test passes.

Actual behavior

  Describing My test
    [-] works 84ms
      Expected a value, but got $null or empty.
      3:         (Get-Command -CommandType Function | Where-Object { $_.Name -eq 'Install-Module' }).ScriptBlock.Attributes | Should -Not -BeNullOrEmpty
      at <ScriptBlock>, C:\Users\cberg\Desktop\foo.tests.ps1: line 3

The test passed only once Get-Command 'Install-Module' gets executed once in the current session.
I acknowledge that Get-Command tries to be more efficient when returning all commands but at least when accessing the properties of the object I'd expect them to be lazily evaluated.
Background: In order to improve performance of PSSA, I would like to run Get-Command in the background once to populate a cached version of the commands instead of individually calling Get-Command -Name for each command, which is much more expensive.

Environment data


Name                           Value
----                           -----
PSVersion                      6.2.0-preview.4
PSEdition                      Core
GitCommitId                    6.2.0-preview.4
OS                             Microsoft Windows 10.0.17763
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-No ActivityIssue has had no activity for 6 months or moreUp-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributors

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions