我创建了以下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
}
}
因为在这里添加测试文件会导致我的浏览器崩溃,所以我提供了可以下载必要参数文件的链接:
答案 0 :(得分:5)
永远不会设置变量$files
,因此它必须是$null
(emtpy)。 foreach
- 循环尝试遍历$files
中的每个元素,这都不是。循环不是真的被跳过,没有什么可以迭代。
如果要遍历$BenchMarksDir
中的所有文件,则必须首先枚举其中的所有文件。
$files = Get-ChildItem -Path $BenchMarksDir -File