none
Unable to copy/paste computer info from GUI RRS feed

  • General discussion

  • Hi,

    I have a GUI that extract ADUser/Ad computer information when you search on a SamAccountName. But I am having huge issue learning how to implement so that I can Copy and Paste the computer information or to get the computer name or IPV4address to list under (data and Value) so I can use my "Copy User" button in the GUI. I am very very new and I have Google and Goggled just to create what I got here. Is there anyone who can help with this?

    Code:

    <#
       
        # User Info #
    
    	.SYNOPSIS
            Display user & computer info.
        .DESCRIPTION
            Script for displaying account info by searching for username.
            Also finds computers that user own with information.
        .INFORMATION
            Version: 1.01
            Created: 2018-06-27
        .FILES
            .\userinfo.ps1
        .CHANGELOG
            2018-06-27: Started development
            2018-07-02: Ready for prod
    	2018-07-02: Added function to copy user info to clipboard
        2019-07-05: Modified output encoding to utf8
    #>
    
    # FUNCTION SEARCH & COLLECT #
    Function List-UserInfo {
        Param(
            [Parameter(Mandatory=$true)]
            [String]$username
        )
        Try{
            $user = @(Get-ADUser $username -properties DisplayName,Mail,mobile,streetAddress,City,PostalCode,DistinguishedName)
            $computer = @(get-adcomputer -filter "ManagedBy -eg '$user'" -properties Name, IPv4Address)
        }Catch{
        }
        
        $lv_comp.Items.Clear()
        $lv_user.Items.Clear()
    
        # When no user is found
        if($user.count -eq 0){
            $wshell = New-Object -ComObject Wscript.Shell
            $wshell.Popup("Could not find any user with that username, please try again.",0,"Error Message",0)
        # Display information
        }else{
            
            #region USER INFORMATION
            # Display Name
            $item1 = New-Object System.Windows.Forms.ListViewItem("")
            $item1.SubItems.Add("Name")
            if($user.displayname -ne $null){
                $item1.SubItems.Add($user.DisplayName)
            }else{
                $item1.SubItems.Add("")
            }
            $lv_user.Items.AddRange($item1)
    
            # Mail Address
            $item1 = New-Object System.Windows.Forms.ListViewItem("")
            $item1.SubItems.Add("Email")
            if($user.mail -ne $null){
                $item1.SubItems.Add($user.Mail)
            }else{
                $item.SubItems.Add("<EmailAddress>")
            }
            $lv_user.Items.AddRange($item1)
         
            # Phone
            $item1 = New-Object System.Windows.Forms.ListViewItem("")
            $item1.SubItems.Add("Telephone No.")
            if($user.mobile -ne $null){
                $item1.SubItems.Add($user.mobile)
            }else{
                $item1.SubItems.Add("<Telephone>")
            }
            $lv_user.Items.AddRange($item1)
    
            # Street Address
            $item1 = New-Object System.Windows.Forms.ListViewItem("")
            $item1.SubItems.Add("Site/Address")
            if($user.streetAddress -ne $null){
                $item1.SubItems.Add($user.streetAddress)
            }else{
                $item1.SubItems.Add("<Address>")
            }
            $lv_user.Items.AddRange($item1)
    
            # Postal Code
            $item1 = New-Object System.Windows.Forms.ListViewItem("")
            $item1.SubItems.Add("PostalCode")
            if($user.PostalCode -ne $null){
                $item1.SubItems.Add($user.postalcode)
            }else{
                $item1.SubItems.Add("<PostalCode>")
            }
            $lv_user.Items.AddRange($item1)
    
            # City
            $item1 = New-Object System.Windows.Forms.ListViewItem("")
            $item1.SubItems.Add("City")
            if($user.city -ne $null){
                $item1.SubItems.Add($user.city -replace @{c=$_.co})
            }else{
                $item1.SubItems.Add("<City>")
            }
            $lv_user.Items.AddRange($item1)
    
             # Computer
            $item1 = New-Object System.Windows.Forms.ListViewItem("")
            $item1.SubItems.Add("Computer")
            if($computer.Name -ne $null){
                $item1.SubItems.Add($computer.name)
             }else{
                 $item1.SubItems.Add("<ComputerName>")
            }
            $lv_user.Items.AddRange($item1)
            #endregion
            #region COMPUTER INFORMATION
            
            $dn = $user.DistinguishedName
            $computers = @(get-adcomputer -filter {ManagedBy -eq $dn} -properties Name,LastLogon,LastLogonTimeStamp,OperatingSystem,IPv4Address)
    
            if($computers.count -eq 0){
                
            }else{
                foreach($computer in $computers){
                    
                    # New List
                    $item1 = New-Object System.Windows.Forms.ListViewItem("")
                        
                    # Name on Computer
                    if($computer.Name -ne $null){
                        $item1.SubItems.Add($computer.name)
                    }else{
                        $item1.SubItems.Add("")
                    }
    
                    # IP Address
                    if($computer.IPv4Address -ne $null){
                        $item1.SubItems.Add($computer.IPv4Address)
                    }else{
                        $item1.SubItems.Add("")
                    }
    
                    # Last Logon on Computer
                    if($computer.LastLogon -ne $null){
                        $item1.SubItems.Add([Datetime]::FromFileTime($computer.LastLogon).ToString("yyyy-MM-dd HH:mm:ss"))
                    }else{
                        $item1.SubItems.Add("")
                    }
                    # Last Logon Time Stamp
                    if($computer.LastLogonTimeStamp -ne $null){
                        $item1.SubItems.Add([Datetime]::FromFileTime($computer.LastLogonTimeStamp).ToString("yyyy-MM-dd HH:mm:ss"))
                    }else{
                        $item1.SubItems.Add("")
                    }
                    
                    # Operating System
                    if($computer.OperatingSystem -ne $null){
                        $item1.SubItems.Add($computer.OperatingSystem)
                    }else{
                        $item1.SubItems.Add("")
                    }
    
                    # Adding items
                    $lv_comp.Items.AddRange($item1)
                }
            }
    
            #endregion
        }
    }
    
    # WINDOWS FORM #
    Add-Type -AssemblyName System.Windows.Forms
    
    $Form = New-Object system.Windows.Forms.Form
    $Form.Text = "User Information Tool"
    $Form.StartPosition = "CenterScreen"
    $Form.KeyPreview = $true
    $Form.TopMost = $false
    $Form.Width = 900
    $Form.Height = 400
    $Form.ForeColor = "White"
    # AUTHOR #
    $label = New-Object system.windows.Forms.Label
    $label.AutoSize = $true
    $label.Text = "Service Desk "+(Get-Date).Year
    $label.Width = 25
    $label.Height = 10
    $label.location = new-object system.drawing.point(25,330)
    $label.Font = "Segoe UI,8"
    $Form.ForeColor = "White"
    $Form.controls.Add($label)
    # TEXTBOX INPUT #
    $tb_input = New-Object system.windows.Forms.TextBox
    $tb_input.Width = 190
    $tb_input.Height = 25
    $tb_input.location = new-object system.drawing.point(100,25)
    $tb_input.Font = "Segoe UI,10"
    $Form.ForeColor = "White"
    $Form.controls.Add($tb_input)
    # LABEL INPUT #
    $la_input = New-Object system.windows.Forms.Label
    $la_input.AutoSize = $true
    $la_input.Text = "UserID:"
    $la_input.Width = 25
    $la_input.Height = 10
    $la_input.location = new-object system.drawing.point(25,25)
    $la_input.Font = "Segoe UI,10"
    $Form.ForeColor = "White"
    $Form.controls.Add($la_input)
    # BUTTON INPUT #
    $bt_input = New-Object system.windows.Forms.Button
    $bt_input.Text = "Search"
    $bt_input.Width = 100
    $bt_input.Height = 25
    $bt_input.location = new-object system.drawing.point(300,25)
    $bt_input.Font = "Segoe UI,10"
    $Form.controls.Add($bt_input)
    # BUTTON COPY USER INFO #
    $bt_copyuser = New-Object system.windows.Forms.Button
    $bt_copyuser.Text = "Copy user info"
    $bt_copyuser.Enabled = $true
    $bt_copyuser.Width = 80
    $bt_copyuser.Height = 23
    $bt_copyuser.location = new-object system.drawing.point(245,328)
    $bt_copyuser.Font = "Segoe UI,10"
    $Form.controls.Add($bt_copyuser)
    # LISTVIEW USER INFO #
    $lv_user = New-Object system.windows.Forms.ListView
    $lv_user.Text = "listView"
    $lv_user.Width = 300
    $lv_user.Height = 250
    $lv_user.View = 'Details'
    $lv_user.GridLines = $true;
    $lv_user.FullRowSelect = $true
    $lv_user.location = new-object system.drawing.point(25,75)
    $Form.controls.Add($lv_user)
    # COLUMN 1 #
    $ch1 = New-Object System.Windows.Forms.ColumnHeader
    $lv_user.Columns.Add($ch1) | Out-Null
    $ch1.Width = 0
    $ch1.Text = ""
    # COLUMN 2 #
    $ch2 = New-Object System.Windows.Forms.ColumnHeader
    $lv_user.Columns.Add($ch2) | Out-Null
    $ch2.Width = 84
    $ch2.Text = "Data"
    # COLUMN 3 - backcolor change #
    $ch3 = New-Object System.Windows.Forms.ColumnHeader
    $lv_user.Columns.Add($ch3) | Out-Null
    $ch3.Width = 216
    $ch3.Text = "Value"
    $lv_comp = New-Object system.windows.Forms.ListView
    $lv_comp.Text = "listView"
    $lv_comp.Width = 520
    $lv_comp.Height = 250
    $lv_comp.View = 'Details'
    $lv_comp.GridLines = $true;
    $lv_comp.FullRowSelect = $true
    $lv_comp.location = new-object system.drawing.point(350,75)
    $Form.BackColor = "#6699ff"
    $Form.controls.Add($lv_comp)
    # COLUMN 1 #
    $ch1 = New-Object System.Windows.Forms.ColumnHeader
    $lv_comp.Columns.Add($ch1) | Out-Null
    $ch1.Width = 0
    $ch1.Text = ""
    # COLUMN 2 #
    $ch2 = New-Object System.Windows.Forms.ColumnHeader
    $lv_comp.Columns.Add($ch2) | Out-Null
    $ch2.Width = $lv_comp.Width/4
    $ch2.TextAlign = "Center"
    $ch2.Text = "Computer Name"
    $Form.controls.Add($lv_user)
    # COLUMN 3 #
    $ch3 = New-Object System.Windows.Forms.ColumnHeader
    $lv_comp.Columns.Add($ch3) | Out-Null
    $ch3.Width = $lv_comp.Width/4
    $ch3.TextAlign = "Center"
    $ch3.Text = "IPv4Address"
    # COLUMN 4 #
    $ch4 = New-Object System.Windows.Forms.ColumnHeader
    $lv_comp.Columns.Add($ch4) | Out-Null
    $ch4.Width = $lv_comp.Width/4
    $ch4.TextAlign = "Center"
    $ch4.Text = "LastTimeStamp"
    # COLUMN 5 #
    $ch4 = New-Object System.Windows.Forms.ColumnHeader
    $lv_comp.Columns.Add($ch4) | Out-Null
    $ch4.Width = $lv_comp.Width/4
    $ch4.TextAlign = "Center"
    $ch4.Text = "Logged on"
    # COLUMN 6 #
    $ch5 = New-Object System.Windows.Forms.ColumnHeader
    $lv_comp.Columns.Add($ch5) | Out-Null
    $ch5.Width = $lv_comp.Width/4
    $ch5.TextAlign = "Center"
    $ch5.Text = "OperatingSystem"
    
    # BUTTON SEARCH #
    $bt_input.Add_Click({
        $bt_input.Enabled = $false
        List-UserInfo ($tb_input.Text)
        $bt_input.Enabled = $true
    })
    # KEY ESCAPE #
    $Form.Add_KeyDown({if ($_.KeyCode -eq "Escape"){$Form.Dispose()}})
    # KEY ENTER #
    $Form.Add_KeyDown({if ($_.KeyCode -eq "Enter"){$bt_input.PerformClick()}})
    # SELECT COMPUTER #
    $lv_comp.Add_Click({
    
        # Checking so there is a selection made
        if($lv_comp.SelectedItems -ne 0){
            
            # Looping through selection(s)
            Foreach($computer in ($lv_comp.SelectedItems)){
                if(Test-Connection ($computer.SubItems[1].Text) -Count 1){
                    $computer.ForeColor = "#269900"
                }else{
                    $computer.ForeColor = "#ff00bf"
                }
            }
            # Unselecting for better display
            $lv_comp.SelectedItems.Clear()
        }
    })
    # COPY USER INFO #
    $bt_copyuser.Add_Click({
        $bt_copyuser.Enabled = $false
        $data = @()
        # Checking if listview contains any values
        if($lv_user.Items.Count -gt 0){
          
            Foreach($item in $lv_user.Items){
                # Checking so line isn't a placeholder
                if($item.SubItems[1].Text -ne "" -and $item.SubItems[2].Text -ne ""){
                    $data += ($item.SubItems[1].Text+": "+$item.SubItems[2].Text)
                }
            }
        }
    
        # Copying to clipboard for pasting else where
        $data | Set-Clipboard
        $bt_copyuser.Enabled = $true
        $OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding
    })
    
    [void]$Form.ShowDialog()
    $Form.Dispose()

    • Changed type Bill_Stewart Thursday, September 5, 2019 9:45 PM
    • Moved by Bill_Stewart Thursday, September 5, 2019 9:46 PM This is not "fix/debug/rewrite script for me" forum
    Wednesday, July 24, 2019 10:51 AM

