Closed ThomasNieto closed 1 year ago
Thanks @ThomasNieto this was by design, so we are discussing whether this should be added... we are wondering if a warning might be more appropriate since the input is valid
@SydneyhSmith why was this design chosen? This is bad design in my opinion and has inconsistent behavior when compared to core cmdlets. Warnings are insufficient because they are difficult to automate against since they are text messages not a rich object with an error/exception and target object. Lets take Get-Item
for example. If the user tries to get a non-existent item it will write a non-terminating error. As the user or scripter I can then make decisions based on the error exception, fully qualfied error id, target object or any other property. If I desire I can then catch only the ItemNotFoundException
or PathNotFound
and then do something with the TargetObject
while leaving any other potential errors go up the call stack.
TLDR: If I tell the command to do something and it cannot for whatever reason it should write an error.
PS C:\> Get-Item -Path c:\doesnotexist.txt
Get-Item: Cannot find path 'C:\doesnotexist.txt' because it does not exist.
PS C:\> Get-Error
Exception :
Type : System.Management.Automation.ItemNotFoundException
ErrorRecord :
Exception :
Type : System.Management.Automation.ParentContainsErrorRecordException
Message : Cannot find path 'C:\doesnotexist.txt' because it does not exist.
HResult : -2146233087
TargetObject : C:\doesnotexist.txt
CategoryInfo : ObjectNotFound: (C:\doesnotexist.txt:String) [], ParentContainsErrorRecordException
FullyQualifiedErrorId : PathNotFound
ItemName : C:\doesnotexist.txt
SessionStateCategory : Drive
TargetSite :
Name : ExpandMshGlobPath
DeclaringType : System.Management.Automation.LocationGlobber, System.Management.Automation, Version=7.2.5.500,
Culture=neutral, PublicKeyToken=31bf3856ad364e35
MemberType : Method
Module : System.Management.Automation.dll
Message : Cannot find path 'C:\doesnotexist.txt' because it does not exist.
Source : System.Management.Automation
HResult : -2146233087
StackTrace :
at System.Management.Automation.LocationGlobber.ExpandMshGlobPath(String path, Boolean allowNonexistingPaths,
PSDriveInfo drive, ContainerCmdletProvider provider, CmdletProviderContext context)
at System.Management.Automation.LocationGlobber.ResolveDriveQualifiedPath(String path, CmdletProviderContext
context, Boolean allowNonexistingPaths, CmdletProvider& providerInstance)
at System.Management.Automation.LocationGlobber.GetGlobbedMonadPathsFromMonadPath(String path, Boolean
allowNonexistingPaths, CmdletProviderContext context, CmdletProvider& providerInstance)
at System.Management.Automation.LocationGlobber.GetGlobbedProviderPathsFromMonadPath(String path, Boolean
allowNonexistingPaths, CmdletProviderContext context, ProviderInfo& provider, CmdletProvider& providerInstance)
at System.Management.Automation.SessionStateInternal.GetItem(String[] paths, CmdletProviderContext context)
at System.Management.Automation.ItemCmdletProviderIntrinsics.Get(String path, CmdletProviderContext context)
at Microsoft.PowerShell.Commands.GetItemCommand.ProcessRecord()
TargetObject : C:\doesnotexist.txt
CategoryInfo : ObjectNotFound: (C:\doesnotexist.txt:String) [Get-Item], ItemNotFoundException
FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand
InvocationInfo :
MyCommand : Get-Item
ScriptLineNumber : 1
OffsetInLine : 1
HistoryId : 19
Line : Get-Item -Path c:\doesnotexist.txt, c:\temp
PositionMessage : At line:1 char:1
+ Get-Item -Path c:\doesnotexist.txt, c:\temp
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
InvocationName : Get-Item
CommandOrigin : Internal
ScriptStackTrace : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo :
Looks like find returns an error but the error message should be more clear, get still doesn't return anything
PS /Users/sysmith2/Downloads/PowerShellGet-interface> Find-PSResource fakepackage -Repository PSGallery
Find-PSResource: Package with search criteria: Name fakepackage could not be found due to: Response didn't contain properties element.
PS /Users/sysmith2/Downloads/PowerShellGet-interface> Get-PSResource -name fakePackage
Prerequisites
Steps to reproduce
If you use
Find-PSResource
orGet-PSResource
on a non-existent resource no error will be output.Expected behavior
Actual behavior
Error details
Environment data
Visuals
No response