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.
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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Forms, text boxes and get-gpo

Post by jvierra »

I downloaded the TXT file but I still cannot tell what the issues is. What, exactly, is not happening and what line is it associated with?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Forms, text boxes and get-gpo

Post by jvierra »

The TXT file is no where like the original code you posted. It does something completely different. Without a better statement of what is not happening it is not possible to guess your issues or cause.

Also there is no way I can run the code as it is dependent on the GPO names you have on your system. I would have to rewrite the code to work on my test system.

When you get into this kind of difficulty you will likely need to use the debugger to find the errors.
Tnt48185
Posts: 17
Last visit: Mon May 13, 2019 7:38 am

Re: Forms, text boxes and get-gpo

Post by Tnt48185 »

The code you submitted needs a process defined for foreach-object. Get-gpo -all | foreach-object {} does not work.

As for my crappy code (lol):
Txtbox2.text = “Gathering GPO’s”
Updates the text box.
Everything after that works until:
Txtbox2.text = “Checking $GPODisp for $Searchterm”
That does NOT update the text box. (Doesn’t matter if variables are removed)
Everything after that works except for any txtbox.text lines.

The same is true if I use a function to update the text box. Once get-gpo runs, the text box will not update.
Tnt48185
Posts: 17
Last visit: Mon May 13, 2019 7:38 am

Re: Forms, text boxes and get-gpo

Post by Tnt48185 »

jvierra wrote: Fri May 10, 2019 9:52 am The TXT file is no where like the original code you posted. It does something completely different. Without a better statement of what is not happening it is not possible to guess your issues or cause.

Also there is no way I can run the code as it is dependent on the GPO names you have on your system. I would have to rewrite the code to work on my test system.

When you get into this kind of difficulty you will likely need to use the debugger to find the errors.

No, it does not do something completely different.
No, you do not have to “guess” anything. I have been explicit in what is not happening and what should be happening.
No, it is not dependent on GPO names. It “gets” the GPO names from your environment.
If you don’t know the answer it is ok, I don’t either.
Tnt48185
Posts: 17
Last visit: Mon May 13, 2019 7:38 am

Re: Forms, text boxes and get-gpo

Post by Tnt48185 »

Fixed the foreach-object issue I caused. I just had to keep { on the same line. That said, even using the function, the text box still does not update after get-gpo runs.
Tnt48185
Posts: 17
Last visit: Mon May 13, 2019 7:38 am

Re: Forms, text boxes and get-gpo

Post by Tnt48185 »

I should note, and maybe this will help, if I add a “pause” after the: txtbox2.text = “Checking.....
Line then the text box updates and I have to close the pause pop-up to continue the script.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Forms, text boxes and get-gpo

Post by jvierra »

Tnt48185 wrote: Fri May 10, 2019 9:57 am The code you submitted needs a process defined for foreach-object. Get-gpo -all | foreach-object {} does not work.

As for my crappy code (lol):
Txtbox2.text = “Gathering GPO’s”
Updates the text box.
Everything after that works until:
Txtbox2.text = “Checking $GPODisp for $Searchterm”
That does NOT update the text box. (Doesn’t matter if variables are removed)
Everything after that works except for any txtbox.text lines.

The same is true if I use a function to update the text box. Once get-gpo runs, the text box will not update.
The code I posted works correctly if you use the whole form I supplied.

My "foreach-Object" looks correct;

The following is completely correct code. It does NOT need any changes:

Code: Select all

   Get-GPO -All |
        ForEach-Object{
            logit "Checking $($_.DisplayName)"
            $path = '\\$($GPODom)\SYSVOL\$($_.DomainName)\Policies\{$($_.Id)}\*'
            $xmlpaths = Get-ChildItem $path -Include @($Look_XML) -Recurse -Force | select -expand Fullname    
    }
You have to tell us what line number is the one that is failing. Because your code is so hard to read I just don't feel like trying to decode it.

Also you can check this in the debugger. It is likely the line is either being overwritten or skipped if it is not being assigned.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Forms, text boxes and get-gpo

Post by jvierra »

Tnt48185 wrote: Fri May 10, 2019 10:01 am

No, it does not do something completely different.
No, you do not have to “guess” anything. I have been explicit in what is not happening and what should be happening.
No, it is not dependent on GPO names. It “gets” the GPO names from your environment.
If you don’t know the answer it is ok, I don’t either.
The XML files cannot be retrieved because no names will match on my system. This also causes errors.
With some fudging I can get the CSV file.

I still cannot tell what is missing in what textbox. The issue you report is a line that gets overwritten immediately at the top of the following loop. If you put a sleep command after the assignment you should see the text.

The multiline textbox needs to be assigned as I showed you in my code example:

$txtBox2.Lines += 'some text'

If you do it your way each line will overwrite the contents completely.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Forms, text boxes and get-gpo

Post by jvierra »

Tnt48185 wrote: Fri May 10, 2019 10:12 am I should note, and maybe this will help, if I add a “pause” after the: txtbox2.text = “Checking.....
Line then the text box updates and I have to close the pause pop-up to continue the script.
Which is why my code uses a multiline textbox and updates it correctly. The code I posted has everything you need.

With the "logit" function you can also use it from a function at a prompt and just alter it like this:

Code: Select all

function logit {
    Param ($msg)
    Write-Host $msg 
    #$TxtBox.Lines += $msg
    #$txtbox.Select($TxtBox.Text.Length, 0)
    #$txtbox.ScrollToCaret()
}
Now your messages will arrive at the console while you test the function. TO test output to controls hand th control to the function as a parameter. You can create a lone control to use in testing. You can check the contents of the control by inspecting the "Lines" property to see if it got all of the output required.
Tnt48185
Posts: 17
Last visit: Mon May 13, 2019 7:38 am

Re: Forms, text boxes and get-gpo

Post by Tnt48185 »

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