Skip to content

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

Closed

Description

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

Activity

  1. changed the title [-]Get-Command: Returned CommandInfo object does not populate ScriptBlock parameter when -Name parameter is not used[/-] [+]Get-Command: Returned CommandInfo object does not populate ScriptBlock property when -Name parameter is not used[/+] on Feb 18, 2019
  2. added
    Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributors
    on Feb 19, 2019
  3. bergmeister commented on Mar 22, 2019

    @bergmeister
    ContributorAuthor

    NB: After discussing this issue with members of the PowerShell team, it seems this is not some sort of optimisation but rather a bug that we could look into fixing.

  4. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  5. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  6. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  7. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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