Powershell: Get-FormatData quietly ignores non-existing type-name arguments

Created on 12 Jul 2017  路  6Comments  路  Source: PowerShell/PowerShell

Steps to reproduce

Get-FormatData NoSuch.Type.Exists.Or.IsLoaded

Expected behavior

An error that indicates that the type does not exist (in the current session).

Actual behavior

A quiet no-op. No output, $? reports $True.

Environment data

PowerShell Core v6.0.0-beta.3 on macOS 10.12.5
PowerShell Core v6.0.0-beta.3 on Ubuntu 16.04.1 LTS
PowerShell Core v6.0.0-beta.3 on Microsoft Windows 10 Pro (64-bit; v10.0.14393)
Windows PowerShell v5.1.15063.413 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)
Area-Cmdlets-Utility Resolution-Fixed Up-for-Grabs

All 6 comments

I would like to work on this

@mklement0 @SteveL-MSFT What should the ErrorCategory be?

@faraazahmad:

Thanks for your willingness to take this on.

I'm not the best person to ask, but looking at the enumeration values at https://docs.microsoft.com/en-us/dotnet/api/system.management.automation.errorcategory?view=powershellsdk-1.1.0, perhaps ResourceUnavailable?

@faraazahmad consider it yours! Thanks!

So my code now outputs this:

PS /home/faraaz> Get-FormatData yolo
Get-FormatData : No such type
At line:1 char:1
+ Get-FormatData yolo
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : ResourceUnavailable: (System.String[]:String[]) [Get-FormatData], Exception
+ FullyQualifiedErrorId : SPECIFIED_TYPE_NOT_FOUND,Microsoft.PowerShell.Commands.GetFormatDataCommand

Is this all right? And should I just check for $? in the test or something more?
:)

HANG ON ... This is a breaking change to the function!!!

Does -ErrorAction SilentlyContinue work, or are you now forcing everyone to wrap this in a try/catch block?

I understand the desire to have this produce an error, but we can't make existing commands throw exceptions in use cases that used to just return nothing.

You're breaking every instance of existing code that looks like this, right?

if(!(Get-FormatData $Type)) {
   Update-FormatData -TypeName $Type ...
}
Was this page helpful?
0 / 5 - 0 ratings