Watch panel evaluation of the result from ConvertTo-DataTable breaks use of DataTable

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST LICENSE NUMBERS, ACTIVATION KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 4 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
User avatar
DrewEaston
Posts: 48
Last visit: Thu Mar 30, 2023 8:47 am

Watch panel evaluation of the result from ConvertTo-DataTable breaks use of DataTable

Post by DrewEaston »

Product, version and build: PowerShell Studio 2019, 5.6.167 (and earlier versions)
32 or 64 bit version of product: 64bit
Operating system: Windows 7 Home Premium and Windows 10 Enterprise
32 or 64 bit OS: both 64bit

Hi all,

When I first saw this happening I had NO idea what was causing it.
Now that I have written a PSF expressly for the SAPIEN forums I believe that I know exactly what is causing this behavior.

If you use the SAPIEN ConvertTo-DataTable function, which is a GREAT piece of code that I love.
DO NOT set a breakpoint AT or BEFORE the function call where one of the following things is to be evaluated and displayed in the Watch panel:

Code: Select all

$dataToDisplayAsDataTable.GetType()
$dataToDisplayAsDataTable.Count
$dataToDisplayAsDataTable is the variable I used for the returned object in this script/form.

When you have this script/form loaded in PSS first set a breakpoint somewhere AFTER the ConvertTo-DataTable function is called.
Then after as many runs as you want to make through the program using the "Retry loading data" button, enter one or both of the two above things to the Watch panel.
(I did not check the DataTable object first to see whether it had a Count property before I added that to the Watch panel ;) )

Then hit the "Retry loading data" button again and see what happens. ;) :o :shock:

After that has gone south once in a Debug Run it can not get better by itself, you have to stop Debugging and remove these "bad guys" from the Watch panel then you CAN run Debug again without this problem.

While writing this test program I took a look at the DataTable object and found these two calls for showing the information that I was trying to get by using the .Count call above.

Code: Select all

$dataToDisplayAsDataTable.Rows.Count
$dataToDisplayAsDataTable.Columns.Count
If either or both of these are in the Watch panel, without ANY "Bad Guys", there will be NO problem Debugging.

I don't think that SAPIEN wants the Watch panel to be having this effect.
I do remeber seeing an article in the internet about property hijacking and I am pretty sure that is happening here.

I hope you can fix the Watch panel from having such a side effect when someone, like me, puts something in there that does NOT work but keeps it from breaking the object.
Attachments
Test-DGV-with-DataTable-In-Debug-Mode-with-Retry-Button.Export.ps1
(23.62 KiB) Downloaded 133 times
Test-DGV-with-DataTable-In-Debug-Mode-with-Retry-Button.psf
(37.88 KiB) Downloaded 140 times
User avatar
DrewEaston
Posts: 48
Last visit: Thu Mar 30, 2023 8:47 am

Re: Watch panel evaluation of the result from ConvertTo-DataTable breaks use of DataTable

Post by DrewEaston »

For anyone who is wondering what OK looks like and what broken looks like:

This is OK:
DataTable Display is as expected
DataTable Display is as expected
2019.08.19_DataTable_Display_OK.png (49.02 KiB) Viewed 3553 times

This is broken:
DataTable Display broken
DataTable Display broken
2019.08.19_DataTable_Display_Broken.png (33.68 KiB) Viewed 3553 times
User avatar
brittneyr
Site Admin
Posts: 1655
Last visit: Thu Mar 28, 2024 3:14 pm
Answers: 39
Been upvoted: 30 times

Re: Watch panel evaluation of the result from ConvertTo-DataTable breaks use of DataTable

Post by brittneyr »

I see the problem, but this is a PowerShell issue.
I am able to replicate the behavior in an exported ps1 as well as a modified exported script that includes $dataToDisplayAsDataTable.Count in a messagebox in the ISE.

The following blog article provides more specifics regarding property hijacking:
https://www.sapien.com/blog/2019/04/29/ ... hijacking/
Brittney
SAPIEN Technologies, Inc.
User avatar
DrewEaston
Posts: 48
Last visit: Thu Mar 30, 2023 8:47 am

