Ouputing ForEach loop to a $richTextBox

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 9 years and 6 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
User avatar
bscull
Posts: 5
Last visit: Wed Jul 29, 2015 3:59 pm

Ouputing ForEach loop to a $richTextBox

Post by bscull »

Hi

I'm a PowerShell newbie and need some help.

I'm in the middle of convert a nice PowerShell script that I found on the NET which 'finds lockout out locations in AD'. I have been tasked to make a GUI using PowerShell studio eval to give to our Helpdesk Dept.

http://gallery.technet.microsoft.com/sc ... ab#content

I have most of the output working apart from this last selection. I am outputting the PowerShell results to the rich text box using this syntax $richTextBox1.Appendtext(). So what I’m trying to do is output the User, DomianController, Eventid, LockedOutTimeStamp, Message and LockedOutLocation labels and information to a richtextbox so the Helpdesk users can see the result.

I hope this makes sense

Thanks!!!!!

PowerShell Code
Double-click the code block to select all.
Foreach($Event in $LockedOutEvents) 
       {             
           If($Event | Where {$_.Properties[2].value -match $UserInfo.SID.Value}) 
           {  
		$Event | Select-Object -Property @( 
              	@{Label = 'User';               Expression = {$_.Properties[0].Value}} 
                @{Label = 'DomainController';   Expression = {$_.MachineName}} 
                @{Label = 'EventId';            Expression = {$_.Id}} 
                @{Label = 'LockedOutTimeStamp'; Expression = {$_.TimeCreated}} 
                @{Label = 'Message';            Expression = {$_.Message -split "`r" | Select -First 1}} 
                @{Label = 'LockedOutLocation';  Expression = {$_.Properties[1].Value}} 
              ) 
            
            }#end ifevent 
		
       }#end foreach lockedout event
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Ouputing ForEach loop to a $richTextBox

Post by jvierra »

I do not see an use of appendText()
User avatar
bscull
Posts: 5
Last visit: Wed Jul 29, 2015 3:59 pm

Re: Ouputing ForEach loop to a $richTextBox

Post by bscull »

I tried placing the $richTextBox1.Appendtext() in a number of different places inside the loop but could not get it to output the correct data to the textbox inside my form

So I just attached the original code without me missing around with it.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Ouputing ForEach loop to a $richTextBox

Post by jvierra »

I looked at the code. It is bogus as it was written by someone who does not know PowerShell well. There is no available output.

I recommend posting to the author to get the code fixed so the results can be retained.

I used the code as is and it canwork but needs to befixed.

Here is how it works:
PowerShell Code
Double-click the code block to select all.
$text=Get-LockedOutLocation testuser11 |
                      Format-List|
                      out-String
$richtextbox1.AppendText($text)
User avatar
bscull
Posts: 5
Last visit: Wed Jul 29, 2015 3:59 pm

Re: Ouputing ForEach loop to a $richTextBox

Post by bscull »

Sorry I don't follow :?

the full code is below and it does give a output when I run it in a standard PowerShell window.

This is what the output looks like when I run it via the PowerShell console. It checks the event viewer on a DC and reports back each time a user has been locked out

The Output I need displaying to a $richTextBox on my form

User : test
DomainController : WIN-HVUFA7OGP6C.bscull.local
EventId : 4740
LockedOutTimeStamp : 03/09/2014 15:05:45
Message : A user account was locked out.
LockedOutLocation : WIN8

 

User : test
DomainController : WIN-HVUFA7OGP6C.bscull.local
EventId : 4740
LockedOutTimeStamp : 03/09/2014 14:52:39
Message : A user account was locked out.
LockedOutLocation : WIN8

 

User : test
DomainController : WIN-HVUFA7OGP6C.bscull.local
EventId : 4740
LockedOutTimeStamp : 01/09/2014 22:18:19
Message : A user account was locked out.
LockedOutLocation : WIN8




