gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 00:34:12 27 4
gpt4 key购买 nike

我创建了以下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

    用法示例,无错误:
    No Errors and Example Usage

    最佳答案

    永远不要设置$files变量,因此它必须是$null(emtpy)。 foreach -loop尝试遍历$files中的每个元素,什么都没有。循环并没有真正跳过,没有什么可重复的。

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

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

    关于xml - 多次成功尝试后未执行PowerShell Foreach循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47820428/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com