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.
Forms, text boxes and get-gpo
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.
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.
Re: Forms, text boxes and get-gpo
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,
There is no normal reason for what you are seeing,
Re: Forms, text boxes and get-gpo
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.
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.
- [void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
- [void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
- $FileName = "c:\Temp\log.csv"; $Look_XML = "Files.xml"; $Searchterm = "exception.sites"
- $Form = New-Object System.Windows.Forms.Form
- $Form.width = 410; $Form.height = 250
- $TxtBox = new-object System.Windows.Forms.TextBox
- $TxtBox.Location = new-object System.Drawing.Size(5, 45)
- $TxtBox.Size = new-object System.Drawing.Size(380, 50)
- $form.Controls.Add($TxtBox)
- $OKButton = new-object System.Windows.Forms.Button
- $OKButton.Location = new-object System.Drawing.Size(5, 10)
- $OKButton.Size = new-object System.Drawing.Size(105, 35)
- $OKButton.Text = "OK"
- $OKButton.Add_Click({
- $TxtBox.Text = "Gathering GPO's"
- Write-Host "Gathering GPO's"
- $GPO = Get-GPO -Id "{71da5601-498e-4e3e-86cf-0f35d63bf322}"
- $TxtBox.Text = "Searching for $Searchterm"
- Write-Host "Searching for $Searchterm"
- foreach ($Policy in $GPO)
- {
- $GPOID = $Policy.Id; $GPODom = $Policy.DomainName; $GPODisp = $Policy.DisplayName
- $TxtBox.Text = "Checking $GPODisp"
- Write-Host "Checking $GPODisp"
- $xmlpaths = Get-ChildItem -Path "\\$($GPODom)\SYSVOL\$($GPODom)\Policies\{$($GPOID)}\*" -Include @($Look_XML) -Recurse -Force | % { $_.fullname }
- }
- })
- $form.Controls.Add($OKButton)
- # Activate the form
- $Form.Add_Shown({ $Form.Activate() })
- [void]$Form.ShowDialog()
Re: Forms, text boxes and get-gpo
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.
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.
Re: Forms, text boxes and get-gpo
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.
$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.
Re: Forms, text boxes and get-gpo
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.
Re: Forms, text boxes and get-gpo
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.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.
Re: Forms, text boxes and get-gpo
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 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.
Re: Forms, text boxes and get-gpo
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.
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()
Re: Forms, text boxes and get-gpo
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.
- [void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
- [void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
- #set up the form
- $Form = New-Object System.Windows.Forms.Form
- $Form.width = 450
- $Form.height = 410
- $Form.Text = "Search Group Policies"
- $Font = New-Object System.Drawing.Font("Times New Roman", 10)
- $Form.Font = $Font
- #build cancel button
- $CancelButton = new-object System.Windows.Forms.Button
- $CancelButton.Location = new-object System.Drawing.Size(305, 230)
- $CancelButton.Size = new-object System.Drawing.Size(105, 35)
- $CancelButton.Text = "Cancel"
- $CancelButton.Add_Click({ $Form.Close() })
- #create your checkboxes
- $checkbox1 = new-object System.Windows.Forms.checkbox
- $checkbox1.Location = new-object System.Drawing.Size(20, 30)
- $checkbox1.Size = new-object System.Drawing.Size(100, 25)
- $checkbox1.Text = "Registry"
- $checkbox1.Checked = $false
- $checkbox2 = new-object System.Windows.Forms.checkbox
- $checkbox2.Location = new-object System.Drawing.Size(20, 55)
- $checkbox2.Size = new-object System.Drawing.Size(100, 25)
- $checkbox2.Text = "Files"
- $checkbox2.Checked = $false
- $checkbox3 = new-object System.Windows.Forms.checkbox
- $checkbox3.Location = new-object System.Drawing.Size(20, 80)
- $checkbox3.Size = new-object System.Drawing.Size(100, 25)
- $checkbox3.Text = "Groups"
- $checkbox3.Checked = $false
- $checkbox4 = new-object System.Windows.Forms.checkbox
- $checkbox4.Location = new-object System.Drawing.Size(20, 105)
- $checkbox4.Size = new-object System.Drawing.Size(100, 25)
- $checkbox4.Text = "Services"
- $checkbox4.Checked = $false
- $checkbox5 = new-object System.Windows.Forms.checkbox
- $checkbox5.Location = new-object System.Drawing.Size(20, 130)
- $checkbox5.Size = new-object System.Drawing.Size(100, 25)
- $checkbox5.Text = "Folders"
- $checkbox5.Checked = $false
- $checkbox6 = new-object System.Windows.Forms.checkbox
- $checkbox6.Location = new-object System.Drawing.Size(20, 155)
- $checkbox6.Size = new-object System.Drawing.Size(100, 25)
- $checkbox6.Text = "Drives"
- $checkbox6.Checked = $false
- $checkbox7 = new-object System.Windows.Forms.checkbox
- $checkbox7.Location = new-object System.Drawing.Size(20, 180)
- $checkbox7.Size = new-object System.Drawing.Size(100, 25)
- $checkbox7.Text = "Shortcuts"
- $checkbox7.Checked = $false
- $checkbox8 = new-object System.Windows.Forms.checkbox
- $checkbox8.Location = new-object System.Drawing.Size(20, 205)
- $checkbox8.Size = new-object System.Drawing.Size(130, 25)
- $checkbox8.Text = "FolderOptions"
- $checkbox8.Checked = $false
- $checkbox9 = new-object System.Windows.Forms.checkbox
- $checkbox9.Location = new-object System.Drawing.Size(20, 230)
- $checkbox9.Size = new-object System.Drawing.Size(130, 25)
- $checkbox9.Text = "PowerOptions"
- $checkbox9.Checked = $false
- $checkbox10 = new-object System.Windows.Forms.checkbox
- $checkbox10.Location = new-object System.Drawing.Size(20, 255)
- $checkbox10.Size = new-object System.Drawing.Size(100, 25)
- $checkbox10.Text = "ScheduledTasks"
- $checkbox10.Checked = $false
- #create some labels
- $Label1 = new-object System.Windows.Forms.Label
- $Label1.Location = new-object System.Drawing.Size(140, 30)
- $Label1.Size = new-object System.Drawing.Size(250, 25)
- $Label1.Text = " <-- Select XML files to search."
- $Label2 = new-object System.Windows.Forms.Label
- $Label2.Location = new-object System.Drawing.Size(160, 65)
- $Label2.Size = new-object System.Drawing.Size(250, 25)
- $Label2.Text = "Enter search term below:"
- $Label3 = new-object System.Windows.Forms.Label
- $Label3.Location = new-object System.Drawing.Size(160, 125)
- $Label3.Size = new-object System.Drawing.Size(350, 25)
- $Label3.Text = "Policies will be recorded to:"
- #create some text boxes
- $TxtBox = new-object System.Windows.Forms.TextBox
- $TxtBox.Font = New-Object System.Drawing.Font("Times New Roman", 9)
- $TxtBox.Location = new-object System.Drawing.Size(160, 90)
- $TxtBox.Size = new-object System.Drawing.Size(250, 25)
- $TxtBox.add_Leave({ $Searchterm = $TxtBox.Text })
- $TxtBox1 = new-object System.Windows.Forms.TextBox
- $TxtBox1.Font = New-Object System.Drawing.Font("Times New Roman", 9)
- $TxtBox1.Location = new-object System.Drawing.Size(160, 155)
- $TxtBox1.Size = new-object System.Drawing.Size(250, 50)
- $TxtBox1.Text = "c:\temp\GP_Search.CSV"
- $TxtBox1.add_Leave({ $FileName = $TxtBox1.Text })
- $TxtBox1.WordWrap = $true
- $TxtBox1.Multiline = $true
- $TxtBox2 = new-object System.Windows.Forms.TextBox
- $TxtBox2.Font = New-Object System.Drawing.Font("Times New Roman", 9)
- $TxtBox2.Location = new-object System.Drawing.Size(20, 290)
- $TxtBox2.Size = new-object System.Drawing.Size(390, 50)
- $TxtBox2.Text = "Script Output"
- $TxtBox2.WordWrap = $true
- $TxtBox2.Multiline = $true
- #build an OK button
- $OKButton = new-object System.Windows.Forms.Button
- $OKButton.Location = new-object System.Drawing.Size(180, 230)
- $OKButton.Size = new-object System.Drawing.Size(105, 35)
- $OKButton.Text = "OK"
- #get to work
- $OKButton.Add_Click({
- $Look_XML = @()
- If ($checkbox1.Checked) { $Look_XML += "Registry.xml" }
- If ($checkbox2.Checked) { $Look_XML += "Files.xml" }
- If ($checkbox3.Checked) { $Look_XML += "Groups.xml" }
- If ($checkbox4.Checked) { $Look_XML += "Services.xml" }
- If ($checkbox5.Checked) { $Look_XML += "Folders.xml" }
- If ($checkbox6.Checked) { $Look_XML += "Drives.xml" }
- If ($checkbox7.Checked) { $Look_XML += "Shortcuts.xml" }
- If ($checkbox8.Checked) { $Look_XML += "FolderOptions.xml" }
- If ($checkbox9.Checked) { $Look_XML += "PowerOptions.xml" }
- If ($checkbox10.Checked) { $Look_XML += "ScheduledTasks.xml" }
- $Searchterm = $TxtBox1.Text
- $FileName = $TxtBox1.Text
- If (Test-Path -Path $FileName) { Remove-Item $FileName }
- If ($Look_XML.Count -eq 0)
- {
- $TxtBox.Font = New-Object System.Drawing.Font("Times New Roman", 10)
- $TxtBox.ForeColor = 'RED'
- $TxtBox.Text = "!!!SELECTION REQUIRED!!!"
- }
- If ($Searchterm -eq $null)
- {
- $TxtBox.Font = New-Object System.Drawing.Font("Times New Roman", 10)
- $TxtBox.ForeColor = 'RED'
- $TxtBox.Text = "!!!ENTRY REQUIRED!!!"}
- Else
- {
- $TxtBox2.Text = "Gathering GPO's"
- $GPO = Get-GPO -Id "{71da5601-498e-4e3e-86cf-0f35d63bf322}"
- $TxtBox2.Text = "Searching for $Searchterm"
- foreach ($Policy in $GPO)
- {
- $TxtBox2.Text = "next"
- Write-Host "3"
- $GPOID = $Policy.Id; $GPODom = $Policy.DomainName; $GPODisp = $Policy.DisplayName
- $TxtBox2.Text = "Checking $GPODisp"
- $xmlpaths = Get-ChildItem -Path "\\$($GPODom)\SYSVOL\$($GPODom)\Policies\{$($GPOID)}\*" -Include @($Look_XML) -Recurse -Force | % { $_.fullname }
- foreach ($xmlfile in $xmlpaths)
- {
- $Searching = Get-Content $xmlfile -OutBuffer 1000
- foreach ($Line in $Searching)
- {
- if ($Line -like "*$Searchterm*")
- {
- Write-Host "Located $Searchterm in $GPODisp"
- Write-Host ""
- $TxtBox2.Text = "Located $Searchterm in $GPODisp"
- $objResult = [PSCustomObject]@{
- GPOName = $GPODisp
- TargetPath = $xmlfile
- Entry = $Line
- }
- $aResults += $objResult
- }
- }
- }
- }
- $aResults | Export-CSV $FileName
- Invoke-Item $FileName
- }
- })
- # add controls
- $form.Controls.Add($CancelButton)
- $Form.Controls.Add($checkbox1)
- $Form.Controls.Add($checkbox2)
- $Form.Controls.Add($checkbox3)
- $Form.Controls.Add($checkbox4)
- $Form.Controls.Add($checkbox5)
- $Form.Controls.Add($checkbox6)
- $Form.Controls.Add($checkbox7)
- $Form.Controls.Add($checkbox8)
- $Form.Controls.Add($checkbox9)
- $Form.Controls.Add($checkbox10)
- $form.Controls.Add($Label1)
- $form.Controls.Add($Label2)
- $form.Controls.Add($Label3)
- $form.Controls.Add($TxtBox)
- $form.Controls.Add($TxtBox1)
- $form.Controls.Add($TxtBox2)
- $form.Controls.Add($OKButton)
- #activate the form
- $Form.Add_Shown({ $Form.Activate() })
- [void]$Form.ShowDialog()