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.
Locked
User avatar
Lembasts
Posts: 206
Joined: Thu Mar 15, 2012 9:47 am

debugging hang on AD directory seacher

Post by Lembasts » Thu May 23, 2019 9:24 pm

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

User avatar
jvierra
Posts: 13809
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: debugging hang on AD directory seacher

Post by jvierra » Thu May 23, 2019 9:39 pm

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: 206
Joined: Thu Mar 15, 2012 9:47 am

Re: debugging hang on AD directory seacher

Post by Lembasts » Thu May 23, 2019 9:45 pm

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: 206
Joined: Thu Mar 15, 2012 9:47 am

Re: debugging hang on AD directory seacher

Post by Lembasts » Thu May 23, 2019 9:50 pm

I thought it might have something to do with :
$ExecutionContext.SessionState.LanguageMode
but when I run that in the console it says FullLanguage.

User avatar
jvierra
Posts: 13809
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: debugging hang on AD directory seacher

Post by jvierra » Thu May 23, 2019 10:06 pm

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: 206
Joined: Thu Mar 15, 2012 9:47 am

Re: debugging hang on AD directory seacher

Post by Lembasts » Sun Jun 02, 2019 10:01 pm

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..

User avatar
jvierra
Posts: 13809
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: debugging hang on AD directory seacher

Post by jvierra » Sun Jun 02, 2019 11:03 pm

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.

User avatar
jvierra
Posts: 13809
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: debugging hang on AD directory seacher

Post by jvierra » Sun Jun 02, 2019 11:10 pm

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 22 times

User avatar
jvierra
Posts: 13809
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: debugging hang on AD directory seacher

Post by jvierra » Sun Jun 02, 2019 11:16 pm

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 23 times

Locked