debugging hang on AD directory seacher

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
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 4 years and 10 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
Lembasts
Posts: 406
Last visit: Mon Apr 15, 2024 3:12 pm
Has voted: 1 time
Been upvoted: 1 time

debugging hang on AD directory seacher

Post by Lembasts »

Greetings,

I have a line of code that goes something like:
$search = new-object System.directoryservices.directorysearcher
When I use the debugger and go through my code line by line, I get to this line and press F11 and it just hangs.
Any ideas why?
Thanks
David
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: debugging hang on AD directory seacher

Post by jvierra »

Doesn't happen for me.

Here is how we usually do this

Code: Select all

$filter = '(objectclass=computer)'
$searcher = [adsisearcher]$filter
$searcher.FindAll()
User avatar
Lembasts
Posts: 406
Last visit: Mon Apr 15, 2024 3:12 pm
Has voted: 1 time
Been upvoted: 1 time

Re: debugging hang on AD directory seacher

Post by Lembasts »

Yeh - its strange.
If I just highlight the code and select 'run selection in console' it works fine.
If I compile it into an exe and run it it doesnt work and neither does debugging.
User avatar
Lembasts
Posts: 406
Last visit: Mon Apr 15, 2024 3:12 pm
Has voted: 1 time
Been upvoted: 1 time

Re: debugging hang on AD directory seacher

Post by Lembasts »

I thought it might have something to do with :
$ExecutionContext.SessionState.LanguageMode
but when I run that in the console it says FullLanguage.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: debugging hang on AD directory seacher

Post by jvierra »

If it works in the console then you need to contact Sapien support for assistance with this. I cannot reproduce your issue with the latest version of PSS.
User avatar
Lembasts
Posts: 406
Last visit: Mon Apr 15, 2024 3:12 pm
Has voted: 1 time
Been upvoted: 1 time

Re: debugging hang on AD directory seacher

Post by Lembasts »

Looks like support cant work it out either.
Must be some weird group policy hardening setting.
I would have thought that good old adsisearcher was so benign...

Anyways - I've rewritten the AD bit to use adfind (from joeware) and all is well..
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: debugging hang on AD directory seacher

Post by jvierra »

There is no GP setting that can do this. It is more likely that your workstation is not joined correctly to the domain or that you have a damaged Net Framework installation.

I have used this code on Windows from XP to the current versions under PS2 through PS5 with WinForms. It has never hung. I can also run it under PSS in the debugger with no issues. If the domain cannot be reached then the line will time out and throw an exception.

I can reproduce this but only in the debugger. Due to how debuggers work this is code that will hang when a breakpoint is set on the either of the two lines noted. Just don't try to breakpoint in this location and the code work fine. These lines work in any PS forms script with no issue.

Code: Select all

Add-Type -AssemblyName System.Windows.Forms

$form = New-Object System.Windows.Forms.Form
$button = New-Object System.Windows.Forms.Button
$form.Controls.Add($button)
$form.StartPosition = 'CenterScreen'
$form.Text = 'Basic Form Demo'

$button.Text = 'Run ADSI'
$button.add_Click({
    Try{
        $filter = '(objectclass=computer)'
        # a breakpoint on either of the following two lines will
        # cause the code to hang in the debugger.
        $searcher = [adsisearcher]$filter
        $searcher.FindAll()
    }
    Catch{
        [System.Windows.Forms.MessageBox]::Show($_)
    }
})
$form.ShowDialog()
This is not about PSS but is how the Windows debugger works when in a single threaded environment.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: debugging hang on AD directory seacher

Post by jvierra »

The exact same code in a PSF does not hang in the debugger. This is likely due to setting up the form more completely so that it is aware of the forms "state".

$form1.add_Load($Form_StateCorrection_Load)

The attached PSF does not hang in the debugger.
Attachments
Test-ADSIHanger.psf
(23.19 KiB) Downloaded 108 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: debugging hang on AD directory seacher

Post by jvierra »

Yes. Adding the form state correction code allows the code to work correctly in the debugger.

Code: Select all

Add-Type -AssemblyName System.Windows.Forms

$form = New-Object System.Windows.Forms.Form
$button = New-Object System.Windows.Forms.Button
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
$Form_StateCorrection_Load = {
    #Correct the initial state of the form to prevent the .Net maximized form issue
    $form.WindowState = $InitialFormWindowState
}

$form.Controls.Add($button)
$form.StartPosition = 'CenterScreen'
$form.Text = 'Basic Form Demo'

$button.Text = 'Run ADSI'
$button.add_Click({
    Try{
        $filter = '(objectclass=computer)'
        # a breakpoint on either of the following two lines will
        # cause the code to hang in the debugger.
        $searcher = [adsisearcher]$filter
        $searcher.FindAll()
    }
    Catch{
        [System.Windows.Forms.MessageBox]::Show($_)
    }
})

$InitialFormWindowState = $form.WindowState
$form.add_Load($Form_StateCorrection_Load)

$form.ShowDialog()
Attachments
Test-ADSI.ps1
(978 Bytes) Downloaded 115 times
This topic is 4 years and 10 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