Powershell: GCIM Alias does not work in PowerShell 7 RC3

Created on 2 Mar 2020  Â·  18Comments  Â·  Source: PowerShell/PowerShell

The GCIM alias works in WIndows PowerShell 5.1 and points to Get-CImInstance.

This alias is not present in RC3

Steps to reproduce

GCIM -Class WIn32_bios

Expected behavior

PS C:\foo> gcim -class win32_Bios

SMBIOSBIOSVersion : A16
Manufacturer      : Dell Inc.
Name              : Phoenix ROM BIOS PLUS Version 1.10 A16
SerialNumber      : 87Q285J
Version           : DELL   - 15

Actual behavior

PS C:foo> gcim win32_bios
gcim: The term 'gcim' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Suggestion [4,General]: The most similar commands are: gci, gcm, gc, gcs, gi, gm, icm, gcb, gin.


Environment data

PS C:\foo> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.0.0-rc.3
PSEdition                      Core
GitCommitId                    7.0.0-rc.3
OS                             Microsoft Windows 10.0.19569
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0, 5.0, 5.1.10032.0, 6.0.0, 6.1.0, 6.2.0, 7.0.0-rc.3}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Please return this alias as it's useful at the command line.

Area-Cmdlets First-Time-Issue Issue-Bug Resolution-Fixed Up-for-Grabs

Most helpful comment

I've gone ahead and created a pull request for just that, my first one, so hope I got it somewhat right

All 18 comments

I can not repo:

 get-alias gcim

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           gcim -> Get-CimInstance

it works as expected (PowerShell 7 RC3)

GCIM -Class WIn32_bios

SMBIOSBIOSVersion : 02.25
Manufacturer      : AMI
Name              : BIOS Date: 10/24/18 11:07:25 Ver: 04.06.05
SerialNumber      : CZC5390L7Y
Version           : HPQOEM - 1072009

Wow that's bizarre. It works for me now, but I know for fact I've hit a command not found for gcim since installing RC3.

If I can figure out how to repro I'll update, but something is up.

I can't repo it now either - but it _was_ failing.

@iSazonov I wouldn't close this yet. There appears to be something situational that causes the command lookup to fail. Until we know what that is, the issue should stay open.

Close is not remove :-) As soon as someone can reproduce the problem we will continue.

Yeah, I know it's not removed. I'm saying "we don't know what causes the problem" is not a good reason to close an issue.

Well - if it's closed it's effectively removed. :-)

I have seen this a couple of times but it is NOT repeatable. And it _always_ goes away after a reboot.

@doctordns You could use transcription or logging to get more info. Or add (Get-Alias).Count in prompt function to see every second the count.

There's definitely some race condition that triggers this. I had it happen again, here's some info:

Note that f in this context is a custom alias to ForEach-Object

Whole transcript

Loading personal and system profiles took 2272ms.
PS> gcm add-odbcdsn

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        Add-OdbcDsn                                        1.0.0.0    Wdac

