当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 准备到剪贴板

作者:精品下载站 日期:2024-12-14 07:46:37 浏览:14 分类:玩电脑

准备到剪贴板


由于我每天花大量时间创建 PowerShell 相关内容,因此我经常需要从 PowerShell 会话复制命令输出。快速但肮脏的解决方案是将我的表达式通过管道传输到 Clip.exe 命令行实用程序。

get-service | where { $_.status -eq 'running'} | clip

这适用于控制台和 PowerShell ISE。但在某些情况下,我希望看到结果,以便我知道它值得粘贴到我正在做的任何事情中。换句话说,我想同时将命令的结果发送到管道和剪贴板。 PowerShell 已经可以使用 Tee-Object cmdlet 执行类似的操作。使用该 cmdlet,您可以将输出发送到变量或文件。因此,我决定围绕 Tee-Object 构建一个包装器,并添加将 tee 输出发送到剪贴板的功能。

我创建了 Tee-Object 的副本并开始修改。我不想对 cmdlet 进行逆向工程。我只是想添加一个新参数。在某些方面,我的新命令就像一个代理功能。

#requires -version 4.0


<#
This is a copy of:

CommandType Name       ModuleName                  
----------- ----       ----------                  
Cmdlet      Tee-Object Microsoft.PowerShell.Utility

Created: 8/12/2015
Author : Jeff Hicks @JeffHicks

  ****************************************************************
  * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED *
  * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK.  IF   *
  * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, *
  * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING.             *
  ****************************************************************
#>


Function Tee-MyObject {
<#

.SYNOPSIS

Saves command output in a file, variable or Windows Clipboard and also sends it down the pipeline.


.DESCRIPTION

The cmdlet redirects output, that is, it sends the output of a command in two directions (like the letter "T"). It stores the output in a file or variable and also sends it down the pipeline. If  is the last command in the pipeline, the command output is displayed at the prompt.

This command is a modified version of Tee-Object.
.PARAMETER Clipboard
Copy the output to the Windows clipboard

.PARAMETER Append
Appends the output to the specified file. Without this parameter, the new content replaces any existing content in the file without warning.

This parameter is introduced in Windows PowerShell 3.0.

.PARAMETER FilePath
Saves the object in the specified file. Wildcard characters are permitted, but must resolve to a single file.

.PARAMETER InputObject
Specifies the object to be saved and displayed. Enter a variable that contains the objects or type a command or expression that gets the objects. You can also pipe an object to .

When you use the InputObject parameter with , instead of piping command results to , the InputObject value—even if the value is a collection that is the result of a command, such as -InputObject (Get-Process)—is treated as a single object. Because InputObject cannot return individual properties from an array or collection of objects, it is recommended that if you use  to perform operations on a collection of objects for those objects that have specific values in defined properties, you use  in the pipeline, as shown in the examples in this topic.

.PARAMETER Variable
Saves the object in the specified variable. Enter a variable name without the preceding dollar sign ($).

.PARAMETER LiteralPath
Saves the object in the specified file. Unlike FilePath, the value of the LiteralPath parameter is used exactly as it is typed. No characters are interpreted as wildcards. If the path includes escape characters, enclose it in single quotation marks. Single quotation marks tell Windows PowerShell not to interpret any characters as escape sequences.


.EXAMPLE
PS C:\> Get-Eventlog -list | Tee-MyObject -clipboard


  Max(K) Retain OverflowAction        Entries Log 
  ------ ------ --------------        ------- --- 
  20,480      0 OverwriteAsNeeded      30,245 Application
  20,480      0 OverwriteAsNeeded           0 HardwareEvents 
     512      7 OverwriteOlder              0 Internet Explorer
  20,480      0 OverwriteAsNeeded           0 Key Management Service
     128      0 OverwriteAsNeeded       1,590 OAlerts               
  32,768      0 OverwriteAsNeeded      49,749 Security 
  20,480      0 OverwriteAsNeeded      33,273 System 
  15,360      0 OverwriteAsNeeded      11,442 Windows PowerShell

Display eventlog information and also copy it to the Windows clipboard.

.EXAMPLE

PS C:\> get-process | Tee-MyObject -filepath C:\Test1\testfile2.txt

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)    Id ProcessName
-------  ------    -----      ----- -----   ------    -- -----------
83       4     2300       4520    39     0.30    4032 00THotkey
272      6     1400       3944    34     0.06    3088 alg
81       3      804       3284    21     2.45     148 ApntEx
81       4     2008       5808    38     0.75    3684 Apoint
...
This command gets a list of the processes running on the computer and sends the result to a file. Because a second path is not specified, the processes are also displayed in the console.

.EXAMPLE

PS C:\> get-process notepad | Tee-MyObject -variable proc | select-object processname,handles

ProcessName                              Handles
-----------                              -------
notepad                                  43
notepad                                  37
notepad                                  38
notepad                                  38
This command gets a list of the processes running on the computer and sends the result to a variable named "proc". It then pipes the resulting objects along to Select-Object, which selects the ProcessName and Handles property. Note that the $proc variable includes the default information returned by Get-Process.

.EXAMPLE

PS C:\>get-childitem -path D: -file -system -recurse | Tee-MyObject -file c:\test\AllSystemFiles.txt -append | out-file c:\test\NewSystemFiles.txt
This command saves a list of system files in a two log files, a cumulative file and a current file.
The command uses the Get-ChildItem cmdlet to do a recursive search for system files on the D: drive. A pipeline operator (|) sends the list to , which appends the list to the AllSystemFiles.txt file and passes the list down the pipeline to the Out-File cmdlet, which saves the list in the NewSystemFiles.txt file.

