简体   繁体   中英

Index was outside the bounds of the array error when processing .svd file

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

I have a script which masked certain fields in a data file (fields seperated by '|'). I have done a lot of these and they work fine. However, when I copy this exact file to another folder to use a different data file (.svd) I get the "Index was outside the bounds of the array", I've seen a few other threads on this but none really answer my query.

Curious as to why it still works in a particular folder location but not the other (I am changing the input and output urls to direct to the new folder).

Error below:

Index was outside the bounds of the array.
AtMYURL-Masked-Fields.ps1:28 char:3
+         $SplitString[3] = $mask30                                                    #To Fax Name
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], IndexOutOfRangeException
    + FullyQualifiedErrorId : System.IndexOutOfRangeException

Answer provided by @lit

"There is at least one record starting with STARTDELIV that does not have enough fields. find /I "STARTDELIV" "MYURL\\ABS.SVD" "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM