Compiled Code Object Reference Error

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 6 years and 8 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
gareth.jacobs
Posts: 35
Last visit: Fri Apr 05, 2024 3:12 am

Compiled Code Object Reference Error

Post by gareth.jacobs »

Product, version and build: PowerShell Studio 2017 v5.4.143
32 or 64 bit version of product: 64
Operating system: Windows 10 Build 14393
32 or 64 bit OS: 64

Hi

Using the following simple code:

Code: Select all

param
(
	[Alias('h')]
	$help,
	[Parameter(ParameterSetName = 'add')]
	$add,
	$remove
)
Write-Host "Hello World"
$ExitCode = 0
If you compile it (32 and 64) and run the executable with -? as an option, it outputs the following error (unreadable characters not showing):

Error executing script.
Object reference not set to an instance of an object.
PoshExeHostCmdV3
at (Object )
at ..(String , String[] )
at ..(String[] )
Int32 (System.Object)


If you remove the parameter set information line and recompile, the error goes away.
The same error also occurs if you have "[CmdletBinding()]" set

This problem is causing an issue in a more complex script I have compiled, where command line arguments are being ignored.
The error only occurs in compiled code; scripts run normally.

Thanks
G
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Compiled Code Object Reference Error

Post by davidc »

Thank you for reporting the issue. I was able to reproduce the error and started to investigate the cause. I will post an update as soon as I have more information.
David
SAPIEN Technologies, Inc.
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Compiled Code Object Reference Error

Post by davidc »

It turns out that PowerShell engine itself is throwing an exception. Apparently, it doesn't like the -? parameter when you have a parameter set. I'm not sure there is much we can do about this since it is the PowerShell engine itself that is causing the error. But if we find something, we will let you know.
David
SAPIEN Technologies, Inc.
User avatar
gareth.jacobs
Posts: 35
Last visit: Fri Apr 05, 2024 3:12 am

Re: Compiled Code Object Reference Error

Post by gareth.jacobs »

Interesting. When you use the -? parameter on a ps1 script it outputs your specified parameters plus [<CommonParameters>].

Ignoring -? for now, what about the command line parameters being ignored in the compiled code, but work from a script?
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Compiled Code Object Reference Error

Post by davidc »

They are invoked differently. The packager runs the script from memory where as a normal script runs directly from the file, which PowerShell internally is handling differently. I wish I had a better answer, but unfortunately, I don't have intimate knowledge of PowerShell inner workings.
David
SAPIEN Technologies, Inc.
User avatar
gareth.jacobs
Posts: 35
Last visit: Fri Apr 05, 2024 3:12 am

Re: Compiled Code Object Reference Error

Post by gareth.jacobs »

Thanks David.

I have found the answer here: https://www.sapien.com/blog/2015/12/07/ ... table-file

It seems that parsing parameters to a compiled executable is entirely different and the traditional method used by scripts.

I have been compiling PowerShell code for a number of years now, but never had to pass parameters before. Exactly how this is done when talking about a compiled EXE is not clear at all and it took some digging to find the answer - some more work by Sapien needs to be done in this area to better inform users.

I have now written some code that captures parameters, whether the script is compiled or not.

As a side thought: Have Sapien though about writing a true PowerShell compiler for this product, which would alleviate most of the issues around script wrapping?

Regards
G
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Compiled Code Object Reference Error

Post by davidc »

Unfortunately, we can't compile the script into a true binary executable. The PowerShell engine still requires us to pass the script in order to run it.
David
SAPIEN Technologies, Inc.
This topic is 6 years and 8 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.