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
Tnt48185
Posts: 17
Last visit: Mon May 13, 2019 7:38 am

Forms, text boxes and get-gpo

Post by Tnt48185 »

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

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
Last visit: Mon May 13, 2019 7:38 am

Re: Forms, text boxes and get-gpo

Post by Tnt48185 »

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.     [void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
  3.  
  4. $FileName = "c:\Temp\log.csv"; $Look_XML = "Files.xml"; $Searchterm = "exception.sites"
  5. $Form = New-Object System.Windows.Forms.Form
  6. $Form.width = 410;  $Form.height = 250
  7. $TxtBox = new-object System.Windows.Forms.TextBox
  8. $TxtBox.Location = new-object System.Drawing.Size(5, 45)
  9. $TxtBox.Size = new-object System.Drawing.Size(380, 50)
  10. $form.Controls.Add($TxtBox)
  11.  
  12. $OKButton = new-object System.Windows.Forms.Button
  13. $OKButton.Location = new-object System.Drawing.Size(5, 10)
  14. $OKButton.Size = new-object System.Drawing.Size(105, 35)
  15. $OKButton.Text = "OK"
  16. $OKButton.Add_Click({
  17.        $TxtBox.Text = "Gathering GPO's"
  18.        Write-Host "Gathering GPO's"
  19.        $GPO = Get-GPO -Id "{71da5601-498e-4e3e-86cf-0f35d63bf322}"
  20.        $TxtBox.Text = "Searching for $Searchterm"
  21.        Write-Host "Searching for $Searchterm"
  22.        foreach ($Policy in $GPO)
  23.        {
  24.              $GPOID = $Policy.Id; $GPODom = $Policy.DomainName; $GPODisp = $Policy.DisplayName
  25.              $TxtBox.Text = "Checking $GPODisp"
  26.              Write-Host "Checking $GPODisp"
  27.              $xmlpaths = Get-ChildItem -Path "\\$($GPODom)\SYSVOL\$($GPODom)\Policies\{$($GPOID)}\*" -Include @($Look_XML) -Recurse -Force | % { $_.fullname }
  28.        }
  29. })
  30. $form.Controls.Add($OKButton)
  31.      
  32. # Activate the form
  33. $Form.Add_Shown({ $Form.Activate() })
  34. [void]$Form.ShowDialog()
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 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: 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 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
Last visit: Mon May 13, 2019 7:38 am

Re: Forms, text boxes and get-gpo

Post by Tnt48185 »

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
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 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
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 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: 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 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
Last visit: Mon May 13, 2019 7:38 am

Re: Forms, text boxes and get-gpo

Post by Tnt48185 »

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. [void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
  3. [void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
  4.  
  5. #set up the form
  6. $Form = New-Object System.Windows.Forms.Form
  7. $Form.width = 450
  8. $Form.height = 410
  9. $Form.Text = "Search Group Policies"
  10. $Font = New-Object System.Drawing.Font("Times New Roman", 10)
  11. $Form.Font = $Font
  12. #build cancel button
  13. $CancelButton = new-object System.Windows.Forms.Button
  14. $CancelButton.Location = new-object System.Drawing.Size(305, 230)
  15. $CancelButton.Size = new-object System.Drawing.Size(105, 35)
  16. $CancelButton.Text = "Cancel"
  17. $CancelButton.Add_Click({ $Form.Close() })
  18. #create your checkboxes
  19. $checkbox1 = new-object System.Windows.Forms.checkbox
  20. $checkbox1.Location = new-object System.Drawing.Size(20, 30)
  21. $checkbox1.Size = new-object System.Drawing.Size(100, 25)
  22. $checkbox1.Text = "Registry"
  23. $checkbox1.Checked = $false
  24. $checkbox2 = new-object System.Windows.Forms.checkbox
  25. $checkbox2.Location = new-object System.Drawing.Size(20, 55)
  26. $checkbox2.Size = new-object System.Drawing.Size(100, 25)
  27. $checkbox2.Text = "Files"
  28. $checkbox2.Checked = $false
  29. $checkbox3 = new-object System.Windows.Forms.checkbox
  30. $checkbox3.Location = new-object System.Drawing.Size(20, 80)
  31. $checkbox3.Size = new-object System.Drawing.Size(100, 25)
  32. $checkbox3.Text = "Groups"
  33. $checkbox3.Checked = $false
  34. $checkbox4 = new-object System.Windows.Forms.checkbox
  35. $checkbox4.Location = new-object System.Drawing.Size(20, 105)
  36. $checkbox4.Size = new-object System.Drawing.Size(100, 25)
  37. $checkbox4.Text = "Services"
  38. $checkbox4.Checked = $false
  39. $checkbox5 = new-object System.Windows.Forms.checkbox
  40. $checkbox5.Location = new-object System.Drawing.Size(20, 130)
  41. $checkbox5.Size = new-object System.Drawing.Size(100, 25)
  42. $checkbox5.Text = "Folders"
  43. $checkbox5.Checked = $false
  44. $checkbox6 = new-object System.Windows.Forms.checkbox
  45. $checkbox6.Location = new-object System.Drawing.Size(20, 155)
  46. $checkbox6.Size = new-object System.Drawing.Size(100, 25)
  47. $checkbox6.Text = "Drives"
  48. $checkbox6.Checked = $false
  49. $checkbox7 = new-object System.Windows.Forms.checkbox
  50. $checkbox7.Location = new-object System.Drawing.Size(20, 180)
  51. $checkbox7.Size = new-object System.Drawing.Size(100, 25)
  52. $checkbox7.Text = "Shortcuts"
  53. $checkbox7.Checked = $false
  54. $checkbox8 = new-object System.Windows.Forms.checkbox
  55. $checkbox8.Location = new-object System.Drawing.Size(20, 205)
  56. $checkbox8.Size = new-object System.Drawing.Size(130, 25)
  57. $checkbox8.Text = "FolderOptions"
  58. $checkbox8.Checked = $false
  59. $checkbox9 = new-object System.Windows.Forms.checkbox
  60. $checkbox9.Location = new-object System.Drawing.Size(20, 230)
  61. $checkbox9.Size = new-object System.Drawing.Size(130, 25)
  62. $checkbox9.Text = "PowerOptions"
  63. $checkbox9.Checked = $false
  64. $checkbox10 = new-object System.Windows.Forms.checkbox
  65. $checkbox10.Location = new-object System.Drawing.Size(20, 255)
  66. $checkbox10.Size = new-object System.Drawing.Size(100, 25)
  67. $checkbox10.Text = "ScheduledTasks"
  68. $checkbox10.Checked = $false
  69. #create some labels
  70. $Label1 = new-object System.Windows.Forms.Label
  71. $Label1.Location = new-object System.Drawing.Size(140, 30)
  72. $Label1.Size = new-object System.Drawing.Size(250, 25)
  73. $Label1.Text = " <-- Select XML files to search."
  74. $Label2 = new-object System.Windows.Forms.Label
  75. $Label2.Location = new-object System.Drawing.Size(160, 65)
  76. $Label2.Size = new-object System.Drawing.Size(250, 25)
  77. $Label2.Text = "Enter search term below:"
  78. $Label3 = new-object System.Windows.Forms.Label
  79. $Label3.Location = new-object System.Drawing.Size(160, 125)
  80. $Label3.Size = new-object System.Drawing.Size(350, 25)
  81. $Label3.Text = "Policies will be recorded to:"
  82. #create some text boxes
  83. $TxtBox = new-object System.Windows.Forms.TextBox
  84. $TxtBox.Font = New-Object System.Drawing.Font("Times New Roman", 9)
  85. $TxtBox.Location = new-object System.Drawing.Size(160, 90)
  86. $TxtBox.Size = new-object System.Drawing.Size(250, 25)
  87. $TxtBox.add_Leave({ $Searchterm = $TxtBox.Text })
  88. $TxtBox1 = new-object System.Windows.Forms.TextBox
  89. $TxtBox1.Font = New-Object System.Drawing.Font("Times New Roman", 9)
  90. $TxtBox1.Location = new-object System.Drawing.Size(160, 155)
  91. $TxtBox1.Size = new-object System.Drawing.Size(250, 50)
  92. $TxtBox1.Text = "c:\temp\GP_Search.CSV"
  93. $TxtBox1.add_Leave({ $FileName = $TxtBox1.Text })
  94. $TxtBox1.WordWrap = $true
  95. $TxtBox1.Multiline = $true
  96. $TxtBox2 = new-object System.Windows.Forms.TextBox
  97. $TxtBox2.Font = New-Object System.Drawing.Font("Times New Roman", 9)
  98. $TxtBox2.Location = new-object System.Drawing.Size(20, 290)
  99. $TxtBox2.Size = new-object System.Drawing.Size(390, 50)
  100. $TxtBox2.Text = "Script Output"
  101. $TxtBox2.WordWrap = $true
  102. $TxtBox2.Multiline = $true
  103. #build an OK button
  104. $OKButton = new-object System.Windows.Forms.Button
  105. $OKButton.Location = new-object System.Drawing.Size(180, 230)
  106. $OKButton.Size = new-object System.Drawing.Size(105, 35)
  107. $OKButton.Text = "OK"
  108. #get to work
  109. $OKButton.Add_Click({
  110.              $Look_XML = @()
  111.              If ($checkbox1.Checked) { $Look_XML += "Registry.xml" }
  112.              If ($checkbox2.Checked) { $Look_XML += "Files.xml" }
  113.              If ($checkbox3.Checked) { $Look_XML += "Groups.xml" }
  114.              If ($checkbox4.Checked) { $Look_XML += "Services.xml" }
  115.              If ($checkbox5.Checked) { $Look_XML += "Folders.xml" }
  116.              If ($checkbox6.Checked) { $Look_XML += "Drives.xml" }
  117.              If ($checkbox7.Checked) { $Look_XML += "Shortcuts.xml" }
  118.              If ($checkbox8.Checked) { $Look_XML += "FolderOptions.xml" }
  119.              If ($checkbox9.Checked) { $Look_XML += "PowerOptions.xml" }
  120.              If ($checkbox10.Checked) { $Look_XML += "ScheduledTasks.xml" }
  121.              $Searchterm = $TxtBox1.Text
  122.              $FileName = $TxtBox1.Text
  123.              If (Test-Path -Path $FileName) { Remove-Item $FileName }
  124.              If ($Look_XML.Count -eq 0)
  125.              {
  126.                     $TxtBox.Font = New-Object System.Drawing.Font("Times New Roman", 10)
  127.                     $TxtBox.ForeColor = 'RED'
  128.                     $TxtBox.Text = "!!!SELECTION REQUIRED!!!"
  129.              }
  130.              If ($Searchterm -eq $null)
  131.              {
  132.                     $TxtBox.Font = New-Object System.Drawing.Font("Times New Roman", 10)
  133.                     $TxtBox.ForeColor = 'RED'
  134.                     $TxtBox.Text = "!!!ENTRY REQUIRED!!!"}
  135.              Else
  136.              {
  137.                     $TxtBox2.Text = "Gathering GPO's"
  138.                     $GPO = Get-GPO -Id "{71da5601-498e-4e3e-86cf-0f35d63bf322}"
  139.                     $TxtBox2.Text = "Searching for $Searchterm"
  140.                     foreach ($Policy in $GPO)
  141.                     {
  142.                            $TxtBox2.Text = "next"
  143.                            Write-Host "3"
  144.                            $GPOID = $Policy.Id; $GPODom = $Policy.DomainName; $GPODisp = $Policy.DisplayName
  145.                            $TxtBox2.Text = "Checking $GPODisp"
  146.                            $xmlpaths = Get-ChildItem -Path "\\$($GPODom)\SYSVOL\$($GPODom)\Policies\{$($GPOID)}\*" -Include @($Look_XML) -Recurse -Force | % { $_.fullname }
  147.                            foreach ($xmlfile in $xmlpaths)
  148.                            {
  149.                                  $Searching = Get-Content $xmlfile -OutBuffer 1000
  150.                                  foreach ($Line in $Searching)
  151.                                  {
  152.                                         if ($Line -like "*$Searchterm*")
  153.                                         {
  154.                                                Write-Host "Located $Searchterm in $GPODisp"
  155.                                                Write-Host ""
  156.                                                $TxtBox2.Text = "Located $Searchterm in $GPODisp"
  157.                                                $objResult = [PSCustomObject]@{
  158.                                                      GPOName    = $GPODisp
  159.                                                      TargetPath = $xmlfile
  160.                                                      Entry     = $Line
  161.                                                      }
  162.                                                $aResults += $objResult
  163.                                         }
  164.                                  }
  165.                            }
  166.                     }
  167.                     $aResults | Export-CSV $FileName
  168.                     Invoke-Item $FileName
  169.              }
  170. })
  171.  
  172. # add controls
  173. $form.Controls.Add($CancelButton)
  174. $Form.Controls.Add($checkbox1)
  175. $Form.Controls.Add($checkbox2)
  176. $Form.Controls.Add($checkbox3)
  177. $Form.Controls.Add($checkbox4)
  178. $Form.Controls.Add($checkbox5)
  179. $Form.Controls.Add($checkbox6)
  180. $Form.Controls.Add($checkbox7)
  181. $Form.Controls.Add($checkbox8)
  182. $Form.Controls.Add($checkbox9)
  183. $Form.Controls.Add($checkbox10)
  184. $form.Controls.Add($Label1)
  185. $form.Controls.Add($Label2)
  186. $form.Controls.Add($Label3)
  187. $form.Controls.Add($TxtBox)
  188. $form.Controls.Add($TxtBox1)
  189. $form.Controls.Add($TxtBox2)
  190. $form.Controls.Add($OKButton)
  191. #activate the form
  192. $Form.Add_Shown({ $Form.Activate() })
  193. [void]$Form.ShowDialog()
  194.  
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