Invoke-WebRequest - the property is displayed as having a value, but when accessed in a certain way, it is equal to zero

I use the following command to pull values ​​from html input fields in a web management interface on a simple linux print device.

$get = Invoke-WebRequest -Uri ("http://" + $station + ":8080/cgi-bin/admin?page=pedi.sh&hash=" + $hash) -Method "GET 

One of the input fields is a checkbox, I need to check if this is checked or not. In this case, the flag is called "Protocol" and it is checked.

 $get.ParsedHtml.getElementsByName("Protocol") | Select checked 

checked
True

If I try to get the value with the following command, it returns nothing.

 ($get.ParsedHtml.getElementsByName("Protocol")).checked 

But if I do this, I get the value ...

 ($get.ParsedHtml.getElementsByName("Protocol") | Select checked).checked 

I wonder why I see this behavior, is this normal?

I am running Windows 8 RTM, PowerShell 3.0. If you need more information, please let me know.

+4
source share
1 answer

I reproduced your problem. To get the value of the checked property (or any other property), I had to force the returned object to be an array, for example:

 @($get.ParsedHtml.getElementsByName("Protocol"))[0].checked 

Or, since the new Powershell 3.0 property enumeration function:

 @($get.ParsedHtml.getElementsByName("Protocol")).checked 

So it seems that it only gives a value when a value is requested, by listing the collection returned from getElementsByName. Maybe someone can intervene and explain this behavior. I assume this is due to the fact that this is using com objects?

+2
source

Source: https://habr.com/ru/post/1441606/


All Articles