Page 1 of 3

Script doesn't work when compiled to .exe

Posted: Fri Jan 05, 2018 8:58 am
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*" }

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

Posted: Fri Jan 05, 2018 9:13 am
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*" }

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

Posted: Fri Jan 05, 2018 9:29 am
by Chris_Ediger
Tried that, same behavior.

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

Posted: Fri Jan 05, 2018 11:10 am
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.

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

Posted: Fri Jan 05, 2018 11:21 am
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.

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

Posted: Fri Jan 05, 2018 11:25 am
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?

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

Posted: Fri Jan 05, 2018 11:40 am
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.

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

Posted: Fri Jan 05, 2018 11:41 am
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 3975 times

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

Posted: Fri Jan 05, 2018 11:59 am
by jvierra
Are you sure you are compiling exactly the same way?

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

Posted: Fri Jan 05, 2018 12:25 pm
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.