PowerShell Code
Double-click the code block to select all.
#Requires -Version 2.0 
Function Get-LockedOutLocation 
{ 
<# 
.SYNOPSIS 
    This function will locate the computer that processed a failed user logon attempt which caused the user account to become locked out. 
 
.DESCRIPTION 
    This function will locate the computer that processed a failed user logon attempt which caused the user account to become locked out.  
    The locked out location is found by querying the PDC Emulator for locked out events (4740).   
    The function will display the BadPasswordTime attribute on all of the domain controllers to add in further troubleshooting. 
 
.EXAMPLE 
    PS C:\>Get-LockedOutLocation -Identity Joe.Davis 
 
 
    This example will find the locked out location for Joe Davis. 
.NOTE 
    This function is only compatible with an environment where the domain controller with the PDCe role to be running Windows Server 2008 SP2 and up.   
    The script is also dependent the ActiveDirectory PowerShell module, which requires the AD Web services to be running on at least one domain controller. 
    Author:Jason Walker 
    Last Modified: 3/20/2013 
#> 
    [CmdletBinding()] 
 
    Param( 
      [Parameter(Mandatory=$True)] 
      [String]$Identity       
    ) 
 
    Begin 
    {  
        $DCCounter = 0  
        $LockedOutStats = @()    
                 
        Try 
        { 
            Import-Module ActiveDirectory -ErrorAction Stop 
        } 
        Catch 
        { 
           Write-Warning $_ 
           Break 
        } 
    }#end begin 
    Process 
    { 
         
        #Get all domain controllers in domain 
        $DomainControllers = Get-ADDomainController -Filter * 
        $PDCEmulator = ($DomainControllers | Where-Object {$_.OperationMasterRoles -contains "PDCEmulator"}) 
         
        Write-Verbose "Finding the domain controllers in the domain" 
        Foreach($DC in $DomainControllers) 
        { 
            $DCCounter++ 
            Write-Progress -Activity "Contacting DCs for lockout info" -Status "Querying $($DC.Hostname)" -PercentComplete (($DCCounter/$DomainControllers.Count) * 100) 
            Try 
            { 
                $UserInfo = Get-ADUser -Identity $Identity  -Server $DC.Hostname -Properties AccountLockoutTime,LastBadPasswordAttempt,BadPwdCount,LockedOut -ErrorAction Stop 
            } 
            Catch 
            { 
                Write-Warning $_ 
                Continue 
            } 
            If($UserInfo.LastBadPasswordAttempt) 
            {     
                $LockedOutStats += New-Object -TypeName PSObject -Property @{ 
                        Name                   = $UserInfo.SamAccountName 
                        SID                    = $UserInfo.SID.Value 
                        LockedOut              = $UserInfo.LockedOut 
                        BadPwdCount            = $UserInfo.BadPwdCount 
                        BadPasswordTime        = $UserInfo.BadPasswordTime             
                        DomainController       = $DC.Hostname 
                        AccountLockoutTime     = $UserInfo.AccountLockoutTime 
                        LastBadPasswordAttempt = ($UserInfo.LastBadPasswordAttempt).ToLocalTime() 
                    }           
            }#end if 
        }#end foreach DCs 
        $LockedOutStats | Format-Table -Property Name,LockedOut,DomainController,BadPwdCount,AccountLockoutTime,LastBadPasswordAttempt -AutoSize 
 
        #Get User Info 
        Try 
        {   
           Write-Verbose "Querying event log on $($PDCEmulator.HostName)" 
           $LockedOutEvents = Get-WinEvent -ComputerName $PDCEmulator.HostName -FilterHashtable @{LogName='Security';Id=4740} -ErrorAction Stop | Sort-Object -Property TimeCreated -Descending 
        } 
        Catch  
        {           
           Write-Warning $_ 
           Continue 
        }#end catch      
                                  
        Foreach($Event in $LockedOutEvents) 
        {             
           If($Event | Where {$_.Properties[2].value -match $UserInfo.SID.Value}) 
           {  
               
              $Event | Select-Object -Property @( 
                @{Label = 'User';               Expression = {$_.Properties[0].Value}} 
                @{Label = 'DomainController';   Expression = {$_.MachineName}} 
                @{Label = 'EventId';            Expression = {$_.Id}} 
                @{Label = 'LockedOutTimeStamp'; Expression = {$_.TimeCreated}} 
                @{Label = 'Message';            Expression = {$_.Message -split "`r" | Select -First 1}} 
                @{Label = 'LockedOutLocation';  Expression = {$_.Properties[1].Value}} 
              ) 
                                                 
            }#end ifevent 
             
       }#end foreach lockedout event 
        
    }#end process 
    
}#end function
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Ouputing ForEach loop to a $richTextBox

Post by jvierra »

I just posted the solution above. I think you are missing how this all works and I cannot determine where you are disconnected.

The output is a collection of objects. You have to convert them to strings or you will get a jumble.
This topic is 9 years and 6 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