从GUI复制和粘贴信息

时间:2019-07-17 10:40:15

标签: winforms powershell user-interface

我无法弄清楚如何将整个文本从我的GUI复制到剪贴板或标记文本>复制然后将其粘贴到GUI之外

我的代码是

# FUNCTION SEARCH & COLLECT #
Function List-UserInfo {
    Param(
        [Parameter(Mandatory=$true)]
        [String]$username
    )
    Try{
        $user = @(Get-ADUser $username -properties DisplayName,Mail,mobile,streetAddress,City,PostalCode,LastLogon,DistinguishedName)
    }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("E-Mail")
        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("<PhoneNo.>")
        }
        $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("<IP/Name>")
        }
        $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
    }
}

0 个答案:

没有答案