PowerShell Foreach循环在多次成功尝试后未执行

Chr*_*ris 1 xml powershell foreach

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

代码工作PowerShell ISE,然后我尝试了它PowerShell Terminal,它不再适用于任何一个.

当我执行时function,它会询问并存储参数,但主循环不会执行(在第23行的注释中).在调试时,我看到foreach完全跳过了循环.要使foreach循环执行,我需要做什么?

我试过的事情:

  • 将param移到函数外面
  • 在函数声明之前或之后调用函数(即,在脚本的顶部或底部
  • 删除检查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
    }
}
Run Code Online (Sandbox Code Playgroud)

因为在这里添加测试文件会导致浏览器崩溃,所以我提供了可以下载必要参数文件的链接:

基准:通用操作系统STIG CCI矩阵:DISA FSO CCI

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

vrd*_*dse 5

$files永远不会设置变量,因此它必须是$null(emtpy).该foreach-loop尝试去通过在每一个元素$files,这不算什么.循环不是真的跳过,没有什么可以迭代.

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

$files = Get-ChildItem -Path $BenchMarksDir -File
Run Code Online (Sandbox Code Playgroud)