PS> gcm add-odbcdsn|f Definition

    [CmdletBinding(PositionalBinding=$false)]

    [OutputType([Microsoft.Management.Infrastructure.CimInstance[]])][OutputType('Microsoft.Management.Infrastructure.CimInstance#MSFT_OdbcDsn')]

    param(

    [Parameter(ParameterSetName='Add0', Mandatory=$true, Position=0, ValueFromPipelineByPropertyName=$true)]
    [Alias('DsnName')]
    [ValidateLength(1, 32)]
    [ValidateNotNull()]
    [ValidateNotNullOrEmpty()]
    [string]
    ${Name},

    [Parameter(ParameterSetName='Add0', Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
    [ValidateNotNull()]
    [ValidateNotNullOrEmpty()]
    [string]
    ${DriverName},

    [Parameter(ParameterSetName='Add0', ValueFromPipelineByPropertyName=$true)]
    [ValidateNotNull()]
    [ValidateNotNullOrEmpty()]
    [string[]]
    ${SetPropertyValue},

    [Parameter(ParameterSetName='Add0')]
    [ValidateNotNull()]
    [ValidateNotNullOrEmpty()]
    [switch]
    ${PassThru},

    [Parameter(ParameterSetName='Add0', ValueFromPipelineByPropertyName=$true)]
    [ValidateNotNull()]
    [ValidateNotNullOrEmpty()]
    [ValidateSet('32-bit','64-bit')]
    [string]
    ${Platform},

    [Parameter(ParameterSetName='Add0', Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
    [ValidateNotNull()]
    [ValidateNotNullOrEmpty()]
    [ValidateSet('User','System')]
    [string]
    ${DsnType},

    [Parameter(ParameterSetName='Add0')]
    [Alias('Session')]
    [ValidateNotNullOrEmpty()]
    [CimSession[]]
    ${CimSession},

    [Parameter(ParameterSetName='Add0')]
    [int]
    ${ThrottleLimit},

    [Parameter(ParameterSetName='Add0')]
    [switch]
    ${AsJob})

    DynamicParam {
        try
        {
            if (-not $__cmdletization_exceptionHasBeenThrown)
            {
                $__cmdletization_objectModelWrapper = $script:ObjectModelWrapper::new()
                $__cmdletization_objectModelWrapper.Initialize($PSCmdlet, $script:ClassName, $script:ClassVersion, $script:ModuleVersion, $script:PrivateData)

                if ($__cmdletization_objectModelWrapper -is [System.Management.Automation.IDynamicParameters])
                {
                    ([System.Management.Automation.IDynamicParameters]$__cmdletization_objectModelWrapper).GetDynamicParameters()
                }
            }
        }
        catch
        {
            $__cmdletization_exceptionHasBeenThrown = $true
            throw
        }
    }

    Begin {
        $__cmdletization_exceptionHasBeenThrown = $false
        try
        {
            __cmdletization_BindCommonParameters $__cmdletization_objectModelWrapper $PSBoundParameters
            $__cmdletization_objectModelWrapper.BeginProcessing()
        }
        catch
        {
            $__cmdletization_exceptionHasBeenThrown = $true
            throw
        }
    }


    Process {
        try
        {
            if (-not $__cmdletization_exceptionHasBeenThrown)
            {
      $__cmdletization_methodParameters = [System.Collections.Generic.List[Microsoft.PowerShell.Cmdletization.MethodParameter]]::new()

        [object]$__cmdletization_defaultValue = $null
        [object]$__cmdletization_defaultValueIsPresent = $false
        if ($PSBoundParameters.ContainsKey('Name')) {
          [object]$__cmdletization_value = ${Name}
          $__cmdletization_methodParameter = [Microsoft.PowerShell.Cmdletization.MethodParameter]@{Name = 'Name'; ParameterType = 'System.String'; Bindings = 'In'; Value = $__cmdletization_value; IsValuePresent = $true}
        } else {
          $__cmdletization_methodParameter = [Microsoft.PowerShell.Cmdletization.MethodParameter]@{Name = 'Name'; ParameterType = 'System.String'; Bindings = 'In'; Value = $__cmdletization_defaultValue; IsValuePresent = $__cmdletization_defaultValueIsPresent}
        }
        $__cmdletization_methodParameters.Add($__cmdletization_methodParameter)

        [object]$__cmdletization_defaultValue = $null
        [object]$__cmdletization_defaultValueIsPresent = $false
        if ($PSBoundParameters.ContainsKey('DriverName')) {
          [object]$__cmdletization_value = ${DriverName}
          $__cmdletization_methodParameter = [Microsoft.PowerShell.Cmdletization.MethodParameter]@{Name = 'DriverName'; ParameterType = 'System.String'; Bindings = 'In'; Value = $__cmdletization_value; IsValuePresent = $true}
        } else {
          $__cmdletization_methodParameter = [Microsoft.PowerShell.Cmdletization.MethodParameter]@{Name = 'DriverName'; ParameterType = 'System.String'; Bindings = 'In'; Value = $__cmdletization_defaultValue; IsValuePresent = $__cmdletization_defaultValueIsPresent}
        }
        $__cmdletization_methodParameters.Add($__cmdletization_methodParameter)

        [object]$__cmdletization_defaultValue = $null
        [object]$__cmdletization_defaultValueIsPresent = $false
        if ($PSBoundParameters.ContainsKey('SetPropertyValue')) {
          [object]$__cmdletization_value = ${SetPropertyValue}
          $__cmdletization_methodParameter = [Microsoft.PowerShell.Cmdletization.MethodParameter]@{Name = 'SetPropertyValue'; ParameterType = 'System.String[]'; Bindings = 'In'; Value = $__cmdletization_value; IsValuePresent = $true}
        } else {
          $__cmdletization_methodParameter = [Microsoft.PowerShell.Cmdletization.MethodParameter]@{Name = 'SetPropertyValue'; ParameterType = 'System.String[]'; Bindings = 'In'; Value = $__cmdletization_defaultValue; IsValuePresent = $__cmdletization_defaultValueIsPresent}
        }
        $__cmdletization_methodParameters.Add($__cmdletization_methodParameter)

        [object]$__cmdletization_defaultValue = $null
        [object]$__cmdletization_defaultValueIsPresent = $false
        if ($PSBoundParameters.ContainsKey('PassThru')) {
          [object]$__cmdletization_value = ${PassThru}
          $__cmdletization_methodParameter = [Microsoft.PowerShell.Cmdletization.MethodParameter]@{Name = 'PassThru'; ParameterType = 'System.Management.Automation.SwitchParameter'; Bindings = 'In'; Value = $__cmdletization_value; IsValuePresent = $true}
        } else {
          $__cmdletization_methodParameter = [Microsoft.PowerShell.Cmdletization.MethodParameter]@{Name = 'PassThru'; ParameterType = 'System.Management.Automation.SwitchParameter'; Bindings = 'In'; Value = $__cmdletization_defaultValue; IsValuePresent = $__cmdletization_defaultValueIsPresent}
        }
        $__cmdletization_methodParameters.Add($__cmdletization_methodParameter)

        [object]$__cmdletization_defaultValue = $null
        [object]$__cmdletization_defaultValueIsPresent = $false
        if ($PSBoundParameters.ContainsKey('Platform')) {
          [object]$__cmdletization_value = ${Platform}
          $__cmdletization_methodParameter = [Microsoft.PowerShell.Cmdletization.MethodParameter]@{Name = 'Platform'; ParameterType = 'System.String'; Bindings = 'In'; Value = $__cmdletization_value; IsValuePresent = $true}
        } else {
          $__cmdletization_methodParameter = [Microsoft.PowerShell.Cmdletization.MethodParameter]@{Name = 'Platform'; ParameterType = 'System.String'; Bindings = 'In'; Value = $__cmdletization_defaultValue; IsValuePresent = $__cmdletization_defaultValueIsPresent}
        }
        $__cmdletization_methodParameters.Add($__cmdletization_methodParameter)

        [object]$__cmdletization_defaultValue = $null
        [object]$__cmdletization_defaultValueIsPresent = $false
        if ($PSBoundParameters.ContainsKey('DsnType')) {
          [object]$__cmdletization_value = ${DsnType}
          $__cmdletization_methodParameter = [Microsoft.PowerShell.Cmdletization.MethodParameter]@{Name = 'DsnType'; ParameterType = 'System.String'; Bindings = 'In'; Value = $__cmdletization_value; IsValuePresent = $true}
        } else {
          $__cmdletization_methodParameter = [Microsoft.PowerShell.Cmdletization.MethodParameter]@{Name = 'DsnType'; ParameterType = 'System.String'; Bindings = 'In'; Value = $__cmdletization_defaultValue; IsValuePresent = $__cmdletization_defaultValueIsPresent}
        }
        $__cmdletization_methodParameters.Add($__cmdletization_methodParameter)

        [object]$__cmdletization_defaultValue = $null
        [object]$__cmdletization_defaultValueIsPresent = $false
          $__cmdletization_methodParameter = [Microsoft.PowerShell.Cmdletization.MethodParameter]@{Name = 'CmdletOutput'; ParameterType = 'Microsoft.Management.Infrastructure.CimInstance[]'; Bindings = 'Out'; Value = $__cmdletization_defaultValue; IsValuePresent = $__cmdletization_defaultValueIsPresent}
        $__cmdletization_methodParameter.ParameterTypeName = 'Microsoft.Management.Infrastructure.CimInstance#MSFT_OdbcDsn'
        $__cmdletization_methodParameters.Add($__cmdletization_methodParameter)

      $__cmdletization_returnValue = [Microsoft.PowerShell.Cmdletization.MethodParameter]@{ Name = 'ReturnValue'; ParameterType = 'System.Int32'; Bindings = 'Error'; Value = $null; IsValuePresent = $false }
      $__cmdletization_methodInvocationInfo = [Microsoft.PowerShell.Cmdletization.MethodInvocationInfo]::new('Add', $__cmdletization_methodParameters, $__cmdletization_returnValue)
      $__cmdletization_objectModelWrapper.ProcessRecord($__cmdletization_methodInvocationInfo)

            }
        }
        catch
        {
            $__cmdletization_exceptionHasBeenThrown = $true
            throw
        }
    }


    End {
        try
        {
            if (-not $__cmdletization_exceptionHasBeenThrown)
            {
                $__cmdletization_objectModelWrapper.EndProcessing()
            }
        }
        catch
        {
            throw
        }
    }

    # .EXTERNALHELP MSFT_OdbcDsnTask_v1.0.cdxml-Help.xml

PS> gcm add-odbcdsn

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        Add-OdbcDsn                                        1.0.0.0    Wdac

PS> gmo wdac

ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Manifest   1.0.0.0               Wdac                                {Add-OdbcDsn, Disable-OdbcPerfCounter, Disable-WdacBidTrace, Enable-O…

PS> gmo wdac|f path
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\Wdac\Wdac.psd1
PS> gcim -namesp root/microsoft/windows/wdac msft_odbcdsntask
gcim: The term 'gcim' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Suggestion [4,General]: The most similar commands are: gci, gcm, gc, gcs, gi, gm, icm, gcb, gin, vim.

Just the commands

gcm add-odbcdsn
gcm add-odbcdsn|f Definition
gcm add-odbcdsn
gmo wdac
gmo wdac|f path
gcim -namesp root/microsoft/windows/wdac msft_odbcdsntask

Repeating those steps exactly doesn't repro which is what makes me think it's a race condition. I tend to have quite a few pwsh windows open and move between them a lot. I think it's possible that something I was doing in one of the other windows at the same time as something else caused this, but I couldn't tell you what else I was doing at the time.

I tried a few things to get gcim back in that session, nothing worked. The CIM commands like Get-CimInstance worked fine, just the alias was missing. In all other sessions gcim is found without issue.

@iSazonov Please reopen this issue.

You could look __how__ the alias and some other aliases is created at the module load time.

@iSazonov oh... yeah... good call...

I had assumed it was a built-in alias since gcm gcim doesn't show a module as a source.

https://github.com/PowerShell/PowerShell/blob/2c0f138a96c100093c1af8c091410f47647311c4/src/Microsoft.Management.Infrastructure.CimCmdlets/CimCmdletModuleInitialize.cs#L35-L53

That's really fragile, here's a super easy repro:

# Start a new session and run this:
& { $null = Get-CimInstance win32_process <# or any other class #>  }
gcim

It probably occurred with what I posted above because I have a custom TabExpansion2 and must have triggered the import through that without realizing it.

Doesn't repro in Windows PowerShell though, so maybe there was a change made to how AllScope aliases are resolved (alias.Options is ReadOnly, AllScope).

We could move this to alias attributes.

Seems odd not to be using the alias attributes, indeed. Would there be a reason not to be? Or just because the code hasn't been updated in a while?

Maybe they wanted to demo IModuleAssemblyInitializer? Or the CIM cmdlets were just written by another team that didn't know about the attribute? I thought it was added in PSv3, but I could be wrong.

Either way, definitely should use the attribute.

I've gone ahead and created a pull request for just that, my first one, so hope I got it somewhat right

:tada:This issue was addressed in #12617, which has now been successfully released as v7.1.0-preview.4.:tada:

Handy links:

Was this page helpful?
0 / 5 - 0 ratings