.NOTES

You can also use the Out-File cmdlet or the redirection operator, both of which save the output in a file but do not send it down the pipeline.
 uses Unicode encoding when it writes to files. As a result, the output might not be formatted properly in files with a different encoding. To specify the encoding, use the Out-File cmdlet.

Learn more about PowerShell:
Essential PowerShell Learning Resources

.INPUTS
System.Management.Automation.PSObject


.OUTPUTS
System.Management.Automation.PSObject

.LINK
Tee-Object
Select-Object
about_Redirection

#>
[CmdletBinding(DefaultParameterSetName='File')]
Param(

    [Parameter(ValueFromPipeline=$true)]
    [psobject]$InputObject,

    [Parameter(ParameterSetName='File', Mandatory=$true, Position=0)]
    [string]$FilePath,

    [Parameter(ParameterSetName='LiteralFile', Mandatory=$true)]
    [Alias('PSPath')]
    [string]$LiteralPath,

    [Parameter(ParameterSetName='File')]
    [switch]$Append,

    [Parameter(ParameterSetName='Variable', Mandatory=$true)]
    [string]$Variable,

    [Parameter(ParameterSetName='Clip')]
    [Switch]$Clipboard
)

Begin {

    Write-Verbose "Starting $($MyInvocation.Mycommand)"
    Write-Verbose "Using parameter set $($PSCmdlet.ParameterSetName)"  

    #initialize array to hold all input. We will process it later
    #with Tee-Object
    $data=@()
} #begin

Process {
    #add each piped in object to the array
    $data+= $InputObject
} #process

End {
    Write-Verbose "Processing $($data.count) input items"
    #run results through Tee-Object or Clipboard
    #remove Clipboard from PSBoundParameters
    if ($Clipboard) {
        #copy to clipboard with extra spaces removed
        $text = ($data | Out-String).Trim() -replace "\s+`n","`n"
        #make sure type is loaded
        Write-Verbose "Adding necessary .NET assembly"
        Add-Type -AssemblyName system.windows.forms
        [System.Windows.Forms.Clipboard]::SetText( $text )
        #write data to the pipeline
        $data
    }
    Else {
        $PSBoundParameters.InputObject = $data
        Write-Verbose "Using PSBoundparameters $($PSBoundParameters | Out-String)"
        Tee-Object @PSBoundParameters

        if ($Variable) {
            #raise the scope to the parent or calling level to persist it
            Write-Verbose "Adjusting variable scope"
            #create a copy of the variable in the parent scope
            New-Variable -Name $Variable -Value $(Get-Variable -name $variable).value -Scope 1 -Force
        }
    }

    Write-Verbose "Ending $($MyInvocation.Mycommand)"

} #end

} #end function Tee-Object

#define an optional alias
Set-Alias -Name tmo -Value Tee-MyObject

我使用 Tee-MyObject 的方式与 Tee-Object 非常相似,即使参数相同。这些是通过 $PSBoundParameters 分配的。我想指出几点,更多的是脚本技术问题。

首先,因为我的函数围绕 Tee-Object,而 Tee-Object 通常具有通过管道传输的输入,所以我需要将管道传输的对象临时存储到我的函数中。这就是为什么我初始化变量 $data 并将每个输入对象添加到其中。当所有内容都通过管道传输到我的函数后,我可以将数据传递给 Tee-Object,假设我正在使用它的参数之一。

$PSBoundParameters.InputObject = $data
Write-Verbose "Using PSBoundparameters $($PSBoundParameters | Out-String)"
Tee-Object @PSBoundParameters

但是,因为我在函数内部运行 Tee-Object,所以作用域成为一个问题。当我使用 -Variable 参数时,Tee-Object 创建变量没有问题。但是当函数结束时,变量就被销毁了。我的解决方案是创建变量的新副本并指定父范围。

if ($Variable) {
            #raise the scope to the parent or calling level to persist it
            Write-Verbose "Adjusting variable scope"
            #create a copy of the variable in the parent scope
            New-Variable -Name $Variable -Value $(Get-Variable -name $variable).value -Scope 1 -Force
        }

我需要这个“技巧”,以便在我的包装器函数和 Tee-Object 之间传递完整的功能。

对于剪贴板部分,我最初使用代码将数据简单地通过管道传输到 Clip.exe。但是,这可能会暂时闪烁控制台窗口,并且根据命令,我还会在每行末尾出现大量额外的空白。所以我决定使用.NET框架将内容添加到剪贴板。棘手的部分是将输出转换为文本并清理它。在将数据传输到 Out-String 后,我最终使用 Trim() 方法删除前导和尾随空格。这给我留下了一根长绳子。为了清理某些行末尾的额外空格,我使用 -Replace 运算符来查找与一堆空格后跟新行标记匹配的任何内容,并将其替换为新行标记。这具有修剪掉所有这些空间的效果。

$text = ($data | Out-String).Trim() -replace "\s+`n","`n"

就是这样!我的版本甚至还有 Tee-Object 帮助的修改副本。

[玩转系统] 准备到剪贴板

我可以使用我的命令代替 Tee-Object。我什至定义了自己的别名。

[玩转系统] 准备到剪贴板

需要明确的是,我没有修改、覆盖或删除 Tee-Object。我只是创建了一个具有一些附加功能的新版本的命令。如果您对我的流程或某些脚本元素有任何疑问,请发表评论。

享受!

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