[玩转系统] 在 PowerShell 中使用优化的文本文件
作者:精品下载站 日期:2024-12-14 07:43:43 浏览:13 分类:玩电脑
在 PowerShell 中使用优化的文本文件
您可以使用Where-Object 并过滤掉这些类型的问题。或者您可以使用我编写的一个名为 Optimize-Text 的新函数。
#requires -version 2.0
Function Optimize-Text {
<#
.Synopsis
Clean and optimize text input.
.Description
Use this command to clean and optimize content from text files. Sometimes text files have blank lines or the content has trailing spaces. These sorts of issues can cause problems when passing the content to other commands.
This command will strip out any lines that are blank or have nothing by white space, and trim leading and trailing spaces. The optimized text is then written back to the pipeline.
Optionally, you can specify a property name. This can be useful when your text file is a list of computernames and you want to take advantage of pipeline binding. See examples.
If your text file has commented lines, use the ignore parameter. As long as the character is the first non-whitespace character in the line, the line will be treated as a comment and ignored.
Finally, you can use the -Filter parameter to specify a regular expression pattern to further filter what text is written to the pipeline. The filter is applied after leading and trailing spaces have been removed and before any text is converted to upper case.
.Parameter ToUpper
Write text output as upper case.
.Parameter Ignore
Specify a character that will be interpreted as a comment character. It must be the first word character in a line. These lines will be ignored. This parameter has an alias of 'comment'.
.Parameter PropertyName
Assign each line of text a property name. This has the effect of turning your text file into an array of objects with a single property.
.Parameter Filter
Use a regular expression pattern to filter. The filtering is applied after leading and trailing spaces have been trimmed and before text can be converted to upper case.
.Example
PS C:\> get-content c:\scripts\computers.txt
win81-ent-01
serenity
quark
jdhit-dc01
novo8
PS C:> get-content c:\scripts\computers.txt | optimize-text
win81-ent-01
serenity
quark
jdhit-dc01
novo8
PS C:> get-content c:\scripts\computers.txt | optimize-text -property computername
computername
------------
win81-ent-01
serenity
quark
jdhit-dc01
novo8
.Example
PS C:\scripts> get-content computers.txt | optimize-text -prop computername | where { test-connection $_.computername -count 1 -erroraction silentlycontinue} | get-service bits | select Name,Status,Machinename
Name Status MachineName
---- ------ -----------
bits Running win81-ent-01
bits Running jdhit-dc01
bits Running novo8
Optimize the computernames in computers.txt and add a Computername property. Test each computer, ignoring those that fail, and get the Bits service on the ones that can be pinged.
.Example
PS C:\Scripts> get-content .\ChicagoServers.txt | optimize-text -Ignore "#" -Property ComputerName
ComputerName
------------
chi-fp01
chi-fp02
chi-core01
chi-test
chi-dc01
chi-dc02
chi-dc04
chi-db01
.Example
PS C:\scripts> get-content .\ChicagoServers.txt | optimize-text -filter "dc\d{2}" -ToUpper -PropertyName Computername | test-connection -count 1
Source Destination IPV4Address IPV6Address Bytes Time(ms)
------ ----------- ----------- ----------- ----- --------
WIN81-ENT-01 CHI-DC01 172.16.30.200 32 0
WIN81-ENT-01 CHI-DC02 172.16.30.201 32 0
WIN81-ENT-01 CHI-DC04 172.16.30.203 32 0
Get names from text file that match the pattern, turn into an object with a property name and pipe to Test-Connection.
.Notes
Last Updated: Sept. 8, 2014
Version : 1.0
Learn more:
PowerShell in Depth: An Administrator's Guide (http://www.manning.com/jones6/)
PowerShell Deep Dives (http://manning.com/hicks/)
Learn PowerShell in a Month of Lunches (http://manning.com/jones3/)
Learn PowerShell Toolmaking in a Month of Lunches (http://manning.com/jones4/)
****************************************************************
* 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. *
****************************************************************
.Inputs
System.String
.Link
https://jdhitsolutions.com/blog/2014/09/using-optimized-text-files-in-powershell
#>
[cmdletbinding(DefaultParameterSetName="Default")]
[OutputType([string],ParameterSetName="Default")]
[OutputType([psobject],ParameterSetName="object")]
Param(
[Parameter(Position=0,HelpMessage="Enter some text",ValueFromPipeline=$True)]
[string[]]$Text,
[regex]$Filter,
[Parameter(ParameterSetName="object")]
[string]$PropertyName,
[Alias("comment")]
[string]$Ignore,
[switch]$ToUpper
)
Begin {
Write-Verbose "Starting $($MyInvocation.Mycommand)"
Write-Verbose "Using parameters: `n $($PSBoundParameters.GetEnumerator() | format-table | out-string)"
} #begin
Process {
foreach ($item in $text) {
#filter out items that don't have at least one non-whitespace character
if ($item -match "\S") {
#trim spaces
Write-Verbose "Trimming: $item"
$output = $item.Trim()
if ($Filter) {
Write-Verbose "Filtering"
$output = $output | where {$_ -match $filter}
}
#only continue if there is output
if ($output) {
Write-Verbose "Post processing $output"
if ($ToUpper) {
$output = $output.toUpper()
} #if to upper
#filter out if using the comment character
if ($Ignore -AND ($output -match "^[\s+]?$Ignore")) {
Write-Verbose "Ignoring comment $output"
} #if ignore
else {
if ($PropertyName) {
#create a custom object with the specified property
New-Object -TypeName PSObject -Property @{$PropertyName = $Output}
}
else {
#just write the output to the pipeline
$output
}
} #else not ignoring
} #if output
} #if item matches non-whitespce
} #foreach
} #process
End {
Write-Verbose "Ending $($MyInvocation.Mycommand)"
} #end
} #end function
#define an alias
Set-Alias -Name ot -Value Optimize-Text
我尝试向此命令添加功能,以解决我认为文本文件的常见问题。核心功能是删除每行文本中的任何空白行并修剪前导和尾随空格。我假设您将使用像 Get-Content 这样的命令以及您想要与另一个 cmdlet 一起使用的某种列表。您可以在两者之间插入 Optimize-Text。
例如,也许您想要对计算机列表进行快速而肮脏的 ping 测试:
get-content .\computers.txt | optimize-text | where {Test-Connection $_ -count 1 -ea 0}
在此示例中,computers.txt 非常混乱。有空行,并且某些名称具有前导和/或尾随空格。使用 Optimize-Text 可以解决这些问题。但是等等……还有更多!
使用 PowerShell 的优点是一切都是对象。通常,它有助于利用管道绑定。例如,Test-Connection 的 Computername 参数接受按属性名称输入的管道。因此,如果传入对象具有与参数名称匹配的属性,它将使用它。 Optimize-Text 允许您指定属性名称。当您这样做时,每一行都会变成具有单个属性名称的自定义对象。
PS C:\scripts> get-content .\computers.txt | optimize-text -PropertyName Computername
Computername
------------
win81-ent-01
serenity
quark
jdhit-dc01
novo8
这意味着我可以运行如下命令:
PS C:\scripts> get-content .\computers.txt | optimize-text -PropertyName Computername | test-connection -count 1 -erroraction SilentlyContinue
Source Destination IPV4Address IPV6Address Bytes Time(ms)
------ ----------- ----------- ----------- ----- --------
WIN81-ENT-01 win81-ent-01 172.16.30.127 fe80::bd30:b56e:c9fa:87e1%18 32 0
WIN81-ENT-01 jdhit-dc01 172.16.10.1 32 0
如果您今天下载这个函数,我将添加参数来将每行文本转换为大写,忽略具有指定注释字符的行,并使用正则表达式模式进行额外的过滤!
PS C:\scripts> get-content .\ChicagoServers.txt | optimize-Text -ToUpper -Ignore "#" -filter "dc\d{2}" -PropertyName Com
putername | get-ciminstance win32_logicaldisk -filter "deviceid='c:'" | Select DeviceID,Size,Freespace,PSComputername
DeviceID Size Freespace PSComputerName
-------- ---- --------- --------------
C: 15999168512 1005551616 CHI-DC01
C: 12777943040 2873114624 CHI-DC02
C: 42946523136 25388650496 CHI-DC04
顺便说一句,文本文件如下所示:
#Globomantics chicago servers
chi-fp01
chi-fp02
chi-core01
chi-test
#these are the DCs
chi-dc01
chi-dc02
chi-dc04
chi-db01
chi-hvr2
#end of file
虽然认为您在 PowerShell 中使用的所有文本文件都干净整洁固然很好,但不能保证其他人不会出现并再次搞砸。希望这个功能能有所帮助。
请告诉我这在现实世界中如何为您工作,或者您在使用文本文件时遇到了哪些其他常见问题。请务必查看完整的帮助和示例。享受!
猜你还喜欢
- 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