Forms, text boxes and get-gpo

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.
Tnt48185
Posts: 17
Joined: Fri May 10, 2019 6:28 am

Re: Forms, text boxes and get-gpo

Post by Tnt48185 » Fri May 10, 2019 10:51 am

Something a little closer to home that says basically the same thing: https://www.sapien.com/blog/2012/05/16/ ... ive-forms/

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

Re: Forms, text boxes and get-gpo

Post by jvierra » Fri May 10, 2019 11:00 am

Here is how to build your function so it can be easily tested and debugged in a console and then directly used in a form:

Code: Select all

$Console = $true
function logit {
    Param (
        $msg
    )
    if($Console){
        Write-Host $msg
    }else{
        $TxtBox.Lines += $msg
        $txtbox.Select($TxtBox.Text.Length, 0)
        $txtbox.ScrollToCaret()
    }
}

function Search-GpoXml{
    Param(
        [string]$SearchTerm,
        [array]$LookupXML
    )
    
    $allgpos = Get-GPO -All
    
    foreach ($gpo in $allgpos) {
        
        logit "Checking $($gpo.DisplayName) for $Searchterm"
        $path = "\\$($gpo.DomainName)\SYSVOL\$($gpo.DomainName)\Policies\{$($gpo.Id)}\*"
        $xmlpaths = Get-ChildItem $path  -Include $LookupXML -Recurse -Force | ForEach-Object { $_.fullname }
        foreach ($xmlfile in $xmlpaths) {
            logit "Parsing $xmlfile for $Searchterm"
            Start-Sleep 10
            $Searching = Get-Content $xmlfile -OutBuffer 1000
            foreach ($line in $Searching) {
                if ($line -like "*$Searchterm*") {
                    logit "Found in $GPODisp"
                    [PSCustomObject]@{
                        GPOName    = $GPO.DisplayName
                        TargetPath = $xmlfile
                        Entry      = $line
                    }
                }
            }
        }
    }
}

$aResults = Search-GpoXml -SearchTerm $searchterm -Look_XML $look_Xml

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

Re: Forms, text boxes and get-gpo

Post by jvierra » Fri May 10, 2019 11:22 am

Tnt48185 wrote:
Fri May 10, 2019 10:49 am
yeah... so, this pretty much sums up what I am seeing, why I am seeing it and what I need to do to fix it: https://stackoverflow.com/questions/352 ... uting-loop
No idea what you are referring to. Your issue is just one of coding and logic. Once you understand my code this will become obvious. The multiline textbox will update in real time using my code. Constantly rewriting the "Text" property will no work as you expect.

This has nothing to do with responsive forms. That is a completely different issue.

Adding my changes to your complete form displays all lines output into a scrollable textbox that tracks the last line added.

Locked