目前我正在从XML文件加载一些数据以配置我的脚本运行的一些参数,但是$keywords
和$maxCounts
在发送时没有获得正确的值到CheckForKeywords
函数
function Main()
{
#### VARIABLES RELATING TO THE LOG FILE
#contains the log path and log file mask
$logPaths = @()
$logFileMasks = @()
# key value pair for the strings to match and the max count of matches before they are considered an issue
$keywords = @(,@())
$maxCounts = @(,@())
#### FUNCTION CALLS
LoadLogTailerConfig $logConfigPath ([ref]$logPaths) ([ref]$logFileMasks) ([ref]$keywords) ([ref]$maxCounts)
for ($i = 0; $i -lt $logPaths.Count; $i++)
{
$tail = GetLogTail $numLinesToTail $logPaths[$i] $logFileMasks[$i]
$tailIssueTable = CheckForKeywords $tail $keywords[$i] $maxCounts[$i]
}
}
# Loads in configuration data for the utility to use
function LoadLogTailerConfig($logConfigPath, [ref]$logPaths, [ref]$logFileMasks, [ref]$keywords, [ref]$maxCounts)
{
Write-Debug "Loading config file data from $logConfigPath"
[xml]$configData = Get-Content "C:\Testing\Configuration\config.xml"
foreach ($log in $configData.Logs.Log) {
$logPaths.Value += $log.FilePath
$logFileMasks.Value += $log.FileMask
$kwp = @()
$kwc = @()
foreach ($keywordSet in $log.Keywords.Keyword)
{
$kwp += $keywordSet.Pattern
$kwc += $keywordSet.MaxMatches
}
$keywords.Value += $kwp
$maxCounts.Value += $kwc
}
}
# Returns body text for email containing details on keywords in the log file and their frequency
function CheckForKeywords($tail, $keywords, $maxCounts)
{
$issuesFound = 0
for ($i = 0; $i -lt $keywords.Count; $i++)
{
$keywordCount = ($tail | Select-String $keywords[$i] -AllMatches).Matches.Count
Write-Debug $keywords.Count
Write-Debug (("Match count for {0} : {1}" -f $keywords[$i], $keywordCount))
if ($keywordCount -gt $maxCounts)
{
#do stuff
}
}
return ""
}
Main
答案 0 :(得分:1)
你所做的不是二维数组,它是一个嵌套数组。通过写作
$keywords = @(,@())
$maxCounts = @(,@())
您正在创建一个包含一个元素的数组。此元素是一个零元素的数组。你不需要那个。所以将上面改为:
$keywords = @()
$maxCounts = @()
现在,当你这样做时:
$keywords.Value += $kwp
$maxCounts.Value += $kwc
powershell在右侧展开数组,并按元素连接元素,左侧是数组。这不是你想要的。所以将其更改为
$keywords.Value += @(,$kwp)
$maxCounts.Value += @(,$kwc)
在旁注中使用ref参数在powershell中不是惯用的,并且它不容易获得该方法的帮助。我建议改变你的功能,通过管道传递结果,powershell方式,将来支持你的脚本的人会很感激。您可以为要在对象上作为属性返回的不同类型的值建模,并返回该对象。祝你好运。