[玩转系统] 准备到剪贴板
作者:精品下载站 日期: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。我只是创建了一个具有一些附加功能的新版本的命令。如果您对我的流程或某些脚本元素有任何疑问,请发表评论。
享受!
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag