Home > database >  How can I output Results from command to textbox
How can I output Results from command to textbox

Time:08-26

Reposting with entire code. This works fine but I want want the username to be output to $User not Out-GridView. The rest of the form relies on the information in the textboe $User I am sure this is something simple that is just eluding me. I tried using $User = $Result as suggested but but that did not work.

<# 
.NAME
    AD Account Tool
.SYNOPSIS
    Check User by SamAccountName . Can Unlock User and lock user. Reset Password, enable nad disable user
.DESCRIPTION
    Checks user by SamAccountName. Returns Name, Last LogonDate, LockedOut Status, LockedoutTime, and Enabled Status. Allows User to be unlocked and locked. Locking of user is by increasing badpasswordcount. User is able to reset password for account. Enabling and disabling of Users are allowed.
#>

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

$Lbl_Inst1                   = New-Object system.Windows.Forms.Label
$Lbl_Inst1.text              = "Enter the First and Last Name if Windows Login is not known."
$Lbl_Inst1.AutoSize          = $true
$Lbl_Inst1.width             = 700
$Lbl_Inst1.height            = 10
$Lbl_Inst1.location          = New-Object System.Drawing.Point(10,10)
$Lbl_Inst1.Font              = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$Lbl_Inst5                   = New-Object system.Windows.Forms.Label
$Lbl_Inst5.text              = "*Must Match Employee Record Exactly"
$Lbl_Inst5.AutoSize          = $true
$Lbl_Inst5.width             = 700
$Lbl_Inst5.height            = 10
$Lbl_Inst5.location          = New-Object System.Drawing.Point(370,10)
$Lbl_Inst5.Font              = New-Object System.Drawing.Font('Microsoft Sans Serif',8,[System.Drawing.FontStyle]([System.Drawing.FontStyle]::Italic -bor [System.Drawing.FontStyle]::Underline))

$Lbl_Line1                   = New-Object system.Windows.Forms.Label
$Lbl_Line1.text              = "__________________________________________________________________________________________________________________"
$Lbl_Line1.AutoSize          = $true
$Lbl_Line1.width             = 1000
$Lbl_Line1.height            = 10
$Lbl_Line1.location          = New-Object System.Drawing.Point(10,100)
$Lbl_Line1.Font              = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$Lbl_Inst2                   = New-Object system.Windows.Forms.Label
$Lbl_Inst2.text              = "Account Information and Administration"
$Lbl_Inst2.AutoSize          = $true
$Lbl_Inst2.width             = 700
$Lbl_Inst2.height            = 10
$Lbl_Inst2.location          = New-Object System.Drawing.Point(150,120)
$Lbl_Inst2.Font              = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$Lbl_Inst3                   = New-Object system.Windows.Forms.Label
$Lbl_Inst3.text              = "Information"
$Lbl_Inst3.AutoSize          = $true
$Lbl_Inst3.width             = 700
$Lbl_Inst3.height            = 10
$Lbl_Inst3.location          = New-Object System.Drawing.Point(300,150)
$Lbl_Inst3.Font              = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$Lbl_Inst4                   = New-Object system.Windows.Forms.Label
$Lbl_Inst4.text              = "Administration"
$Lbl_Inst4.AutoSize          = $true
$Lbl_Inst4.width             = 700
$Lbl_Inst4.height            = 10
$Lbl_Inst4.location          = New-Object System.Drawing.Point(440,150)
$Lbl_Inst4.Font              = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$CheckLockTool                   = New-Object system.Windows.Forms.Form
$CheckLockTool.ClientSize        = New-Object System.Drawing.Point(600,360)
$CheckLockTool.text              = "User Account Administration Tool"
$CheckLockTool.TopMost           = $false

$CheckLocked                     = New-Object system.Windows.Forms.Button
$CheckLocked.text                = "Check Locked"
$CheckLocked.width               = 100
$CheckLocked.height              = 30
$CheckLocked.location            = New-Object System.Drawing.Point(300,175)
$CheckLocked.Font                = New-Object System.Drawing.Font('Microsoft Sans Serif',8)

$CheckGroups                     = New-Object system.Windows.Forms.Button
$CheckGroups.text                = "Check Groups"
$CheckGroups.width               = 100
$CheckGroups.height              = 30
$CheckGroups.location            = New-Object System.Drawing.Point(300,205)
$CheckGroups.Font                = New-Object System.Drawing.Font('Microsoft Sans Serif',8)