All replies

  • Please read the first post at the very top of this forum:

    This forum is for scripting questions rather than script requests


    -- Bill Stewart [Bill_Stewart]

    Wednesday, July 24, 2019 5:08 PM
  • PowerShell has clipboard commands that will place text and images on the clipboard.

    help set-clipboard -online
    help get-clipboard -online

    What you copy is up to you in code  Just create the text you want to copy and use the CmdLet.

    You can automatically copy CSV data from a DataGridView using its built in methods. Just select the DGV rows and use Ctl-C to copy to the clipboard.

    The clipboard contents can be pasted into Excel and will be pasted as data with column headers.

    To paste as text you can get the DGV data and use "ConvertTo-Csv" the use "Set-Clipboard" to put the CSV text on the clipboard.


    \_(ツ)_/

    Wednesday, July 24, 2019 8:35 PM
  • Note that you can ge the CSV from the Clip data from the CB in PowerShell/Net like this:

    [System.Windows.Forms.Clipboard]::GetData('CSV')


    \_(ツ)_/

    Wednesday, July 24, 2019 8:40 PM
  • I can also recommend that you not use a ListView as it is much harder to program even for experience programmers. Use DataGridView as it is a data bound control (ListView is not data binding).


    \_(ツ)_/

    Wednesday, July 24, 2019 8:42 PM