我正在尝试使用 powershell 读取 txt 文件,仅读取某些字符
$TxtFilePath = "C:\Users\myname\mycode\mydata.txt"
$TxtData = Get-Content $TxtFilePath | ForEach-Object {
# Ensure the line is long enough before extracting substrings
$Line = $_.PadRight(138) # Pad the line to ensure it has at least 138 characters
$entry = @{
LastName = $Line.Substring(18, 40).Trim()
FirstName = $Line.Substring(58, 40).Trim()
Birthdate = $Line.Substring(128, 10).Trim()
}
[PSCustomObject]$entry
}
第 N 个字符到第 N 个字符是对应值的空格(尽管有时例如名字只有 5 个字符,但 18-58 个字符是为姓氏准备的)。当我运行该代码时,错误是“”值无法转换为“System.Management.Automation.LanguagePrimitives+InternalPSCustomObject”类型。此语言模式仅支持核心类型。“””
它引用了这一行“”“[PSCustomObject]$entry””将不胜感激任何线索
我确实希望它可以按照规则读取 txt 文件。