$FirstName                       = New-Object system.Windows.Forms.TextBox
$FirstName.Text                  = ""
$FirstName.multiline             = $false
$FirstName.width                 = 100
$FirstName.height                = 20
$FirstName.location              = New-Object System.Drawing.Point(10,60)
$FirstName.Font                  = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$Lbl_FirstName                   = New-Object system.Windows.Forms.Label
$Lbl_FirstName.text              = "First Name"
$Lbl_FirstName.AutoSize          = $true
$Lbl_FirstName.width             = 25
$Lbl_FirstName.height            = 10
$Lbl_FirstName.location          = New-Object System.Drawing.Point(10,40)
$Lbl_FirstName.Font              = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$LastName                        = New-Object system.Windows.Forms.TextBox
$LastName.Text                   = ""
$LastName.multiline              = $false
$LastName.width                  = 100
$LastName.height                 = 20
$LastName.location               = New-Object System.Drawing.Point(150,60)
$LastName.Font                   = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$Lbl_LastName                    = New-Object system.Windows.Forms.Label
$Lbl_LastName.text               = "Last Name"
$Lbl_LastName.AutoSize           = $true
$Lbl_LastName.width              = 25
$Lbl_LastName.height             = 10
$Lbl_LastName.location           = New-Object System.Drawing.Point(150,40)
$Lbl_LastName.Font               = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$User                            = New-Object system.Windows.Forms.TextBox
$User.Text                       = ""
$User.multiline                  = $false
$User.width                      = 175
$User.height                     = 20
$User.location                   = New-Object System.Drawing.Point(14,170)
$User.Font                       = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$Header                          = New-Object system.Windows.Forms.Label
$Header.text                     = "Enter Users Windows Login"
$Header.AutoSize                 = $true
$Header.width                    = 25
$Header.height                   = 10
$Header.location                 = New-Object System.Drawing.Point(12,150)
$Header.Font                     = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$UnlockAccount                   = New-Object system.Windows.Forms.Button
$UnlockAccount.text              = "Unlock Account"
$UnlockAccount.width             = 100
$UnlockAccount.height            = 30
$UnlockAccount.location          = New-Object System.Drawing.Point(440,175)
$UnlockAccount.Font              = New-Object System.Drawing.Font('Microsoft Sans Serif',8)
<#
$LockAccount                     = New-Object system.Windows.Forms.Button
$LockAccount.text                = "Lock Account"
$LockAccount.width               = 100
$LockAccount.height              = 30
$LockAccount.location            = New-Object System.Drawing.Point(440,205)
$LockAccount.Font                = New-Object System.Drawing.Font('Microsoft Sans Serif',8)
#>
$LastLogon                       = New-Object system.Windows.Forms.Button
$LastLogon.text                  = "Last Logon"
$LastLogon.width                 = 100
$LastLogon.height                = 30
$LastLogon.location              = New-Object System.Drawing.Point(300,235)
$LastLogon.Font                  = New-Object System.Drawing.Font('Microsoft Sans Serif',8)

$Header2                         = New-Object system.Windows.Forms.Label
$Header2.text                    = "Set New Password"
$Header2.AutoSize                = $true
$Header2.width                   = 25
$Header2.height                  = 10
$Header2.location                = New-Object System.Drawing.Point(14,215)
$Header2.Font                    = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$Password                        = New-Object system.Windows.Forms.TextBox
$Password.multiline              = $false
$Password.width                  = 174
$Password.height                 = 20
$Password.location               = New-Object System.Drawing.Point(12,235)
$Password.Font                   = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$SetPassword                     = New-Object system.Windows.Forms.Button
$SetPassword.text                = "Set Password"
$SetPassword.width               = 100
$SetPassword.height              = 30
$SetPassword.location            = New-Object System.Drawing.Point(12,270)
$SetPassword.Font                = New-Object System.Drawing.Font('Microsoft Sans Serif',8)

$DIsableAccount                  = New-Object system.Windows.Forms.Button
$DIsableAccount.text             = "Disable Account"
$DIsableAccount.width            = 100
$DIsableAccount.height           = 30
$DIsableAccount.location         = New-Object System.Drawing.Point(440,235)
$DIsableAccount.Font             = New-Object System.Drawing.Font('Microsoft Sans Serif',8)

$EnableAccount                   = New-Object system.Windows.Forms.Button
$EnableAccount.text              = "Enable Account"
$EnableAccount.width             = 100
$EnableAccount.height            = 30
$EnableAccount.location          = New-Object System.Drawing.Point(440,205)
$EnableAccount.Font              = New-Object System.Drawing.Font('Microsoft Sans Serif',8)

$getacctname                     = New-Object system.Windows.Forms.Button
$getacctname.text                = "Get Login"
$getacctname.width               = 100
$getacctname.height              = 30
$getacctname.location            = New-Object System.Drawing.Point(300,50)
$getacctname.Font                = New-Object System.Drawing.Font('Microsoft Sans Serif',10)


