【问题标题】:Powershell -- Array Casting Type IssuePowershell - 数组转换类型问题
【发布时间】:2011-04-22 21:00:30
【问题描述】:

并提前感谢您查看。 我在 Powershell 中编写的脚本中遇到问题。下面的脚本有点草率,请见谅。

基本上,此脚本从文本文件目录中获取输入。每个文件中都有一行,结构如下:

GlobalPath, AgencyPath,SitePath,SizeofSite(以字节为单位)

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

有问题的行是:

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

这会导致以下错误:

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

所以我的问题是:数组是否被错误地转换了?既然是报告 [System.Char] 而不是 [System.String]?

如果我输入的文件有两行,则不会导致此错误。如果文件只有一行,则将其转换为 [System.Char]。

:完整脚本:

# 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
}

【问题讨论】:

    标签: arrays scripting programming-languages powershell


    【解决方案1】:

    这是 Powershell 中的一个已知陷阱。只需将Get-Content 包装在数组“表达式”@() 中:

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

    【讨论】:

    • 完美无瑕。非常感谢。
    【解决方案2】:

    我认为您遇到的问题是 get-content 不返回字符串数组(行),而是一个字符串。因此,当您查看 $dailybuffer[k] 时,您看到的是字符串的第 k 个字符,而不是第 k 行。

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题,只有像下面这样的行解决了这个问题。对于您的问题,它将是:

      [string[]] $DailyBuffer = Get-Content $file.pspath
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-25
        • 1970-01-01
        • 2011-11-17
        相关资源
        最近更新 更多