What is the best way to access KeyValuePairs from .Net classes in Powershell (core/7)? I have found that the classes I'm trying to use will not let me access the value using the key name in the indexer.
My current theory is that its due to the underlying .net classes making use of Extension methods which Powershell cannot resolve (but I could be on the wrong path with that...).
Here is an example object:
$HTTPMsg = [System.Net.Http.HttpRequestMessage]::new()
$HTTPMsg.Headers.Add("WWW-Authenticate", "Basic realm=C:\xxx")
$HTTPMsg.Headers.Add("Date", "Wed, 02 Mar 2022 09:41:36 GMT")
$HTTPMsg.Headers
***Output:
Key Value
--- -----
WWW-Authenticate {Basic realm=C:\xxx}
Date {Wed, 02 Mar 2022 09:41:36 GMT}
Ideally, I would have liked to be able to access the Date value with:
$MyDate = $HTTPMsg.Headers["Date"]
But this returns $null.
So far the best way I've found is as follows, but this looks way harder than it should be:
$MyDate = $HTTPMsg.Headers.Value[$HTTPMsg.Headers.Key.IndexOf("Date")]
What am I missing?
Thanks in advance!
@Jeroen Mostert answered in the comments:
$HTTPMsg.Headers.GetValues("Date")