$CheckLockTool.controls.AddRange(@($Lbl_Inst5,$Lbl_Inst4,$Lbl_Inst3,$Lbl_Inst2,$Lbl_Line1,$Lbl_Inst1,$Lbl_FirstName,$Lbl_LastName,$FirstName,$LastName,$getacctname,$LastLogon,$CheckGroups,$CheckLocked,$User,$Header,$UnlockAccount,$Header2,$Password,$SetPassword,$DIsableAccount,$EnableAccount))

$CheckLocked.Add_Click({ CheckLocked })
$CheckGroups.Add_Click({ CheckGroups })
$UnlockAccount.Add_Click({ UnlockAccount })
#$LockAccount.Add_Click({ LockAccount })
$SetPassword.Add_Click({ SetPassword })
$DIsableAccount.Add_Click({ DisableAccount })
$EnableAccount.Add_Click({ EnableAccount })
$LastLogon.Add_Click({ LastLogon })
$getacctname.Add_Click({getacctname})

#region Logic 

#Write your logic code here

function getacctname {
    $fname= $FirstName.Text
    $lname= $LastName.Text
    $Result = Get-ADUser -Filter "GivenName -eq '$fname' -and SurName -eq '$lname'"| Select-Object -ExpandProperty 'SamAccountName' 
    $Result | Out-Gridview -Title 'Windows Logon'
}

function SetPassword { 
    Set-ADAccountPassword -Identity $User.text -NewPassword (ConvertTo-SecureString -AsPlainText $Password.text -Force)
    [System.Windows.MessageBox]::Show('Password Changed')
}

function CheckLocked {

$Result = Get-ADUser -Identity $User.text -Properties Name, LastLogonDate, LockedOut, AccountLockOutTime, Enabled | select Name, LastLogonDate, LockedOut, AccountLockOutTime, Enabled 
$Result | Out-GridView -Title 'Locked Accounts'

    
}

function CheckGroups {

$Result = Get-ADUser –Identity $User.text -Properties Name, Memberof | Select-Object -ExpandProperty MemberOf
$Result | Out-GridView -Title 'Group Memberships'

    
}

function LastLogon {

$Result = Get-ADUser -Identity $User.text -Properties Name, LastLogonDate| Select-Object -ExpandProperty LastLogonDate 
$Result | Out-GridView -Title 'Last Logon'

    
}

function UnlockAccount { 
    Unlock-ADAccount -Identity $User.text
    
    $Result = Get-ADUser -Identity $User.text -Properties Name, LastLogonDate, LockedOut, AccountLockOutTime, Enabled | select Name, LastLogonDate, LockedOut, AccountLockOutTime, Enabled 
    $Result | Out-GridView -Title 'Unlocked Account'
}
<#
function LockAccount { 
if ($LockoutBadCount = ((([xml](Get-GPOReport -Name "Default Domain Policy" -ReportType Xml)).GPO.Computer.ExtensionData.Extension.Account |
            Where-Object name -eq LockoutBadCount).SettingNumber)) {
 
    $Password = ConvertTo-SecureString 'NotMyPassword' -AsPlainText -Force
 
    Get-ADUser -Identity $User.text -Properties SamAccountName, UserPrincipalName, LockedOut |
        ForEach-Object {
 
            for ($i = 1; $i -le $LockoutBadCount; $i  ) { 
 
                Invoke-Command -ComputerName dc01 {Get-Process
                } -Credential (New-Object System.Management.Automation.PSCredential ($($_.UserPrincipalName), $Password)) -ErrorAction SilentlyContinue            
 
            }
 
            $Result = Get-ADUser -Identity $User.text -Properties Name, LastLogonDate, LockedOut, AccountLockOutTime, Enabled | select Name, LastLogonDate, LockedOut, AccountLockOutTime, Enabled 
            $Result | Out-GridView -Title 'Unlocked Account'
        }
}
}
#>


function EnableAccount {
    Enable-ADAccount -Identity $User.text
    $Result = Get-ADUser -Identity $User.text -Properties Name, LastLogonDate, LockedOut, AccountLockOutTime, Enabled | select Name, LastLogonDate, LockedOut, AccountLockOutTime, Enabled 
    $Result | Out-GridView -Title 'Enabled Account'
    }


function DisableAccount { 
    Disable-ADAccount -Identity $User.text
    $Result = Get-ADUser -Identity $User.text -Properties Name, LastLogonDate, LockedOut, AccountLockOutTime, Enabled | select Name, LastLogonDate, LockedOut, AccountLockOutTime, Enabled 
    $Result | Out-GridView -Title 'Disabled Account'
}


# Disable other types of close/exit
#$form.add_FormClosing({$_.Cancel=$true})


#Write-Output
#endregion

[void]$CheckLockTool.ShowDialog()

CodePudding user response:

Don't have the AD Module here for testing so will use this tweaked version of the function to show you have you can achieve this, you simply need to add -PassThru to demo

  • Related