Search code examples
arraysscriptingprogramming-languagespowershell

Powershell -- Array Casting Type Issue


and thank you in advance for taking a look. I am having an issue in a script I wrote in Powershell. The script below is a little sloppy so please forgive me.

Basically, this script takes input from a directory of text files. Each file has a line in it like so, with the following structure:

GlobalPath, AgencyPath,SitePath,SizeofSite (in bytes)

\\servername\shared, \8055\Single\department, \sitename,524835900000

The line in question is:

    # Split full path and peak usage
    $CalculationBuffer = $DailyBuffer[$k].Split(",")

Which results in the following error:

Method invocation failed because [System.Char] doesn't contain a method named 'Split'.
At D:\script.ps1:387 char:52
+         $CalculationBuffer = $DailyBuffer[$k].Split <<<< (",")
+ CategoryInfo          : InvalidOperation: (Split:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound

So my question: Is the array casted incorrectly? Since it is reporting [System.Char] instead of [System.String]?

If the file I am inputting has two lines, it does not result in this error. If the file has only one line, it gets casted as [System.Char] instead.

:Full Script:

# Monthly Output File
[string]$monthoutfile = $ProgPath + "Billing\" + $monthdate + "\_Master_" + $monthdate + ".log"
[string]$currentmonth = $ProgPath + "Billing\" + $monthdate + "\"

# Define what type of files to look for
$files = gci $currentmonth | Where {$_.extension -eq ".log"}

# Create a datastore\dictionary for this month
$MonthDataDictionary = New-Object 'System.Collections.Generic.Dictionary[string,long]'
$MonthAvgDictionary = New-Object 'System.Collections.Generic.Dictionary[string,long]'
# Arrays
$DailyBuffer = @()
$CalculationBuffer = @()
$TempArray = @()
# Counters\Integers
[int]$Linesinday = 1
[int]$DayCounter = 1
[int]$LineCounter = 0
# Strings
[string]$DailyPath = ""
[string]$Outline = ""
# Longs
[long]$DailyPeak = 0
[long]$Value = 0

##########################################################################
# Begin Loop

# Write once...
#$CalcBuffer += "\"

foreach ($file in $files) 
{

    # First get content from text file and store in buffer
    $DailyBuffer = Get-Content $file.pspath

    # Determine how many lines are in the file, call function
    $Linesinday = linecount $file.pspath

    for ($k = 0; $k -lt $Linesinday; $k++ ) 
    { 

        # Split full path and peak usage
        $CalculationBuffer = $DailyBuffer[$k].Split(",")

        # Store site path
        $DailyPath = $CalculationBuffer[0] + $CalculationBuffer[1] + $CalculationBuffer[2]

        # Store peak usage
        $DailyPeak = $CalculationBuffer[3]

        # Write to dictionary under conditions

        # Check if current path is stored or "Site".
        # If NOT .ContainsKey($DailyPath)
        if (!($MonthDataDictionary.ContainsKey($DailyPath))) {

            # Add Key
            $MonthDataDictionary.Add($DailyPath, $DailyPeak)

        # If it does contain a value
        } elseif ($MonthDataDictionary.ContainsKey($DailyPath)) {

            # Add the value to the current value for averaging
            $MonthDataDictionary.Item($DailyPath) += $DailyPeak

        }
    }

    # Accumulator
    $DayCounter ++

}        

# Now that each file is tallied up, run an average calculation
$MonthDataDictionary.getenumerator() | Foreach-Object -process {
    $Value = $_.Value / $DayCounter
    $MonthAvgDictionary.Add($_.Key, $Value)

}

# Debug:
# Write-Host the values
$MonthAvgDictionary

# Output the "Average Peak" values to a file
$MonthAvgDictionary.getenumerator() | Foreach-Object -process {

        # Construct output line
        $OutLine = $_.Key + "," + $_.Value
        $OutLine >> $MonthOutFile
}

Solution

  • This is a known pitfall in Powershell. Just wrap the Get-Content in an array "expression" @() :

    $DailyBuffer = @(Get-Content $file.pspath)