PowerShell 将文本文件数据读取到数组中

文本数据更常见的存储格式之一是采用文件形式,其中单独的行被视为不同的数据元素。 Get-Content cmdlet 可用于一步读取整个文件,如下所示:

PS E:/> Get-Content -Path E:/wamp64/changelog.txt
Changelog
--- Wampserver 3.1.9
- Fix VirtualHost creation issue (CVE-2019-11517). Thanks to Imre Rad
- Warning if Apache variables not found
- Improving the verification of the status of services
- Support VirtualHost IDNA (Internationalized Domain Names in Applications) ServerName
- Possibility to backup hosts file before modification (Wamp settings)
- Non-systematic rewriting of some files
To use or unactive MariaDB, read the file mariadb_mysql.txt
This update is cumulative Updates 3.0.1 to 3.1.9. It may be reinstalled in case of modified files by error

--- Wampserver 3.1.8
- Use of PHPIniDir "${APACHE_DIR}/bin" in httpd.conf
No more warnings if the PATH environment variable contains a path on Wamp or a version of PHP.
- Update of spanish language files - Thank to Raphy

--- Wampserver 3.1.7
- Replace integrated local base64 images by img/xxx.yyy
- Don't change vhosts port with apache version switching

--- Wampserver 3.1.6
- Verify Apache Define's. Add Define SRVROOT if not exist
- Don't use dnscache service anymore
- Do not create symlink for libcrypto-1_1.dll, libssl-1_1.dll,
  libcrypto-1_1-x64.dll libssl-1_1-x64.dll
- New folder : wamp(64)/bin/apache/original_libcrypto_libssl/
  containing the original libcrypto and libssl files above
  for Apache versions 2.4.26 to 2.4.37 32 & 64 bit
- Preparation to support PHP 7.3

Get-Content 已将从文件读取的数据视为数组,其中每行文件内容视为一个元素。 可以通过检查返回的内容的长度来确认此点:

PS E:/> (Get-Content -Path E:/wamp64/changelog.txt).Length
157

此命令对于直接从 Windows PowerShell 获取列表信息最为有用。 例如,可能会在文件 C:/temp/domainMembers.txt 中存储计算机名称或 IP 地址的列表,其中文件的每一行上一个名称。 你可以使用 Get-Content 来检索文件内容并将它们放在变量 $Computers 中:

$Computers = Get-Content -Path C:/temp/DomainMembers.txt

$Computers 现在是一个数组,其中每个元素包含一个计算机名。

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/264554.html

(0)
上一篇 2022年6月7日
下一篇 2022年6月7日

相关推荐

发表回复

登录后才能评论