Powershell ScriptBlock 关闭:我错过了什么吗?

Powershell ScriptBlock closure: am I missing something?

我已经为此苦苦挣扎了几个小时,在阅读了许多关于 ScriptBlocks、闭包、范围等的线程之后,我仍然看不出我的代码有什么问题。

让我解释一下:我有一个主脚本,它使用 PSWriteHTML 模块和 ScriptBlocks 动态生成一个 HTML 页面。

因为我有很多PSWriteHTML页面要写,所以我使用了一个ScriptBlocks的arrayList来生成每次具有不同值集的代码(对应不同的服务器),这些ScriptBlocks被执行到foreach 循环。

这是使用Save-utilizationReport函数完成的(我只保留了相关代码):

function Save-utilizationReport ($currentDate, $navLinksScriptBlock, $htmlScriptBlockArray, $emeaTotalNumberOfCalls, $namTotalNumberOfCalls, $apacTotalNumberOfCalls, $path, $logFilePath) {
    
    [...]

    # Using Script Blocks, add the pages generated during the analysis to the HTML Report
    foreach($htmlScriptBlock in $htmlScriptBlockArray){
        Invoke-Command -ScriptBlock ($htmlScriptBlock)
    }

    [...]
}

ScriptBlock 是使用从服务器日志列表中收集的一组值创建的,并添加到 Create-utilizationReportPage 函数中的 ScriptBlocks arrayList(同样,我已经只保留相关代码):

function Create-utilizationReportPage ($matchedLines, $ipAddress, $hostname, $pageId, $utilisationReportTemplate, $htmlScriptBlockArray) {

# Retrieve the content of the Utilisation Report Template as a RAW string (Here-String)
$htmlPageCodeBlock = Get-Content $utilisationReportTemplate -Raw

# Create the Script Block that contains the HTML page
$htmlPageScriptBlock = {
    
    # Get the "Total number of calls" information
    $timeArray = $matchedLines.time
    $participantNumberArray = $matchedLines.participantNumber

    [...]
    
    # Update the page ID in the template
    $htmlPageCodeBlock = $htmlPageCodeBlock -replace '%PAGE_ID%', "$pageId"
    
    # Update the page header information in the template
    $htmlPageCodeBlock = $htmlPageCodeBlock -replace '%PAGE_HEADER%', "$ipAddress [$hostname]"
    
    Invoke-Command -ScriptBlock ([scriptblock]::Create($htmlPageCodeBlock))
}.GetNewClosure()

# Add the Page's script block to the Script Blocks array
$htmlScriptBlockArray.Add($htmlPageScriptBlock)
}

这些在下面的脚本中被调用:

$currentDate = Get-CorrectDate $latestFolder

# The global Log file
$logFilePath = "$scriptPath/Logs/logs_$currentDate.txt"

$serversList = "$scriptPath/Config/$configFileName"