Re: Watch panel evaluation of the result from ConvertTo-DataTable breaks use of DataTable

Post by DrewEaston »

Hi Brittney,

OK, are you trying to say that there is nothing SAPIEN can do about this?

If so then this is just informational for those who also experience this effect.
User avatar
brittneyr
Site Admin
Posts: 1655
Last visit: Thu Mar 28, 2024 3:14 pm
Answers: 39
Been upvoted: 30 times

Re: Watch panel evaluation of the result from ConvertTo-DataTable breaks use of DataTable

Post by brittneyr »

I don't believe there is anything we can do to prevent this behavior as it's a fault with PowerShell itself, but I will investigate some more and let you know.
Brittney
SAPIEN Technologies, Inc.
User avatar
DrewEaston
Posts: 48
Last visit: Thu Mar 30, 2023 8:47 am

Re: Watch panel evaluation of the result from ConvertTo-DataTable breaks use of DataTable

Post by DrewEaston »

I do hope you can find something.
It is a bummer that even .GetType() causes this to happen.
If that would work without breaking anything then you guys/gals could do something.
I hope you have more tricks up your sleeves. :)
User avatar
Alexander Riedel
Posts: 8479
Last visit: Thu Mar 28, 2024 9:29 am
Answers: 19
Been upvoted: 37 times

Re: Watch panel evaluation of the result from ConvertTo-DataTable breaks use of DataTable

Post by Alexander Riedel »

Since I am the one working on the debugger bits, I think I should jump in here.
Basically the intention of the Watch panel was to observe specific variables and their properties while stopped at a breakpoint.
It was never intended to execute code in the sense of a function call. So that part is relatively easy to fix.
We can just prevent anything that is a function call in the watch panel to be executed.

That will however not solve the underlying PowerShell property hijacking problem. Someone can type that call in the debug console,
which was specifically meant to execute code while stopped ta a breakpoint, and cause the same effect.

As Brittney has already mentioned, you can do the same with simple messagebox line in any script.

I'll leave it up to everyone else to decide if that is a PowerShell bug (Don't expect Microsoft to fix property hijacking) or a simple case of Garbage-in, Garbage-Out.
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
DrewEaston
Posts: 48
Last visit: Thu Mar 30, 2023 8:47 am

Re: Watch panel evaluation of the result from ConvertTo-DataTable breaks use of DataTable

Post by DrewEaston »

Hi Alex,

Good to hear from you again.

I only put the $dataTableObject.Count into the Watch panel because I was having this display problem when I was Debugging a script.
I was NOT having the problem all the time.
I did NOT know that it would cause that problem.
Now I know the real cause and I will of course avoid that in the future.

I had read about the property hijacking problem earlier and only came to the conclusion that this was more than likely the cause when I was putting together a test script to upload into this forum. As I was working out the exact message that I wanted to post I discovered, even for myself, that it was breaking the DataTable ONLY when I had some specific things in the Watch panel. As I stated above if the values for the $dataTableObject.Rows.Count and $dataTableObject.Column.Count are entered into the Watch panel then nothing goes wrong. SO I will be using these values when I want to see, in the Watch panel, if the resulting DataTable is what I was expecting.

Today I tried to use the Variables panel to see if it would react differently than the Watch panel. As you probably already know that did NOT help at all and also caused the same effect. So it is very important that anybody who uses PSS Debugging to know if they are using DataTables that they have the risk here of Messing up their debug run accidently by "looking" at these values in debug mode.

I had also tried to find ANY reference about this behavior before I opened the topic.

I was also hoping that SAPIEN could do something, but sadly this is a Microsoft PowerShell general problem/bug.

So I hope this post will help others avoid the long search/trial and error that I went through if they are having the same problem.

I myself now judge the property hijacking as a BIG bug. I also understand Microsofts problem if they want to change this behavior, it has wide ranging effects in other shortcut programming style, as describe in the SAPIEN link above. ;)
This topic is 4 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.