Powershell: NullReferenceException in Get-Command

Created on 18 Apr 2019  路  5Comments  路  Source: PowerShell/PowerShell

Steps to reproduce

# This throws:
PS> get-command "less `"-PsPage %db?B of %D:`"" -ErrorAction Ignore
get-command : Object reference not set to an instance of an object.
# This doesn't:
PS> get-command "less `"-PsPage %db?B of %D`"" -ErrorAction Ignore

It looks like the D: in the string looks like a path??

Expected behavior

This command shouldn't throw a terminating error given this input.

Actual behavior

It throws this exception:

PSMessageDetails      :
Exception             : System.NullReferenceException: Object reference not set to an instance of an object.
                           at System.Management.Automation.CommandSearcher.GetNextFromPath() in C:\Users\Keith\GitHub\r
                        keithhill\PowerShell\src\System.Management.Automation\engine\CommandSearcher.cs:line 477
                           at System.Management.Automation.CommandSearcher.ProcessBuiltinScriptState() in C:\Users\Keit
                        h\GitHub\rkeithhill\PowerShell\src\System.Management.Automation\engine\CommandSearcher.cs:line
                        292
                           at System.Management.Automation.CommandSearcher.MoveNext() in C:\Users\Keith\GitHub\rkeithhi
                        ll\PowerShell\src\System.Management.Automation\engine\CommandSearcher.cs:line 191
                           at
                        Microsoft.PowerShell.Commands.GetCommandCommand.FindCommandForName(SearchResolutionOptions
                        options, String commandName, Boolean isPattern, Boolean emitErrors, Int32& currentCount,
                        Boolean& isDuplicate) in C:\Users\Keith\GitHub\rkeithhill\PowerShell\src\System.Management.Auto
                        mation\engine\GetCommandCommand.cs:line 887
                           at Microsoft.PowerShell.Commands.GetCommandCommand.AccumulateMatchingCommands(IEnumerable`1
                        commandNames) in C:\Users\Keith\GitHub\rkeithhill\PowerShell\src\System.Management.Automation\e
                        ngine\GetCommandCommand.cs:line 759
                           at Microsoft.PowerShell.Commands.GetCommandCommand.AccumulateMatchingCommands() in C:\Users\
                        Keith\GitHub\rkeithhill\PowerShell\src\System.Management.Automation\engine\GetCommandCommand.cs
                        :line 700
                           at Microsoft.PowerShell.Commands.GetCommandCommand.ProcessRecord() in C:\Users\Keith\GitHub\
                        rkeithhill\PowerShell\src\System.Management.Automation\engine\GetCommandCommand.cs:line 421
                           at System.Management.Automation.Cmdlet.DoProcessRecord() in C:\Users\Keith\GitHub\rkeithhill
                        \PowerShell\src\System.Management.Automation\engine\cmdlet.cs:line 173
                           at System.Management.Automation.CommandProcessor.ProcessRecord() in C:\Users\Keith\GitHub\rk
                        eithhill\PowerShell\src\System.Management.Automation\engine\CommandProcessor.cs:line 386
TargetObject          :
CategoryInfo          : NotSpecified: (:) [Get-Command], NullReferenceException
FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Commands.GetCommandCommand
ErrorDetails          :
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {}

Environment data

Name                           Value
----                           -----
PSVersion                      6.2.0-rc.1
PSEdition                      Core
GitCommitId                    6.2.0-rc.1-167-ge9ffbe5cf27ad6461607fcf4bc69258652a88d33
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

Interesting this shows 6.2.0-rc.1 since I'm building off of the latest on master. BTW this is a regression from 6.1.0 where this did not throw an NRE.

Issue-Bug Resolution-Fixed WG-Engine-Providers

All 5 comments

@SteveL-MSFT When will we switch from 6.2.0-rc.1 to 7.0.0-preview.1?

/cc @TravisEz13 The issue comes from #9202.
I debug the code and here we get null in resolvedPaths
https://github.com/PowerShell/PowerShell/blob/e9ffbe5cf27ad6461607fcf4bc69258652a88d33/src/System.Management.Automation/engine/CommandSearcher.cs#L471-L474

Direct fix could be simple but the code works weird and I think we need to make more better fix for the new code:

https://github.com/PowerShell/PowerShell/blob/e9ffbe5cf27ad6461607fcf4bc69258652a88d33/src/System.Management.Automation/engine/CommandSearcher.cs#L458-L493

@iSazonov we should probably switch the version as part of the netcore30 work

@rkeithhill Have you tried to repro this is 6.2.0? never mind. I can repro on the latest code.

This is post 6.2.0. I built PowerShell and was verifying my PR that updates the parsing of $env:PAGER worked for me. It failed because Get-Command threw the NRE. I haven't pulled recently and rebuilt master but I could do that.

@rkeithhill Thanks for reporting the issue.

Was this page helpful?
0 / 5 - 0 ratings