In the previous example we looked at how numeric HTTP response codes can automatically be converted to descriptive text, simply by converting them to the type System.Net.HttpStatusCode.
PS> [System.Net.HttpStatusCode]500 InternalServerError
This works because System.Net.HttpStatusCode is a so-called “enumeration” and acts like a “lookup table”. You can easily dump all members of an enumeration, and for example create a nice table of HTTP response codes:
[Enum]::GetValues([System.Net.HttpStatusCode]) | ForEach-Object { [PSCustomObject]@{ Code = [int]$_ Description = $_.toString() } }
That’s all you need to get a table of the most common HTTP response codes:
Code Description ---- ----------- 100 Continue 101 SwitchingProtocols 200 OK 201 Created 202 Accepted 203 NonAuthoritativeInformation 204 NoContent 205 ResetContent 206 PartialContent 300 MultipleChoices 300 MultipleChoices 301 MovedPermanently 301 MovedPermanently 302 Redirect 302 Redirect 303 SeeOther 303 SeeOther 304 NotModified 305 UseProxy 306 Unused 307 TemporaryRedirect 307 TemporaryRedirect 400 BadRequest 401 Unauthorized 402 PaymentRequired 403 Forbidden 404 NotFound 405 MethodNotAllowed 406 NotAcceptable 407 ProxyAuthenticationRequired 408 RequestTimeout 409 Conflict 410 Gone 411 LengthRequired 412 PreconditionFailed 413 RequestEntityTooLarge 414 RequestUriTooLong 415 UnsupportedMediaType 416 RequestedRangeNotSatisfiable 417 ExpectationFailed 426 UpgradeRequired 500 InternalServerError 501 NotImplemented 502 BadGateway 503 ServiceUnavailable 504 GatewayTimeout 505 HttpVersionNotSupported
The approach works for any enumeration you may come across. Simply change the name of the enumeration data type. This example dumps the available console color codes:
[Enum]::GetValues([System.ConsoleColor]) | ForEach-Object { [PSCustomObject]@{ Code = [int]$_ Description = $_.toString() } }
Code Description ---- ----------- 0 Black 1 DarkBlue 2 DarkGreen 3 DarkCyan 4 DarkRed 5 DarkMagenta 6 DarkYellow 7 Gray 8 DarkGray 9 Blue 10 Green 11 Cyan 12 Red 13 Magenta 14 Yellow 15 White