# If the Servers list exists, retrieve the Servers list
if (Test-Path $serversList) {
    # Get the data from the file
    [xml]$servers = Get-Content $serversList

    # Select only the Servers information
    $nodes = $servers.SelectNodes("//server")
        
    try {
    
        [...]
    
        # Iterate through the Servers list
        foreach ($node in $nodes) {
        
            # Get the Server IP Address
            $ipAddress = $node.ip
            
            # Get the Server Hostname
            $hostname = $node.hostname
            
            # Get the "Debug Utilization" lines from the logbundle's syslog files
            $matchedLines = [System.Collections.ArrayList]@(Find-UtilizationLinesInLogs $currentDate "$scriptPath\Data$currentDate\logs_$ipAddress" "host:server:  \[USAGE\] : \{`"1`" : ")[-1]

            # Add the Server's participants throughout the day
            switch ($hostname) {
                {$_.Contains("emea")} {
                    # Update EMEA total number of participants
                    Update-ZoneTotalParticipants ([ref]$emeaServer) ([ref]$emeaTotalNumberOfCalls) $matchedLines
                }
                {$_.Contains("nam")} {
                    # Update NAM total number of participants
                    Update-ZoneTotalParticipants ([ref]$namServer) ([ref]$namTotalNumberOfCalls) $matchedLines
                }
                {$_.Contains("apac")} {
                    # Update APAC total number of participants
                    Update-ZoneTotalParticipants ([ref]$apacServer) ([ref]$apacTotalNumberOfCalls) $matchedLines
                }
            }
            
            # Get the CPU Utilization values lines from the logbundle's sysdebug files
            $cpu = Get-CpuUsage "$scriptPath\Data$currentDate\logs_$ipAddress\sysdebug"

            # Get the Memory Utilization values lines from the logs' sysdebug files
            $memory = Get-MemoryUsage "$scriptPath\Data$currentDate\logs_$ipAddress\sysdebug"

            # Export the "Debug Utilization" to a CSV file
            Save-csvUtilizationReport $matchedLines "$scriptPath\Output$currentDate" "$ipAddress" "$hostname" $logFilePath

            # Draw graphs from the "Debug Utilization" information and then export it to an HTML File
            Create-utilizationReportPage $matchedLines "$ipAddress" "$hostname" $pageId $utilisationReportTemplate $htmlScriptBlockArray
            
            # Add the new navigation link to the Navigation Links array
            Add-htmlNavLink $navLinksArray "$ipAddress" "$hostname" $pageId $logFilePath
            
            # Increment the Page ID counter
            $pageId += 1
        }
    
        # Create an Here-String from the Navigation Links array
        $OFS = ""
        $navLinksCode =@"
        $($navLinksArray)
"@
        $OFS = " "

        # Create a script block from the Navigation Links
        $navLinksScriptBlock = [scriptblock]::Create($navLinksCode)

        # Save the daily HTML utilization report
        Save-utilizationReport $currentDate $navLinksScriptBlock $htmlScriptBlockArray $emeaTotalNumberOfCalls $namTotalNumberOfCalls $apacTotalNumberOfCalls "$scriptPath\Output$currentDate" $logFilePath
    }
    catch
    {
        Write-Logs $logFilePath "Error: $($_.Exception.Message)"
        
        exit 1
    }
}

除了第一页中的第一组值不知何故是所有其他组值的总和外,一切正常并符合预期...

例如,当我有 3 个页面时,当执行 Find-UtilizationLinesInLogs 函数时,我可以看到收集的值是正确的:

  1. matchedLines.participantNumber: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 5 5 5 4 4 4 4 4 12 14 14 15 16 16 16 7 7 7 7 8 14 17 18 19 18 19 19 20 16 16 16 15 7 7 7 7 5 4 4 4 4 4 4 4 4 1 1 0 0 0 3 6 5 5 5 5 9 14 16 18

  2. matchedLines.participantNumber: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 8 9 10 10 10 10 9 9 9 9 9 9 8 8 8 8 8 8 8 8 8 8 9 4 12 15 11 14 14 13 12 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0

  3. matchedLines.participantNumber: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 2 0 0 0 3 9 10 10 9 8 10 9 9 10 10 10 10 10 11 11 11 11 8 7 8 8 7 7 7 7 7 7 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 4 16 17 16 16

但是当在 foreach 循环中使用 Invoke-Command 执行 ScriptBlocks 时,第一批值系统地是总和3 组值中的一个,而下列值是正确的:

  1. matchedLines.participantNumber 在 Create-utilizationReportPage 中: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 6 6 7 7 5 5 5 8 29 33 34 34 34 36 34 25 26 26 26 27 32 36 37 38 37 35 34 36 32 31 32 26 26 29 25 22 20 18 17 17 17 6 6 6 6 3 23 2 8 2 7 7 7 10 25 31 32 34

  2. matchedLines.participantNumber 在 Create-utilizationReportPage 中: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 8 9 10 10 10 10 9 9 9 9 9 9 8 8 8 8 8 8 8 8 8 8 9 4 12 15 11 14 14 13 12 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0

  3. matchedLines.participantNumber 在 Create-utilizationReportPage 中: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 2 0 0 0 3 9 10 10 9 8 10 9 9 10 10 10 10 10 11 11 11 11 8 7 8 8 7 7 7 7 7 7 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 4 16 17 16 16

我已经尝试了很多事情都没有成功,所以如果有人能提示可能会出错的地方,那就太好了!

感谢您的帮助!

所以!终于知道哪里不对了

我怀疑我的问题与 arrayList 副本或至少可变副本有关...所以我尝试删除提取 [=34] 的脚本的这一部分=]$macthedLines 值并使用引用将它们复制到全局数组中:

            # Add the Server's participants throughout the day
            switch ($hostname) {
                {$_.Contains("emea")} {
                    # Update EMEA total number of participants
                    Update-ZoneTotalParticipants ([ref]$emeaServer) ([ref]$emeaTotalNumberOfCalls) $matchedLines
                }
                {$_.Contains("nam")} {
                    # Update NAM total number of participants
                    Update-ZoneTotalParticipants ([ref]$namServer) ([ref]$namTotalNumberOfCalls) $matchedLines
                }
                {$_.Contains("apac")} {
                    # Update APAC total number of participants
                    Update-ZoneTotalParticipants ([ref]$apacServer) ([ref]$apacTotalNumberOfCalls) $matchedLines
                }
            }

还有宾果游戏,这次第一个 PSWriteHTML 页面中记下的值是正确的!

所以我专注于 Update-ZoneTotalParticipants 函数,它正在执行值复制:

function Update-ZoneTotalParticipants ([ref][int]$ServerNumber, [ref][System.Collections.ArrayList]$totalNumberOfCalls, $values) {
    # If this is the first server to be analysed in the zone
    if ($ServerNumber.value -eq 1) {

        # Copy the Utilization lines of the server
        $totalNumberOfCalls.value = $values
        
        # Increment the zone's server counter
        $ServerNumber.value += 1
    }
    # If this at least the 2nd server to be analysed in the zone
    elseif ($ServerNumber.value -gt 1) {

        # Parse the server matched lines, get the participantNumber value and add it to the total
        0..($totalNumberOfCalls.value.Count - 1) | ForEach-Object {
            if ($_ -le ($values.Count - 1)) {
                $totalNumberOfCalls.value[$_].participantNumber = [int]($totalNumberOfCalls.value[$_].participantNumber) + [int]($values[$_].participantNumber)
            }
            # If there are less objects in the current server matched lines, add a 0 instead
            else {
                $totalNumberOfCalls.value[$_].participantNumber = [int]($totalNumberOfCalls.value[$_].participantNumber) + 0
            }
        }
    }
}

代码中唯一涉及 $matchedLines 的部分是 $totalNumberOfCalls.value = $values 所以肯定是数组操作出错了。

所以我研究了 ArrayList 副本或克隆,发现我没有对对象进行深度复制,这可能会导致问题。

我使用了Petru Zaharia的解决方案来更新函数:

        # Copy the Utilization lines of the server
        $totalNumberOfCalls.value = $values

# replaced with:

        # Copy the Utilization lines of the server : Serialize and Deserialize data using PSSerializer:
        $_TempCliXMLString = [System.Management.Automation.PSSerializer]::Serialize($matchedLines, [int32]::MaxValue)
        $totalNumberOfCalls.value = [System.Management.Automation.PSSerializer]::Deserialize($_TempCliXMLString)

现在一切正常。

感谢大家的支持!