Script doesn't work when compiled to .exe

Ask your Windows 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.
User avatar
Chris.Ediger
Posts: 26
Joined: Tue Oct 24, 2017 9:33 am

Script doesn't work when compiled to .exe

Post by Chris.Ediger » Fri Jan 05, 2018 9:58 am

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 » Fri Jan 05, 2018 10:13 am

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: 26
Joined: Tue Oct 24, 2017 9:33 am

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

Post by Chris.Ediger » Fri Jan 05, 2018 10:29 am

Tried that, same behavior.

cody m

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

Post by cody m » Fri Jan 05, 2018 12:10 pm

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: 13999
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Fri Jan 05, 2018 12:21 pm

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: 26
Joined: Tue Oct 24, 2017 9:33 am

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

Post by Chris.Ediger » Fri Jan 05, 2018 12:25 pm

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: 26
Joined: Tue Oct 24, 2017 9:33 am

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

Post by Chris.Ediger » Fri Jan 05, 2018 12:40 pm

jvierra wrote:
Fri Jan 05, 2018 12:21 pm
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: 13999
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Fri Jan 05, 2018 12:41 pm

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 1895 times
Attachments
Test-AdResults.psf
(17.64 KiB) Downloaded 38 times

jvierra
Posts: 13999
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Fri Jan 05, 2018 12:59 pm

Are you sure you are compiling exactly the same way?

User avatar
Chris.Ediger
Posts: 26
Joined: Tue Oct 24, 2017 9:33 am

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

Post by Chris.Ediger » Fri Jan 05, 2018 1:25 pm

jvierra wrote:
Fri Jan 05, 2018 12:59 pm
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.

Locked