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

[玩转系统] PowerShell Get-Content 与 PowerShell Tail 等效项

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

PowerShell Get-Content 与 PowerShell Tail 等效项


通过自动化,从文本文件读取数据是一种常见的情况。大多数编程语言都至少有一种读取文本文件的方法,PowerShell 也不例外。 PowerShell Get-Content cmdlet(PowerShell tail 等效项)读取文本文件的内容并将数据导入到 PowerShell 会话中。

当您需要使用文本文件作为脚本的输入时,PowerShell Get-Content cmdlet 是不可或缺的工具。也许您的 PowerShell 脚本需要读取计算机列表来监视或导入电子邮件模板以发送给您的用户。 PowerShell Get-Content 轻松支持这些场景!

实时跟踪日志文件怎么样?是的,PowerShell Get-Content 也可以做到这一点!继续阅读本文,您将了解如何使用 Get-Content cmdlet 在 PowerShell 中读取文本文件。

先决条件

如果您有兴趣遵循本教程中的示例,您将需要满足以下要求。

  • 您需要一台运行 Windows 10 的计算机。本教程使用 Windows 10 版本 20H2。但不用担心,您使用的 Windows 10 版本不会有问题。
  • 您的计算机上至少应安装 Windows PowerShell 5.1 或 PowerShell 7.1。这里使用的是 PowerShell 7.1,但是任何一个版本都可以工作!
  • 您将编写和测试命令,因此您需要一个代码编辑器。推荐的编辑器是 Windows 内置的 Windows PowerShell ISE 和 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

[玩转系统] PowerShell Get-Content 与 PowerShell Tail 等效项

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 变量是一个包含十个对象的数组。每个对象代表一行文本。

[玩转系统] PowerShell Get-Content 与 PowerShell Tail 等效项

从文本文件返回特定行

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

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

[玩转系统] PowerShell Get-Content 与 PowerShell Tail 等效项

要仅显示第五行内容,您需要指定索引号 4,并用方括号括起来(称为数组表示法)。

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

您可能会注意到 Get-Content 命令包含在括号中。此表示法告诉 PowerShell 在执行其他操作之前先运行括号中的命令。

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

[玩转系统] PowerShell Get-Content 与 PowerShell Tail 等效项

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

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

限制 Get-Content 返回的热门结果的数量

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

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

Get-Content .\fruits.txt -TotalCount 3

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

[玩转系统] PowerShell Get-Content 与 PowerShell Tail 等效项

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

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

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

Get-Content .\fruits.txt -Tail 4

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

[玩转系统] PowerShell Get-Content 与 PowerShell Tail 等效项

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

Get-Content -Path .\fruits.txt -Tail 1 -Wait

[玩转系统] PowerShell Get-Content 与 PowerShell Tail 等效项

将结果作为单个字符串返回

您可能已经注意到,在前面的示例中,您一直将字符串数组作为 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

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

[玩转系统] PowerShell Get-Content 与 PowerShell Tail 等效项

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

相关: 查找和替换字符串

# Get the raw content of the text file
$fruits = Get-Content .\fruits.txt -Raw
# Display the content
$fruits
# Find and replace the word 'apricot' with 'mango'
$fruits -replace 'apricot','mango'

[玩转系统] PowerShell Get-Content 与 PowerShell Tail 等效项

仅读取与过滤器匹配的文件中的内容

您是否有一个装满文件的文件夹,但需要读取选定的几个文件的内容?使用 PowerShell Get-Content,您无需在读取文件内容之前单独过滤文件。 Get-ContentFilter 参数限制 cmdlet 读取的文件。

为了演示仅读取选定文件的内容,首先,创建几个要读取的文件。如下所示,使用 Add-Content 在工作文件夹中创建文件。

# Add-Content creates the log1.log and log2.log file if they don't exist already and adds the given value
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
# Verify that the files have been created
Get-ChildItem C:\demo

[玩转系统] PowerShell Get-Content 与 PowerShell Tail 等效项

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

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

如下面的输出所示,仅显示 .log 文件中的内容。

[玩转系统] PowerShell Get-Content 与 PowerShell Tail 等效项

相关: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 *

[玩转系统] PowerShell Get-Content 与 PowerShell Tail 等效项

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

Get-Content -Path .\fruits.txt -Stream ':$DATA'

[玩转系统] PowerShell Get-Content 与 PowerShell Tail 等效项

要演示使用 Get-Content 检索备用数据流,请使用 Add-Content 修改文件以添加新流。使用 Get-Item 在默认 :$DATA 流旁边显示新流,如以下示例所示。

# Add a new ADS named Secret to the fruits.txt file
Add-Content -Path .\fruits.txt -Stream Secret -Value 'This is a secret. No one should find this.'
Get-Item -Path .\fruits.txt -Stream *

[玩转系统] PowerShell Get-Content 与 PowerShell Tail 等效项

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

Get-Content -Path .\fruits.txt -Stream secret

[玩转系统] PowerShell Get-Content 与 PowerShell Tail 等效项

PowerShell 的后续步骤 Get-Content

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

根据您在本文中学到的知识,您还可以在工作中使用哪些其他方式使用 Get-Content?也许您可以使用 Get-Content 来确定备份文件是否已过期并触发自动调用来运行备份作业?

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

取消回复欢迎 发表评论:

关灯