【问题标题】:PowerShell Foreach Loop Not Executing after multiple successful attempts多次成功尝试后,PowerShell Foreach 循环未执行
【发布时间】:2018-05-28 23:03:11
【问题描述】:

我创建了以下 PowerShell 函数来遍历用户指定目录中的文件,将 DISA FSO 提供的 CCI 值与目录中每个 STIG 的测试 ID 结合起来,并将该数据输出到.csv 用户选择的文件。

代码在PowerShell ISE 中运行,然后我在PowerShell Terminal 中尝试了它,但它不再适用于任何一个。

当我执行function 时,它会请求并存储参数,但是主循环不会执行(下面第 23 行的注释)。在调试时,我看到foreach 循环完全被跳过了。我需要做什么才能使 foreach 循环执行?

我尝试过的事情:

  • 将参数移到函数外
  • 在函数声明之前或之后调用函数(即在脚本的顶部或底部)
  • 删除检查if用户指定的输出文件是否存在
  • 在输出文件存在后添加变量检查以显示参数(这会显示 -- 跳过之后的任何内容)

当前功能状态:

Function CreateTestPlan {
    param (
        [Parameter(Mandatory = $true, HelpMessage="Filename of DISA STIG Benchmark XCCDF.xml file. Downloaded from IASE website. Usage: -BenchMarksDir")]
        [string]$BenchMarksDir,
        [Parameter(Mandatory = $true, HelpMessage="Filename of DISA CCI .XML file. Downloaded from IASE website. Usages: -CCIFile")]
        [string]$CCIFile,
        [Parameter(Mandatory = $true, HelpMessage="Filename of your choosing, ending in .csv. Usages: -OutFile")]
        [string]$OutFile,
        [Parameter(Mandatory = $true, HelpMessage="Determines output of control numbers and selection. Usages: -CCIFilter + NIST SP 800-53, NIST SP 800-53 Revision 4, NIST SP 800-53A")]
        [ValidateSet("NIST SP 800-53","NIST SP 800-53 Revision 4","NIST SP 800-53A")]
        [string]$CCIFilter
    )

    if (![System.IO.File]::Exists($OutFile))
    {
        New-Item -ItemType file $OutFile -EA Stop
    }
    ElseIf([System.IO.File]::Exists($OutFile))
    {
        Clear-Content $OutFile -EA Stop
    }

    Foreach ($file in $files) #Loop does not execute
    {
        [xml]$Stigx = Get-Content -Path $file.FullName -EA Stop
        [xml]$CCIx = Get-Content -Path $CCIFile -EA Stop

        # start by parsing the xccdf benchmark
        if($Stigx){
            $StigCollection = @()
            # loop through the xccdf benchmark collecting data into an object collection
            $StigName = $Stigx.Benchmark.title
            #loop through each group in the stig
            foreach ($group in $StigX.Benchmark.Group){
                # create a new PSObject collecting and stripping out as required.
                $STIG = New-Object -TypeName PSObject -Property ([ordered]@{
                    GroupID = $group.id
                    RuleTitle = $group.Rule.title 
                    Severity = $group.Rule.severity
                    VulnerabilityDetails = $($($($group.Rule.description) -split '</VulnDiscussion>')[0] -replace '<VulnDiscussion>', '')
                    Check = $group.Rule.check.'check-content'
                    Fix = $group.Rule.fixtext.'#text'
                    ControlIdentifier = $group.Rule.ident.'#text' -join "`r`n"
                    Control = $null # control is null as it will be added from the CCI List
                    StigName = $StigName
                })
                $StigCollection += $STIG
           }# close foreach
        }# close if
        # loop through the Stig Collection updating the Control information pulled from the U_CCI_List.xml
        foreach($StigObj in $StigCollection){
            foreach($CciItem in $CCIX.cci_list.cci_items.cci_item){
                if($CciItem.Id -EQ $StigObj.ControlIdentifier){
                    # filter the control version by the title
                    if($CciItem.references.reference.title -EQ $CCIFilter){
                        $StigObj.Control = $CciItem.references.reference.index -join "`r`n"
                    }
                }
            }
        }
        $StigCollection | Select-Object -Property 'StigName', 'GroupID', 'Control', 'Check' | Export-Csv $OutFile -Append -NoTypeInformation
    }
}

由于在此处添加测试文件导致我的浏览器崩溃,我提供了可以下载必要参数文件的链接:

基准:General Operating System STIG CCI 矩阵:DISA FSO CCI

示例用法和没有收到错误:

【问题讨论】:

    标签: xml powershell foreach


    【解决方案1】:

    变量$files 从未设置,因此它必须是$null (emtpy)。 foreach-loop 试图遍历$files 中的每个元素,这没什么。循环并没有真正跳过,没有什么可以迭代的。

    如果您想遍历$BenchMarksDir 中的所有文件,则必须首先枚举其中的所有文件。

    $files = Get-ChildItem -Path $BenchMarksDir -File
    

    【讨论】:

    • 感谢您的宝贵时间。我觉得我曾经有过那行,一定是不小心删除了。
    • 还要感谢 George Rolston 在他的帖子中提供的 for 循环逻辑:entelechyit.com/2017/01/02/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 2017-11-24
    • 1970-01-01
    • 2018-11-16
    • 2011-11-11
    • 1970-01-01
    相关资源
    最近更新 更多