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

[玩转系统] 在 PowerShell 中使用优化的文本文件

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

在 PowerShell 中使用优化的文本文件


[玩转系统] 在 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 中使用的所有文本文件都干净整洁固然很好,但不能保证其他人不会出现并再次搞砸。希望这个功能能有所帮助。

请告诉我这在现实世界中如何为您工作,或者您在使用文本文件时遇到了哪些其他常见问题。请务必查看完整的帮助和示例。享受!

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

取消回复欢迎 发表评论:

关灯