PowerShell Get-Content 等同于 PowerShell 的 Tail

通过自动化,从文本文件中读取数据是一种常见情景。大多数编程语言至少有一种读取文本文件的方式,PowerShell也不例外。PowerShell的\href{https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-content?view=powershell-7.2&viewFallbackFrom=powershell-7.1#parameters}{Get-Content} cmdlet,与Linux中的tail命令类似,可以读取文本文件的内容,并将数据导入PowerShell会话。\code

当您需要将文本文件用作脚本的输入时,PowerShell的\code{Get-Content} cmdlet是一个不可或缺的工具。也许您的PowerShell脚本需要读取计算机列表以进行监视,或者导入电子邮件模板以发送给用户。PowerShell的\code{Get-Content}轻松支持这些情景!\code

那么,如何实时跟踪日志文件呢?是的,PowerShell的\code{Get-Content}也可以做到!继续阅读本文,您将学习如何在PowerShell中使用\code{Get-Content} cmdlet读取文本文件。\code

先决条件\code

如果您有兴趣跟随本教程中的示例,您将需要满足以下要求。\code

  • 您需要一台运行Windows 10的计算机。本教程使用的是Windows 10版本20H2。但不用担心,您使用的Windows 10版本也是可以的。\code
  • 您的计算机应至少安装了Windows PowerShell 5.1,或\codePowerShell 7.1。此处使用的是PowerShell 7.1,但任何一个版本都可以!
  • 您将编写和测试命令,因此您需要一个代码编辑器。推荐的编辑器是Windows PowerShell ISE,内置于Windows中,以及Visual Studio Code(VSCode)。本文使用的是VSCode。
  • 如果您在计算机上创建一个工作目录,也会有所帮助。工作文件夹可以放在您想要的任何位置。但是,您会注意到本教程中的示例位于C:\demo文件夹中。
  • 要开始,您需要一些内容!在您的工作目录中创建一个名为fruits.txt的文件,其中包含十种不同的水果以简化操作。您将在所有示例中使用此文本文件。
cherry
 berry
 apricot
 papaya
 raspberry
 melon
 peach
 tangerine
 cantaloupe
 orange

不知道您使用的是哪个PowerShell版本?访问文章如何检查您的PowerShell版本(所有方法!)

读取文本文件并将结果返回为字符串数组

Get-Content cmdlet从文件中读取内容,并默认情况下,将文本文件的每一行都作为一个字符串对象返回。因此,PowerShell对象的集合变成了一个字符串对象数组。

以下代码读取fruits.txt文件的内容,并将结果显示在PowerShell控制台上,如下面的屏幕截图所示。

Get-Content .\fruits.txt
Retrieving the text file content using PowerShell Get-Content.

Get-Content读取并将内容存储为数组,但您如何确定这一点呢?首先,将内容保存到一个PowerShell对象中,然后您可以检查以确定其类型。

Save the content into to a object
 $fruits = Get-Content .\fruits.txt
 Display the type of the object
 $fruits.GetType()
 Retrieve the count of items within the object
 $fruits.Count
 Output the contents of the object to the console
 $fruits

查看下面的屏幕截图,$fruits变量是一个包含十个对象的数组。每个对象代表一行文本。

Confirming that the text file content is stored as an array.

从文本文件返回特定行

在前面的示例中,您已经了解到默认的Get-Content结果是一个数组或对象的集合。集合中的每个项都对应一个索引号,而PowerShell的索引通常从零开始。

下面的屏幕截图显示字符串数组中有十个项目。该数组将这十个项目从零到九进行了索引。

Showing that the indexed items in the string array start at zero index.

要仅显示第五行内容,您需要指定索引号4,并将其放在方括号中(称为数组表示法)。

(Get-Content .\fruits.txt)[4]

您可能注意到Get-Content命令被括号括起来。这种表示法告诉PowerShell先运行括号中的命令,然后再执行其他操作。

在下面的屏幕截图中,您将看到唯一返回的结果是raspberry,这是索引为4的项目,对应文本文件中的第五行。

Returning a specific line from Get-Content results.

如果您需要获取最后一行的内容怎么办?幸运的是,您不需要知道总行数。而是使用[-1]作为索引,Get-Content将仅显示文件的最后一行。

(Get-Content .\fruits.txt)[-1]

限制Get-Content返回的前几个结果数量

使用TotalCount参数的Get-Content来从文本文件中检索指定数量的行。TotalCount参数接受一个值,表示一个最大值为9,223,372,036,854,775,807

例如,下面的命令读取内容并将结果限制为三个项目。

Get-Content .\fruits.txt -TotalCount 3

正如您所预期的那样,下面的结果仅显示文本文件开头的前三行。

Reading the top three results using the Get-Content command and the TotalCount parameter.

使用PowerShell尾参数从文件末尾返回结果

在前面的示例中,您使用了PowerShell Get-Content cmdlet读取文本文件并限制了前面的结果。使用PowerShell Get-Content也可以实现相反的效果。使用PowerShell Tail参数从文件末尾读取指定数量的行。

下面的示例代码读取文本文件并显示底部四行的内容。

Get-Content .\fruits.txt -Tail 4

运行PowerShell tail命令后,预期结果将被限制为内容的最后四行,如下图所示。

Getting the results from the end of a file using the Get-Content Tail parameter.

Tail参数通常与Wait参数一起使用。使用Wait参数保持文件打开,并每秒检查新内容。下面的演示显示了TailWait参数的操作。要退出Wait,请使用CTRL+C组合键。

Get-Content -Path .\fruits.txt -Tail 1 -Wait
Using the wait and Tail parameters with Get-Content.

返回结果作为单个字符串

你可能已经注意到在之前的例子中,你一直在处理字符串数组作为PowerShell Get-Content的输出。正如你迄今为止学到的,数组的性质允许你逐项操作内容。

数组通常效果很好,但在替换字符串方面可能会更加困难。Get-ContentRaw参数将文件的整个内容读入单个字符串对象。尽管下面的代码与第一个例子中使用的代码相同,但Raw参数将文件内容存储为单个字符串。

Save the content into to a object
 $fruits = Get-Content .\fruits.txt -Raw
 Display the type of the object
 $fruits.GetType()
 Retrieve the count of items within the object
 $fruits.Count
 Output the contents of the object to the console
 $fruits

下面的截图演示了在Get-Content中添加Raw参数会将内容视为单个字符串而不是对象数组。

Confirming that the Raw parameter of Get-Content reads the file content as a single string object.

一旦你使用Raw参数将文件的内容保存为单个字符串,你可以做什么呢?也许你需要在该文件内容中查找并替换字符串。在下面的示例中,Get-Content将文件的内容读取为单个字符串。然后,使用replace运算符,将一个特定单词替换为另一个。

相关: 查找和替换字符串

# 获取文本文件的原始内容
$fruits = Get-Content .\fruits.txt -Raw
# 显示内容
$fruits
# 查找并替换单词'apricot'为'mango'
$fruits -replace 'apricot','mango'
Reading the content of a text file as a single string and replacing a word using the replace operator.

只从匹配过滤器的文件中读取内容

您是否有一个文件夹里有许多文件,但需要读取其中几个的内容?通过 PowerShell 的 Get-Content,您无需在读取文件内容之前单独筛选文件。 Get-ContentFilter 参数限制了 cmdlet 读取的文件。

为了演示只读取特定文件的内容,首先,在您的工作文件夹中创建一些要读取的文件。如下所示,使用 Add-Content 创建文件。

# 如果不存在,Add-Content 创建 log1.log 和 log2.log 文件,并添加给定的值
Add-Content -Value "This is the content in Log1.log" -Path C:\demo\Log1.log
Add-Content -Value "This is the content in Log2.log" -Path C:\demo\Log2.log
# 验证文件是否已创建
Get-ChildItem C:\demo
Creating test .log files using Add-Content.

创建了测试文件后,使用 FilterPath 参数仅读取根目录中的 .log 文件。在过滤器定义中使用的星号表示让 Get-Content 读取以 .log 结尾的任何文件。路径参数的结束星号限制了只读取根目录中的文件。

Get-Content -Path C:\demo* -Filter *.log

如下所示的输出显示,只显示了来自 .log 文件的内容。

Using the Filter parameter with PowerShell Get-Content to limit the read files.

相关:Get-ChildItem: 作为一个列出文件、注册表、证书等的整体

读取文件的备用数据流

到目前为止,您一直在专门处理文本文件,但Get-Content可以从文件的备用数据流(ADS)中读取数据。随意了解更多有关流的信息,但您可以将流视为存储在典型文件内容旁的另一个数据属性。

备用数据流是Windows NTFS文件系统的一个特性,因此当与非Windows操作系统一起使用时,这不适用于Get-Content

您可以通过使用Stream参数运行Get-Item来查看备用数据流。在使用Stream参数引用文件时,Get-Item会返回一个名为Stream的属性,如下所示。这个默认的文件内容流用:$DATA表示。

为了演示默认的:$DATA流,请使用Get-Item cmdlet来显示文件fruits.txt中所有可用的流。如下所示,Get-Item显示了一个单一的流。

Get-Item -Path .\fruits.txt -Stream *
Listing all available streams in a file using Get-Item.

Get-ContentStream参数明确读取默认的:$DATA流内容,如下所示。返回的内容与默认的Get-Content输出相同,因为默认情况下会读取:$DATA流。

Get-Content -Path .\fruits.txt -Stream ':$DATA'
Explicitly reading the :$DATA stream using Get-Content.

为了演示使用 Get-Content 检索备用数据流,使用 Add-Content 修改文件以添加新流。使用 Get-Item 显示新流以及默认的 :$DATA 流,如下例所示。

# 向 fruits.txt 文件添加名为 Secret 的新 ADS
Add-Content -Path .\fruits.txt -Stream Secret -Value 'This is a secret. No one should find this.'
Get-Item -Path .\fruits.txt -Stream *
Adding the Secret alternate data stream using Add-Content and displaying the new stream with Get-Item.

由于默认情况下只读取 :$DATA 流,因此使用 Get-ContentStream 参数检索新的 Secret 流内容。如下所示,显示了 Secret 流内容而不是默认文件内容。

Get-Content -Path .\fruits.txt -Stream secret
Using Get-Content to read the Secret alternate data stream content.

使用 PowerShell 的下一步 Get-Content

在本文中,您已经学会了许多使用 Get-Content 读取和操作内容的方法。您甚至已经了解到 Get-Content 足够灵活,可以从备用数据流中读取内容!

根据本文所学,您还可以如何在工作中使用 Get-Content?也许您可以使用 Get-Content 判断备份文件是否过时,并触发自动调用运行备份作业?

Source:
https://adamtheautomator.com/powershell-get-content/