并提前感谢你看一看。 我在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
}
答案 0 :(得分:5)
这是Powershell中众所周知的陷阱。只需将Get-Content
包装在数组“expression”@()
:
$DailyBuffer = @(Get-Content $file.pspath)
答案 1 :(得分:0)
我认为你遇到的问题是get-content不返回字符串数组(行),而是返回单个字符串。因此,当你查看$ dailybuffer [k]时,你正在查看字符串的第k个字符,而不是第k行。
答案 2 :(得分:0)
我有同样的问题,只有像下面这样的行解决了这个问题。对于你的问题,它将是:
[string[]] $DailyBuffer = Get-Content $file.pspath