I've created the following PowerShell function to loop through the files in a user specified directory, marry the CCI values provided by the DISA FSO to the test IDs for each of the STIGs in the directory, and output that data to a .csv
file of the users choosing.
The code worked in PowerShell ISE
, then I tried it in PowerShell Terminal
and it no longer works in either.
When I execute the function
, it asks for and stores the parameters however the primary loop does not execute (comment below at line 23). While debugging, I saw that the foreach
loop is skipped altogether. What do I need to do to make the foreach loop execute?
Things I've tried:
if
the user specified output file existsCurrent function state:
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
}
}
Because adding the test files here caused my browser to crash, I'm providing links to where the necessary parameter files may be downloaded:
Benchmarks: General Operating System STIG CCI Matrix: DISA FSO CCI
The variable $files
is never set, thus it must be $null
(emtpy). The foreach
-loop tries to go through every element in $files
, which is nothing. The loop is not realy skipped, there is just nothing to iterate on.
If you want to iterate through all files in $BenchMarksDir
, you would have to enumerate all files in there, first.
$files = Get-ChildItem -Path $BenchMarksDir -File
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments