Powershell: Get-Item fails to retrieve scoped items in Scoped PSDrives (Alias/Function/Variable)

Created on 9 Dec 2018  路  4Comments  路  Source: PowerShell/PowerShell

Steps to reproduce

Get-Content function:prompt
Get-Item function:prompt
Get-Content function:global:prompt
Get-Item function:global:prompt

Get-Content variable:ConfirmPreference
Get-Item variable:ConfirmPreference
Get-Content variable:global:ConfirmPreference
Get-Item variable:global:ConfirmPreference

Expected behavior

"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) ";
# .Link
# https://go.microsoft.com/fwlink/?LinkID=225750
# .ExternalHelp System.Management.Automation.dll-help.xml

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        prompt


"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) ";
# .Link
# https://go.microsoft.com/fwlink/?LinkID=225750
# .ExternalHelp System.Management.Automation.dll-help.xml

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        prompt

High

Name                           Value
----                           -----
ConfirmPreference              High


High

Name                           Value
----                           -----
ConfirmPreference              High

Actual behavior

"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) ";
# .Link
# https://go.microsoft.com/fwlink/?LinkID=225750
# .ExternalHelp System.Management.Automation.dll-help.xml

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        prompt


"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) ";
# .Link
# https://go.microsoft.com/fwlink/?LinkID=225750
# .ExternalHelp System.Management.Automation.dll-help.xml

High

Name                           Value
----                           -----
ConfirmPreference              High


High

The final Get-Item call in both cases returns _nothing_. No error, no output. But _all_ Get-Content calls successfully find the targeted item. As @mklement0 notes, this affects the Alias provider as well as Function and Variable providers.

Environment data

Name                           Value
----                           -----
PSVersion                      6.1.0
PSEdition                      Core
GitCommitId                    6.1.0
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
Area-Cmdlets-Core Issue-Discussion WG-Engine-Providers

All 4 comments

Nice find; also affects the Alias provider, which has scoped definitions too.

Note that namespace variable notation isn't affected, as it is the equivalent of the Get-Content calls; e.g., ${function:global:prompt} works fine.

Indeed, namespace variable notation is what I was toying with when I stumbled across this weird inconsistency!

This is a really interesting find.
Get-ChildItem behaves the same way.

I've been digging into this, and it _looks_ like the code from the affected providers all traces back to somewhere in SessionStateProvider and the methods buried deeply in there somewhere. I'm having difficulty tracing back exactly where this is going wrong at the moment, but I'll keep looking a bit later.

Was this page helpful?
0 / 5 - 0 ratings