0

I have a PowerShell script that has a GUI, which has below where users enter the credentials, and it is redirected to the next page if he is a valid member. Below is the function to validate the users. This works as expected when run via ISE, but when I convert it exe and run it, I get "Exception calling "FindOne" with "0" argument(s) " network path not found while authenticating via LDAP" error

I have an exe which I created a year back with the same function for authentication, which works, hence I am wondering what the issue is now, is it the way I am converting the script to exe?

$loginButton.Add_Click({ try { [System.Windows.Forms.MessageBox]::Show("Login Clicked", "Step 1")

 # Step 2 - current user $currentUser = "$env:USERDOMAIN\$env:USERNAME" [System.Windows.Forms.MessageBox]::Show("Current User: $currentUser", "Step 2") # Step 3 - load assembly (already loaded, but just in case) Add-Type -AssemblyName System.DirectoryServices.AccountManagement [System.Windows.Forms.MessageBox]::Show("Loaded AccountManagement assembly", "Step 3") # Step 4 - create context $context = New-Object System.DirectoryServices.AccountManagement.PrincipalContext('Domain') [System.Windows.Forms.MessageBox]::Show("Created PrincipalContext for domain: $domainName", "Step 4") # Step 5 - get user principal $userPrincipal = [System.DirectoryServices.AccountManagement.UserPrincipal]::FindByIdentity($context, $env:USERNAME) if (-not $userPrincipal) { [System.Windows.Forms.MessageBox]::Show("User not found in AD", "Error", "OK", "Error") return } [System.Windows.Forms.MessageBox]::Show("UserPrincipal found: $($userPrincipal.Name)", "Step 5") # Step 6 - check group membership $isInGroup = $false $groups = $userPrincipal.GetAuthorizationGroups() foreach ($group in $groups) { if ($group.Name -ieq $requiredGroup) { $isInGroup = $true break } } # Step 7 - result if ($isInGroup) { [System.Windows.Forms.MessageBox]::Show("Access granted!", "Success") $loginPanel.Visible = $false $LetterRemoval.Visible = $true } else { [System.Windows.Forms.MessageBox]::Show("Access denied. Not in group '$requiredGroup'.", "Unauthorized", "OK", "Error") } } catch { $errorMessage = $_.Exception.Message if ($_.Exception.InnerException) { $errorMessage += "`nInner Exception: " + $_.Exception.InnerException.Message } $errorMessage += "`nStack Trace:`n" + $_.Exception.StackTrace [System.Windows.Forms.MessageBox]::Show("Fatal error:`n$errorMessage", "Error", "OK", "Error") } 

}) I am using Invoke-PS2EXE -InputFile "E:\Auth_test.ps1" -OutputFile "E:\Auth_test.exe" for conversion

Fails at Step 4

1
  • 1
    I'm not sure I understand the entire premise of this script. What's the point of a custom auth dialog – isn't the user already authenticated to Windows? (If they weren't already authenticated, then they wouldn't be able to make a directory search in the first place.) Not to mention it going kinda contrary to all the efforts to teach users not to enter their credentials into shady-looking forms... Commented Sep 2 at 15:29

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.