Script doesn't work when compiled to .exe

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

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 2 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.
Locked
User avatar
Chris_Ediger
Posts: 45
Last visit: Tue May 09, 2023 8:35 pm

Script doesn't work when compiled to .exe

Post by Chris_Ediger »

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

I have a script that works great when I run it from within Studio but when I compile it into an .exe it fails.

Error initializing default drive: 'Unable to contact the server. This may be because this server does not exist, it is currently down, or it does not have the Active Directory Web Services running.'.
Get-ADUser : Unable to contact the server. This may be because this server does not exist, it is currently down, or it does not have the Active Directory Web Services running.
At line:2533 char:21
+ ... l:ADUsers = Get-ADUser -Filter * -Properties DisplayName,Distinguishe ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:) [Get-ADUser], ADServerDownException
+ FullyQualifiedErrorId : ActiveDirectoryServer:0,Microsoft.ActiveDirectory.Management.Commands.GetADUser

First line of script:

$CreateNewUser_Load={
#TODO: Initialize Form Controls here
Import-Module ActiveDirectory
$Global:ADUsers = Get-ADUser -Filter * -Properties DisplayName,DistinguishedName,HomeDirectory,Department,MemberOf,SAMAccountName,CN,EmailAddress -SearchBase "OU=<USEROU>,DC=<MYDOMAIN>,DC=com" | Where-Object { $_.DisplayName -notlike "zz*" }
cody m

Re: Script doesn't work when compiled to .exe

Post by cody m »

Try setting Get-ADUser's -server parameter to the Active Directory server that you would like to access manually.

Code: Select all

$CreateNewUser_Load={
#TODO: Initialize Form Controls here
Import-Module ActiveDirectory
$Global:ADUsers = Get-ADUser -Filter * -Properties DisplayName,DistinguishedName,HomeDirectory,Department,MemberOf,SAMAccountName,CN,EmailAddress -SearchBase "OU=<USEROU>,DC=<MYDOMAIN>,DC=com"  -Server <YourServerName> | Where-Object { $_.DisplayName -notlike "zz*" }
User avatar
Chris_Ediger
Posts: 45
Last visit: Tue May 09, 2023 8:35 pm

Re: Script doesn't work when compiled to .exe

Post by Chris_Ediger »

Tried that, same behavior.
cody m

Re: Script doesn't work when compiled to .exe

Post by cody m »

I moved the topic to Scripting answers in to get more user's feedback. I also talked to one of our engineers here, could you check to make sure that the exe uses the same system settings as it does in PSS.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Script doesn't work when compiled to .exe

Post by jvierra »

Perhaps your firewall is blocking the connection. Be sure the EXE is allowed outbound connections. To quickly test disable the firewall and test.
User avatar
Chris_Ediger
Posts: 45
Last visit: Tue May 09, 2023 8:35 pm

Re: Script doesn't work when compiled to .exe

Post by Chris_Ediger »

Thank you.
When you say check the settings are you talking about the script packager settings? The Script Engine settings for example?
User avatar
Chris_Ediger
Posts: 45
Last visit: Tue May 09, 2023 8:35 pm

Re: Script doesn't work when compiled to .exe

Post by Chris_Ediger »

jvierra wrote: Fri Jan 05, 2018 11:21 am Perhaps your firewall is blocking the connection. Be sure the EXE is allowed outbound connections. To quickly test disable the firewall and test.
Here is something I probably should have shared. I have other scripts that use Get-ADUser in the same way and they work just fine after I compiled them. I just tried recompiling them and now I get the same error as with this current script! The previously compiled .exe scripts still work fine on my computer.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Script doesn't work when compiled to .exe

Post by jvierra »

Firewall settings. When I run an eXE that wants to connect to the network I get a message asking if I want to allow the EXE to access the net. If I allow the FW to allow the EXE then it works.

In most domains the FW is set by GP and cannot be altered. Check with you net admins.

Attached is a simple PSF that will run on any system with RSAT. When converted to an EXE the firewall on most systems will block it.

On systems that alert automatically I get this:
fwalert.png
fwalert.png (21.95 KiB) Viewed 3950 times
Attachments
Test-AdResults.psf
(17.64 KiB) Downloaded 130 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Script doesn't work when compiled to .exe

Post by jvierra »

Are you sure you are compiling exactly the same way?
User avatar
Chris_Ediger
Posts: 45
Last visit: Tue May 09, 2023 8:35 pm

Re: Script doesn't work when compiled to .exe

Post by Chris_Ediger »

jvierra wrote: Fri Jan 05, 2018 11:59 am Are you sure you are compiling exactly the same way?
Is there a way to see how I compiled previous scripts? I've been using PSS for only a few months now and I'm not totally sure how to manage the settings on the packager. I'm fairly certain I did V5 Host (Windows Application) or V5 Host (Windows). However I did it originally, neither of the options work. I even tried Windows Forms.
This topic is 6 years and 2 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.
Locked