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

Forms, text boxes and get-gpo

Post by Tnt48185 » Thu May 09, 2019 11:45 pm

Anyone have any idea? I create a form, add a text box, change the text, run test$=get-gpo -all, and I can no longer change the value of said text box unless I use a pause command afterwards.

Ex.
$TxtBox2.Text = "Gathering GPO's"
$GPO = Get-GPO -Id "{71da5601-498e-4e3e-86cf-0f35d63bf322}"
$TxtBox2.Text = "Done”

The first edit of the text box works. The second does not unless I use a “pause” after the get-gpo.
For any iterations of $txtbox2.text= after this I have to pause for the textbox on the form to be updated.

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

Re: Forms, text boxes and get-gpo

Post by jvierra » Fri May 10, 2019 5:46 am

Get-Gpo loads an object. It will take a bit of time on large GPOs. It will have no effect on the form. You may have a corrupt file or a corrupt PS1 file.

There is no normal reason for what you are seeing,

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 7:25 am

Hi Jvierra and thank you for your response.

I rewrote the script as compact as possible where the issue still occurs. Would you mind having a look? You would have to replace the gpo ID but otherwise you should see where the textbox does not update but write-host shows progress.
  1. [void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
  2.  
  3.     [void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
  4.  
  5.  
  6.  
  7. $FileName = "c:\Temp\log.csv"; $Look_XML = "Files.xml"; $Searchterm = "exception.sites"
  8.  
  9. $Form = New-Object System.Windows.Forms.Form
  10.  
  11. $Form.width = 410;  $Form.height = 250
  12.  
  13. $TxtBox = new-object System.Windows.Forms.TextBox
  14.  
  15. $TxtBox.Location = new-object System.Drawing.Size(5, 45)
  16.  
  17. $TxtBox.Size = new-object System.Drawing.Size(380, 50)
  18.  
  19. $form.Controls.Add($TxtBox)
  20.  
  21.  
  22.  
  23. $OKButton = new-object System.Windows.Forms.Button
  24.  
  25. $OKButton.Location = new-object System.Drawing.Size(5, 10)
  26.  
  27. $OKButton.Size = new-object System.Drawing.Size(105, 35)
  28.  
  29. $OKButton.Text = "OK"
  30.  
  31. $OKButton.Add_Click({
  32.  
  33.        $TxtBox.Text = "Gathering GPO's"
  34.  
  35.        Write-Host "Gathering GPO's"
  36.  
  37.        $GPO = Get-GPO -Id "{71da5601-498e-4e3e-86cf-0f35d63bf322}"
  38.  
  39.        $TxtBox.Text = "Searching for $Searchterm"
  40.  
  41.        Write-Host "Searching for $Searchterm"
  42.  
  43.        foreach ($Policy in $GPO)
  44.  
  45.        {
  46.  
  47.              $GPOID = $Policy.Id; $GPODom = $Policy.DomainName; $GPODisp = $Policy.DisplayName
  48.  
  49.              $TxtBox.Text = "Checking $GPODisp"
  50.  
  51.              Write-Host "Checking $GPODisp"
  52.  
  53.              $xmlpaths = Get-ChildItem -Path "\\$($GPODom)\SYSVOL\$($GPODom)\Policies\{$($GPOID)}\*" -Include @($Look_XML) -Recurse -Force | % { $_.fullname }
  54.  
  55.        }
  56.  
  57. })
  58.  
  59. $form.Controls.Add($OKButton)
  60.  
  61.      
  62.  
  63. # Activate the form
  64.  
  65. $Form.Add_Shown({ $Form.Activate() })
  66.  
  67. [void]$Form.ShowDialog()

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

Re: Forms, text boxes and get-gpo

Post by jvierra » Fri May 10, 2019 7:53 am

I looked at your code. There are many undefined variables and some syntax that will never do anything.

Write the button click code at a PS prompt and debug it until it gives you what you want. You will have to make it a function that does not depend on the form.

One item that is obvious. The following line will never return anything:

foreach ($Policy in $GPO) {

This syntax requires a collection. "$GPO" is not a collection.

Anther very bad habit is adding lines together with ";". This serves no purpose and makes the code hard to understand and debug.

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

Re: Forms, text boxes and get-gpo

Post by jvierra » Fri May 10, 2019 7:57 am

The following line will never return anything. It is just bad code.

$xmlpaths = Get-ChildItem -Path "\\$($GPODom)\SYSVOL\$($GPODom)\Policies\{$($GPOID)}\*" -Include @($Look_XML) -Recurse -Force | % { $_.fullname }

Its purpose makes no sense and any result will never be available.

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 8:15 am

Thanks for your help! You might have missed where I said I shortened the script to only what was needed to reproduce the error. In my script, get-gpo -id is switched to get-gpo -all which creates the array. Further, $look_xml is an array of file names and populates $xmlpaths just fine. This is actually faster than filtering the array one entry at a time. It returns matching file names from all group policies. Bad code or not, everything works fine except the assignment of the txtbox.text. I’d love it if someone could tell me why the textbox does not update after the get-gpo command is run. I have walked through the button click and sent working code (you just need to enter a valid gpo ID). The issue occurs after get-gpo when the textbox stops updating but the script continues as apparent by the write-host output.

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 8:19 am

jvierra wrote:
Fri May 10, 2019 7:57 am
The following line will never return anything. It is just bad code.

$xmlpaths = Get-ChildItem -Path "\\$($GPODom)\SYSVOL\$($GPODom)\Policies\{$($GPOID)}\*" -Include @($Look_XML) -Recurse -Force | % { $_.fullname }

Its purpose makes no sense and any result will never be available.
You are incorrect. Even in current context it works without being an array but was designed as an array because it will be used that way. This is pseudo code for brevity.

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 8:21 am

jvierra wrote:
Fri May 10, 2019 7:53 am
I looked at your code. There are many undefined variables and some syntax that will never do anything.

Write the button click code at a PS prompt and debug it until it gives you what you want. You will have to make it a function that does not depend on the form.

One item that is obvious. The following line will never return anything:

foreach ($Policy in $GPO) {

This syntax requires a collection. "$GPO" is not a collection.

Anther very bad habit is adding lines together with ";". This serves no purpose and makes the code hard to understand and debug.
In my original code it was s function. The same issue occurred. I have since taken the function out for simplicity’s sake and still experience the same issue.

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

Re: Forms, text boxes and get-gpo

Post by jvierra » Fri May 10, 2019 8:32 am

The code is bad no matter what you do with it. Creating a function or a standalone script will hel you debug the code. There are a dozen of more serious syntax, logic and tecnicak errors.

Here is an example of how to use build a form and how to use your code in an event. I will leave it up to you to fix it to get what you want. I cannot determine the purpose for what you are doing from the questions or from the code.

Code: Select all

Add-Type   -AssemblyName  System.Windows.Forms

$FileName = 'c:\Temp\log.csv'
$Look_XML = 'Files.xml'
$Searchterm = 'exception.sites'

$OKButton_Click = {
    $id = '71da5601-498e-4e3e-86cf-0f35d63bf322'
    $TxtBox.Lines += 'Gathering GPOs'
    if($GPO = Get-GPO -Id $id -ErrorAction 0){
        $GPOID = $GPO.Id
        $GPODom = $GPO.DomainName
        $GPODisp = $GPO.DisplayName
        $TxtBox.Lines += "Checking $GPODisp"
        
        #the following lines make no sense.
        $path = '\\$($GPODom)\SYSVOL\$($GPODom)\Policies\{$($GPOID)}\*'
        $xmlpaths = Get-ChildItem $path -Include @($Look_XML) -Recurse -Force
        
    }else{
        $TxtBox.Lines += 'GPO not found'
    }
}
$Form = New-Object System.Windows.Forms.Form
$Form.Size = '410,300'
$Form.StartPosition = 'CenterScreen'

$TxtBox = new-object System.Windows.Forms.TextBox
$form.Controls.Add($TxtBox)
$TxtBox.Location = '5, 35'
$TxtBox.Size = '230, 200'
$TxtBox.Multiline = $true


$OKButton = new-object System.Windows.Forms.Button
$form.Controls.Add($OKButton)
$OKButton.Location = '5, 5'
#$OKButton.Size = '105, 35'
$OKButton.Text = 'OK'
$OKButton.Add_Click($OKButton_Click)

# Activate the form
#$Form.Add_Shown({ $Form.Activate() })
[void]$Form.ShowDialog()

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 8:43 am

Maybe you just need the whole script to get past the fact that I’m simply asking about populating a text box. There is absolutely no need for a function. That is a preference. See below.
  1.  
  2.  
  3. [void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
  4.  
  5. [void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
  6.  
  7.  
  8.  
  9. #set up the form
  10.  
  11. $Form = New-Object System.Windows.Forms.Form
  12.  
  13. $Form.width = 450
  14.  
  15. $Form.height = 410
  16.  
  17. $Form.Text = "Search Group Policies"
  18.  
  19. $Font = New-Object System.Drawing.Font("Times New Roman", 10)
  20.  
  21. $Form.Font = $Font
  22.  
  23. #build cancel button
  24.  
  25. $CancelButton = new-object System.Windows.Forms.Button
  26.  
  27. $CancelButton.Location = new-object System.Drawing.Size(305, 230)
  28.  
  29. $CancelButton.Size = new-object System.Drawing.Size(105, 35)
  30.  
  31. $CancelButton.Text = "Cancel"
  32.  
  33. $CancelButton.Add_Click({ $Form.Close() })
  34.  
  35. #create your checkboxes
  36.  
  37. $checkbox1 = new-object System.Windows.Forms.checkbox
  38.  
  39. $checkbox1.Location = new-object System.Drawing.Size(20, 30)
  40.  
  41. $checkbox1.Size = new-object System.Drawing.Size(100, 25)
  42.  
  43. $checkbox1.Text = "Registry"
  44.  
  45. $checkbox1.Checked = $false
  46.  
  47. $checkbox2 = new-object System.Windows.Forms.checkbox
  48.  
  49. $checkbox2.Location = new-object System.Drawing.Size(20, 55)
  50.  
  51. $checkbox2.Size = new-object System.Drawing.Size(100, 25)
  52.  
  53. $checkbox2.Text = "Files"
  54.  
  55. $checkbox2.Checked = $false
  56.  
  57. $checkbox3 = new-object System.Windows.Forms.checkbox
  58.  
  59. $checkbox3.Location = new-object System.Drawing.Size(20, 80)
  60.  
  61. $checkbox3.Size = new-object System.Drawing.Size(100, 25)
  62.  
  63. $checkbox3.Text = "Groups"
  64.  
  65. $checkbox3.Checked = $false
  66.  
  67. $checkbox4 = new-object System.Windows.Forms.checkbox
  68.  
  69. $checkbox4.Location = new-object System.Drawing.Size(20, 105)
  70.  
  71. $checkbox4.Size = new-object System.Drawing.Size(100, 25)
  72.  
  73. $checkbox4.Text = "Services"
  74.  
  75. $checkbox4.Checked = $false
  76.  
  77. $checkbox5 = new-object System.Windows.Forms.checkbox
  78.  
  79. $checkbox5.Location = new-object System.Drawing.Size(20, 130)
  80.  
  81. $checkbox5.Size = new-object System.Drawing.Size(100, 25)
  82.  
  83. $checkbox5.Text = "Folders"
  84.  
  85. $checkbox5.Checked = $false
  86.  
  87. $checkbox6 = new-object System.Windows.Forms.checkbox
  88.  
  89. $checkbox6.Location = new-object System.Drawing.Size(20, 155)
  90.  
  91. $checkbox6.Size = new-object System.Drawing.Size(100, 25)
  92.  
  93. $checkbox6.Text = "Drives"
  94.  
  95. $checkbox6.Checked = $false
  96.  
  97. $checkbox7 = new-object System.Windows.Forms.checkbox
  98.  
  99. $checkbox7.Location = new-object System.Drawing.Size(20, 180)
  100.  
  101. $checkbox7.Size = new-object System.Drawing.Size(100, 25)
  102.  
  103. $checkbox7.Text = "Shortcuts"
  104.  
  105. $checkbox7.Checked = $false
  106.  
  107. $checkbox8 = new-object System.Windows.Forms.checkbox
  108.  
  109. $checkbox8.Location = new-object System.Drawing.Size(20, 205)
  110.  
  111. $checkbox8.Size = new-object System.Drawing.Size(130, 25)
  112.  
  113. $checkbox8.Text = "FolderOptions"
  114.  
  115. $checkbox8.Checked = $false
  116.  
  117. $checkbox9 = new-object System.Windows.Forms.checkbox
  118.  
  119. $checkbox9.Location = new-object System.Drawing.Size(20, 230)
  120.  
  121. $checkbox9.Size = new-object System.Drawing.Size(130, 25)
  122.  
  123. $checkbox9.Text = "PowerOptions"
  124.  
  125. $checkbox9.Checked = $false
  126.  
  127. $checkbox10 = new-object System.Windows.Forms.checkbox
  128.  
  129. $checkbox10.Location = new-object System.Drawing.Size(20, 255)
  130.  
  131. $checkbox10.Size = new-object System.Drawing.Size(100, 25)
  132.  
  133. $checkbox10.Text = "ScheduledTasks"
  134.  
  135. $checkbox10.Checked = $false
  136.  
  137. #create some labels
  138.  
  139. $Label1 = new-object System.Windows.Forms.Label
  140.  
  141. $Label1.Location = new-object System.Drawing.Size(140, 30)
  142.  
  143. $Label1.Size = new-object System.Drawing.Size(250, 25)
  144.  
  145. $Label1.Text = " <-- Select XML files to search."
  146.  
  147. $Label2 = new-object System.Windows.Forms.Label
  148.  
  149. $Label2.Location = new-object System.Drawing.Size(160, 65)
  150.  
  151. $Label2.Size = new-object System.Drawing.Size(250, 25)
  152.  
  153. $Label2.Text = "Enter search term below:"
  154.  
  155. $Label3 = new-object System.Windows.Forms.Label
  156.  
  157. $Label3.Location = new-object System.Drawing.Size(160, 125)
  158.  
  159. $Label3.Size = new-object System.Drawing.Size(350, 25)
  160.  
  161. $Label3.Text = "Policies will be recorded to:"
  162.  
  163. #create some text boxes
  164.  
  165. $TxtBox = new-object System.Windows.Forms.TextBox
  166.  
  167. $TxtBox.Font = New-Object System.Drawing.Font("Times New Roman", 9)
  168.  
  169. $TxtBox.Location = new-object System.Drawing.Size(160, 90)
  170.  
  171. $TxtBox.Size = new-object System.Drawing.Size(250, 25)
  172.  
  173. $TxtBox.add_Leave({ $Searchterm = $TxtBox.Text })
  174.  
  175. $TxtBox1 = new-object System.Windows.Forms.TextBox
  176.  
  177. $TxtBox1.Font = New-Object System.Drawing.Font("Times New Roman", 9)
  178.  
  179. $TxtBox1.Location = new-object System.Drawing.Size(160, 155)
  180.  
  181. $TxtBox1.Size = new-object System.Drawing.Size(250, 50)
  182.  
  183. $TxtBox1.Text = "c:\temp\GP_Search.CSV"
  184.  
  185. $TxtBox1.add_Leave({ $FileName = $TxtBox1.Text })
  186.  
  187. $TxtBox1.WordWrap = $true
  188.  
  189. $TxtBox1.Multiline = $true
  190.  
  191. $TxtBox2 = new-object System.Windows.Forms.TextBox
  192.  
  193. $TxtBox2.Font = New-Object System.Drawing.Font("Times New Roman", 9)
  194.  
  195. $TxtBox2.Location = new-object System.Drawing.Size(20, 290)
  196.  
  197. $TxtBox2.Size = new-object System.Drawing.Size(390, 50)
  198.  
  199. $TxtBox2.Text = "Script Output"
  200.  
  201. $TxtBox2.WordWrap = $true
  202.  
  203. $TxtBox2.Multiline = $true
  204.  
  205. #build an OK button
  206.  
  207. $OKButton = new-object System.Windows.Forms.Button
  208.  
  209. $OKButton.Location = new-object System.Drawing.Size(180, 230)
  210.  
  211. $OKButton.Size = new-object System.Drawing.Size(105, 35)
  212.  
  213. $OKButton.Text = "OK"
  214.  
  215. #get to work
  216.  
  217. $OKButton.Add_Click({
  218.  
  219.              $Look_XML = @()
  220.  
  221.              If ($checkbox1.Checked) { $Look_XML += "Registry.xml" }
  222.  
  223.              If ($checkbox2.Checked) { $Look_XML += "Files.xml" }
  224.  
  225.              If ($checkbox3.Checked) { $Look_XML += "Groups.xml" }
  226.  
  227.              If ($checkbox4.Checked) { $Look_XML += "Services.xml" }
  228.  
  229.              If ($checkbox5.Checked) { $Look_XML += "Folders.xml" }
  230.  
  231.              If ($checkbox6.Checked) { $Look_XML += "Drives.xml" }
  232.  
  233.              If ($checkbox7.Checked) { $Look_XML += "Shortcuts.xml" }
  234.  
  235.              If ($checkbox8.Checked) { $Look_XML += "FolderOptions.xml" }
  236.  
  237.              If ($checkbox9.Checked) { $Look_XML += "PowerOptions.xml" }
  238.  
  239.              If ($checkbox10.Checked) { $Look_XML += "ScheduledTasks.xml" }
  240.  
  241.              $Searchterm = $TxtBox1.Text
  242.  
  243.              $FileName = $TxtBox1.Text
  244.  
  245.              If (Test-Path -Path $FileName) { Remove-Item $FileName }
  246.  
  247.              If ($Look_XML.Count -eq 0)
  248.  
  249.              {
  250.  
  251.                     $TxtBox.Font = New-Object System.Drawing.Font("Times New Roman", 10)
  252.  
  253.                     $TxtBox.ForeColor = 'RED'
  254.  
  255.                     $TxtBox.Text = "!!!SELECTION REQUIRED!!!"
  256.  
  257.              }
  258.  
  259.              If ($Searchterm -eq $null)
  260.  
  261.              {
  262.  
  263.                     $TxtBox.Font = New-Object System.Drawing.Font("Times New Roman", 10)
  264.  
  265.                     $TxtBox.ForeColor = 'RED'
  266.  
  267.                     $TxtBox.Text = "!!!ENTRY REQUIRED!!!"}
  268.  
  269.              Else
  270.  
  271.              {
  272.  
  273.                     $TxtBox2.Text = "Gathering GPO's"
  274.  
  275.                     $GPO = Get-GPO -Id "{71da5601-498e-4e3e-86cf-0f35d63bf322}"
  276.  
  277.                     $TxtBox2.Text = "Searching for $Searchterm"
  278.  
  279.                     foreach ($Policy in $GPO)
  280.  
  281.                     {
  282.  
  283.                            $TxtBox2.Text = "next"
  284.  
  285.                            Write-Host "3"
  286.  
  287.                            $GPOID = $Policy.Id; $GPODom = $Policy.DomainName; $GPODisp = $Policy.DisplayName
  288.  
  289.                            $TxtBox2.Text = "Checking $GPODisp"
  290.  
  291.                            $xmlpaths = Get-ChildItem -Path "\\$($GPODom)\SYSVOL\$($GPODom)\Policies\{$($GPOID)}\*" -Include @($Look_XML) -Recurse -Force | % { $_.fullname }
  292.  
  293.                            foreach ($xmlfile in $xmlpaths)
  294.  
  295.                            {
  296.  
  297.                                  $Searching = Get-Content $xmlfile -OutBuffer 1000
  298.  
  299.                                  foreach ($Line in $Searching)
  300.  
  301.                                  {
  302.  
  303.                                         if ($Line -like "*$Searchterm*")
  304.  
  305.                                         {
  306.  
  307.                                                Write-Host "Located $Searchterm in $GPODisp"
  308.  
  309.                                                Write-Host ""
  310.  
  311.                                                $TxtBox2.Text = "Located $Searchterm in $GPODisp"
  312.  
  313.                                                $objResult = [PSCustomObject]@{
  314.  
  315.                                                      GPOName    = $GPODisp
  316.  
  317.                                                      TargetPath = $xmlfile
  318.  
  319.                                                      Entry     = $Line
  320.  
  321.                                                      }
  322.  
  323.                                                $aResults += $objResult
  324.  
  325.                                         }
  326.  
  327.                                  }
  328.  
  329.                            }
  330.  
  331.                     }
  332.  
  333.                     $aResults | Export-CSV $FileName
  334.  
  335.                     Invoke-Item $FileName
  336.  
  337.              }
  338.  
  339. })
  340.  
  341.  
  342.  
  343. # add controls
  344.  
  345. $form.Controls.Add($CancelButton)
  346.  
  347. $Form.Controls.Add($checkbox1)
  348.  
  349. $Form.Controls.Add($checkbox2)
  350.  
  351. $Form.Controls.Add($checkbox3)
  352.  
  353. $Form.Controls.Add($checkbox4)
  354.  
  355. $Form.Controls.Add($checkbox5)
  356.  
  357. $Form.Controls.Add($checkbox6)
  358.  
  359. $Form.Controls.Add($checkbox7)
  360.  
  361. $Form.Controls.Add($checkbox8)
  362.  
  363. $Form.Controls.Add($checkbox9)
  364.  
  365. $Form.Controls.Add($checkbox10)
  366.  
  367. $form.Controls.Add($Label1)
  368.  
  369. $form.Controls.Add($Label2)
  370.  
  371. $form.Controls.Add($Label3)
  372.  
  373. $form.Controls.Add($TxtBox)
  374.  
  375. $form.Controls.Add($TxtBox1)
  376.  
  377. $form.Controls.Add($TxtBox2)
  378.  
  379. $form.Controls.Add($OKButton)
  380.  
  381. #activate the form
  382.  
  383. $Form.Add_Shown({ $Form.Activate() })
  384.  
  385. [void]$Form.ShowDialog()
  386.  
  387.  

Locked