【问题标题】:Index was outside the bounds of the array error when processing .svd file处理 .svd 文件时,索引超出数组错误范围
【发布时间】:2018-04-19 16:50:25
【问题描述】:
clear

# Powershell scripts used to mask Private Personal Information on data files.

# path to PERM file
$path = "MYURL\ABS.SVD"

# get file content
$content = Get-Content $path

$content | ForEach-Object {
    $mask50 = 'X' * 50   # variables to makes fields by letter according to length
    $mask30 = 'Y' * 30
    $mask20 = 'A' * 20
    $mask08 = 'Z' * 8
    $mask05 = 'B' * 5
    $mask16 = 'C' * 16

    # if statement, if the string is at position 0-10, and begins with
    # 'STARTDELIV' then run replace statement
    if ($_.Substring(0,10) -eq 'STARTDELIV') {
        $SplitString = $_.Split('|')

        $SplitString[3] = $mask30    # Fields : To Fax Name
        $SplitString[4] = $mask20    # Fields : To fax Number
        $SplitString[9] = $mask50    # Fields : To EMail Address

        $SplitString -join '|'
    } else {
        $_
    }
} | Out-File "MYURL\ABS-Output.svd" -Encoding ASCII

我有一个脚本,它掩盖了数据文件中的某些字段(由“|”分隔的字段)。我已经做了很多这些,它们工作得很好。但是,当我将这个确切的文件复制到另一个文件夹以使用不同的数据文件(.svd)时,我得到“索引超出了数组的范围”,我已经看到了其他一些线程,但没有一个真正回答我查询。

很好奇为什么它仍然可以在特定文件夹位置工作,但不能在另一个位置工作(我正在更改输入和输出 url 以指向新文件夹)。

以下错误:

指数数组的边界之外。 AtMYURL-Masked-Fields.ps1:28 char:3 + $SplitString[3] = $mask30 #To Fax Name + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (:) [], IndexOutOfRangeException + FullyQualifiedErrorId : System.IndexOutOfRangeException

【问题讨论】:

  • 更新:脚本运行成功,字段被屏蔽,但我仍然在控制台中收到错误?
  • 检查 $SplitString 数组的计数。如果它小于 9,那么你正在引用数组之外的东西。也许不是文件中的所有记录都有足够的字段。
  • 错误非常明显。无论您认为的情况如何,至少有一行的字段少于 4 个
  • 至少有一条以 STARTDELIV 开头的记录没有足够的字段。 find /I "STARTDELIV" "MYURL\ABS.SVD"
  • Select-String -Path "MYURL\ABS.SVD" -Pattern "STARTDELIV"

标签: powershell


【解决方案1】:

@lit 提供的答案

"至少有一条以 STARTDELIV 开头的记录没有足够的字段。find /I "STARTDELIV" "MYURL\ABS.SVD" "

【讨论】:

    猜你喜欢
    • 2022-01-22
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多