Dot Loaded File Compiler Error at Runtime

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 6 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

Dot Loaded File Compiler Error at Runtime

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

Create the following code in a .ps1

Code: Select all

function Get-RandomPassword {
	
	param
	(
		[Parameter(Mandatory = $false)]
		[ValidateRange(3, 256)]
		[Alias('L')]
		[int]$Length = 15
	)
	
	$pswd = $null
	$alphabet = $null
	
	for ([int]$a = 35; $a –le 126; $a++) {
		$alphabet += , [char][byte]$a
	}
	
	for ($loop = 1; $loop –le $length; $loop++) {
		$pswd += ($alphabet | Get-Random)
	}
	return $pswd
}
In a new .Ps1 dot load the file as shown (use your own path):

Code: Select all

. "c:\Data\PowerShell\test\Get-RandomPassword.ps1"

Get-RandomPassword -Length 30
Compile the code: In this instance the following settings were used:
  • 64 bit code for PowerShell v5
    Resolve and include external scripts
When the compiled code is run the following error is produced:

Code: Select all

[5CG6301YZ5][21/09/2017 09:49:21]PS:27 C: ..\x64 ► .\RandomPassword.exe
Line 29: At line:28 char:24
+     for ([int]$a = 35; $a ?le 126; $a++) {
+                           ~~~
Unexpected token '?le' in expression or statement.

At line:28 char:31
+     for ([int]$a = 35; $a ?le 126; $a++) {
+                                  ~
Missing closing ')' after expression in 'for' statement.

At line:15 char:29
+ function Get-RandomPassword {
+                             ~
Missing closing '}' in statement block or type definition.

At line:28 char:37
+     for ([int]$a = 35; $a ?le 126; $a++) {
+                                        ~
Unexpected token ')' in expression or statement.

At line:32 char:24
+     for ($loop = 1; $loop ?le $length; $loop++) {
+                           ~~~
Unexpected token '?le' in expression or statement.

At line:32 char:35
+     for ($loop = 1; $loop ?le $length; $loop++) {
+                                      ~
Missing closing ')' after expression in 'for' statement.

At line:32 char:44
+     for ($loop = 1; $loop ?le $length; $loop++) {
+                                               ~
Unexpected token ')' in expression or statement.

At line:36 char:1
+ }
+ ~
Unexpected token '}' in expression or statement.
However, if you embed the code in the source file and compile, it runs normally.

This same error occurs with other pieces of dot loaded code, not just the one shown here. But not all dot loaded code is affected, most compiles and runs correctly.

I also tried a combination of 32 bit code; PowerShell 3; and Windows script engine settings; all with the same result.

Thanks
G
cody m

Re: Dot Loaded File Compiler Error at Runtime

Post by cody m »

I tired to replicate your error on a couple machines here and was unable to reproduce the error that you are having. Can you do me a favor and just run the code through PowerShell Studio, rather than packaging it up and running the exe to see if it will run that way and if it doesn't can you copy and paste the error message here?
cody m

Re: Dot Loaded File Compiler Error at Runtime

Post by cody m »

Also could you post screen shots of the settings you are using. And are you Dot source file in Unicode.
User avatar
gareth.jacobs
Posts: 35
Last visit: Fri Apr 05, 2024 3:12 am

Re: Dot Loaded File Compiler Error at Runtime

Post by gareth.jacobs »

Your 2nd post was the clue.

The files (in fact all my files) are UTF8 by default.

I changed them to ASCII (Windows-1252); saved and recompiled and the problem went away.

I have never changed to default file format before now and, as I mentioned, this isn't a problem for scripts that are not dot sourced.

How can I make ASCII the default format for new files?

Thanks
G
cody m

Re: Dot Loaded File Compiler Error at Runtime

Post by cody m »

You're welcome, if you run into any other problems feel free to let us know.
This topic is 6 years and 6 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.