Not sure why xml is not populating listbox and why csv is including additional characters.
Code: Select all
Function Get-FileName($Title, $FormType, $FileType)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
Out-Null
switch ($FormType)
{
'OpenFileDialog' {
$OpenFileDialog = New-Object System.Windows.Forms.$FormType
$OpenFileDialog.title = $Title
$OpenFileDialog.RestoreDirectory = $true
$OpenFileDialog.initialDirectory = $pwd #$initialDirectory
$OpenFileDialog.filter = $FileType
$OpenFileDialog.ShowHelp = $true
$file = $OpenFileDialog.ShowDialog((New-Object System.Windows.Forms.Form -Property @{ TopMost = $true }))
[int]$x = 0
[int]$i = 0
If ($file -eq "OK")
{
$OpenFileDialog.filename
}
Else
{
Write-Host "Operation cancelled by user" -ForegroundColor Red -BackgroundColor White
Exit
}
}
'FolderBrowserDialog' {
$OpenFileDialog = New-Object System.Windows.Forms.$FormType
$OpenFileDialog.Description = $Title
$OpenFileDialog.ShowNewFolderButton = 'False'
$OpenFileDialog.RootFolder = 'MyComputer'
$file = $OpenFileDialog.ShowDialog((New-Object System.Windows.Forms.Form -Property @{ TopMost = $true }))
[int]$x = 0
[int]$i = 0
If ($file -eq "OK")
{
$OpenFileDialog.SelectedPath
}
Else
{
Write-Host "Operation cancelled by user" -ForegroundColor Red -BackgroundColor White
Exit
}
}
default
{
Exit
}
}
} #end function Get-FileName
#-------------------------------------------------------
# NOTE: When new TabPage added place the validation code
# in the Validate-WizardPage function.
#-------------------------------------------------------
function Validate-WizardPage
{
<#
Add TabPages and place the validation code in this function
#>
[OutputType([boolean])]
param([System.Windows.Forms.TabPage]$tabPage)
if($tabPage -eq $tabpageStep1)
{
#TODO: Enter Validation Code here for Step 1
if(-not $labelIntent.Text)
{
return $false
}
return $true
}
elseif ($tabPage -eq $tabpageStep2)
{
#TODO: Enter Validation Code here for Step 2
# if ((Test-Path $textbox1.Text -PathType Leaf) -and (Test-Path $textbox2.Text -PathType Container) -and (Test-Path $textbox3.Text -PathType Container))
if ($textbox1.Text -and $textbox2.Text -and $textbox3.Text)
{
return $true
}
return $false
}
elseif ($tabPage -eq $tabpageStep3)
{
#TODO: Enter Validation Code here for Step 3
if( $radiobuttonSCAP.Checked -or
$radiobuttonManual.Checked)
{
return $true
}
}
#Add more pages here
return $false
}
$buttonFinish_Click={
#-------------------------------------------------------
# TODO: Place finalization script here
#-------------------------------------------------------
}
#region Events and Functions
$formESPSToSTIGACASUtilit_Load={
Update-NavButtons
}
function Update-NavButtons
{
<#
.DESCRIPTION
Validates the current tab and Updates the Next, Prev and Finish buttons.
#>
$enabled = Validate-WizardPage $tabcontrolWizard.SelectedTab
$buttonNext.Enabled = $enabled -and ($tabcontrolWizard.SelectedIndex -lt $tabcontrolWizard.TabCount - 1)
$buttonBack.Enabled = $tabcontrolWizard.SelectedIndex -gt 0
$buttonFinish.Enabled = $enabled -and ($tabcontrolWizard.SelectedIndex -eq $tabcontrolWizard.TabCount - 1)
#Uncomment to Hide Buttons
#$buttonNext.Visible = ($tabcontrolWizard.SelectedIndex -lt $tabcontrolWizard.TabCount - 1)
#$buttonFinish.Visible = ($tabcontrolWizard.SelectedIndex -eq $tabcontrolWizard.TabCount - 1)
}
$script:DeselectedIndex = -1
$tabcontrolWizard_Deselecting=[System.Windows.Forms.TabControlCancelEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.TabControlCancelEventArgs]
# Store the previous tab index
$script:DeselectedIndex = $_.TabPageIndex
}
$tabcontrolWizard_Selecting=[System.Windows.Forms.TabControlCancelEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.TabControlCancelEventArgs]
# We only validate if we are moving to the Next TabPage.
# Users can move back without validating
if($script:DeselectedIndex -ne -1 -and $script:DeselectedIndex -lt $_.TabPageIndex)
{
#Validate each page until we reach the one we want
for($index = $script:DeselectedIndex; $index -lt $_.TabPageIndex; $index++)
{
$_.Cancel = -not (Validate-WizardPage $tabcontrolWizard.TabPages[$index])
if($_.Cancel)
{
# Cancel and Return if validation failed.
return;
}
}
}
Update-NavButtons
}
$buttonBack_Click={
#Go to the previous tab page
if($tabcontrolWizard.SelectedIndex -gt 0)
{
$tabcontrolWizard.SelectedIndex--
}
}
$buttonNext_Click={
#Go to the next tab page
if($tabcontrolWizard.SelectedIndex -lt $tabcontrolWizard.TabCount - 1)
{
$tabcontrolWizard.SelectedIndex++
}
}
#endregion
function Load-ListBox
{
<#
.SYNOPSIS
This functions helps you load items into a ListBox or CheckedListBox.
.DESCRIPTION
Use this function to dynamically load items into the ListBox control.
.PARAMETER ListBox
The ListBox control you want to add items to.
.PARAMETER Items
The object or objects you wish to load into the ListBox's Items collection.
.PARAMETER DisplayMember
Indicates the property to display for the items in this control.
.PARAMETER SelectionMode
Allows multi-selection of items in the ListBox.
.PARAMETER Append
Adds the item(s) to the ListBox without clearing the Items collection.
.EXAMPLE
Load-ListBox $ListBox1 "Red", "White", "Blue"
.EXAMPLE
Load-ListBox $listBox1 "Red" -Append
Load-ListBox $listBox1 "White" -Append
Load-ListBox $listBox1 "Blue" -Append
.EXAMPLE
Load-ListBox $listBox1 (Get-Process) "ProcessName"
#>
Param (
[ValidateNotNull()]
[Parameter(Mandatory=$true)]
[System.Windows.Forms.ListBox]$ListBox,
[ValidateNotNull()]
[Parameter(Mandatory=$true)]
$Items,
[Parameter(Mandatory=$false)]
[string]$DisplayMember,
[switch]$SelectionMode,
[switch]$Append
)
If ($SelectionMode)
{
$ListBox.SelectionMode = 'MultiExtended'
}
if(-not $Append)
{
$listBox.Items.Clear()
}
if($Items -is [System.Windows.Forms.ListBox+ObjectCollection] -or $Items -is [System.Collections.ICollection])
{
$listBox.Items.AddRange($Items)
}
elseif ($Items -is [System.Collections.IEnumerable])
{
$listBox.BeginUpdate()
foreach($obj in $Items)
{
$listBox.Items.Add($obj)
}
$listBox.EndUpdate()
}
else
{
$listBox.Items.Add($Items)
}
$listBox.DisplayMember = $DisplayMember
}
#endregion
#------------------------------------------------------
# NOTE: When a Control State changes you should call
# Update-NavButtons to trigger validation
#------------------------------------------------------
$button1_Click = {
#TODO: Place custom script here
$textbox1.Text = Get-FileName -Title "Select ESPS File" -FormType "OpenFileDialog" -FileType "XML Data (*.xml)|*.xml|Comma delimited File (*.csv)|*.csv"
$ESPSPath = [IO.Path]::GetDirectoryName($textbox1.Text)
$ESPSFileExtension = [IO.Path]::GetExtension($textbox1.Text)
If ($ESPSFileExtension -eq ".xml")
{
[xml]$ESPSDetails = Get-Content $textbox1.Text
$Platforms = $ESPSDetails.ServerToolkitFindings.ServerToolkitFinding.target | Sort-Object | Get-Unique
$MissionPartners = $ESPSDetails.ServerToolkitFindings.ServerToolkitFinding.MissionPartner | Sort-Object | Get-Unique
# Load-ListBox $listbox1 -Items $Platform
# Load-ListBox $listbox2 -Items $MissionPartner
Foreach ($Platform in $Platforms) #Populate Listbox on 3rd tab
{
# [void]$listbox1.Items.Add($Platform)
Load-ListBox $listbox1 $Platform -SelectionMode -Append
}
foreach ($MissionPartner in $MissionPartners)
{
# [void]$listbox2.Items.Add($MissionPartner)
Load-ListBox $listbox2 $MissionPartner -Append
}
}
Else
{
$ESPSDetails = Import-Csv $textbox1.Text
$Platforms = $ESPSDetails | select Target | Sort Target -Unique
$MissionPartners = $ESPSDetails | select MissionPartner | Sort MissionPartner -Unique
# Load-ListBox $listbox1 -Items $Platform
# Load-ListBox $listbox2 -Items $MissionPartner
Foreach ($Platform in $Platforms) #Populate Listbox on 3rd tab
{
# [void]$listbox1.Items.Add($Platform)
Load-ListBox $listbox1 $Platform -SelectionMode -Append
}
foreach ($MissionPartner in $MissionPartners)
{
# [void]$listbox2.Items.Add($MissionPartner)
Load-ListBox $listbox2 $MissionPartner -Append
}
}
Update-NavButtons
}
$button2_Click = {
$textbox2.Text = Get-FileName -Title "Select folder of the source STIG files" -FormType "FolderBrowserDialog"
Update-NavButtons
}
$button3_Click = {
$textbox3.Text = Get-FileName -Title "Specify Folder for new STIG file(s)" -FormType "FolderBrowserDialog"
Update-NavButtons
}
$textbox1_TextChanged={
Update-NavButtons
}
$textbox2_TextChanged = {
Update-NavButtons
}
$textbox3_TextChanged = {
Update-NavButtons
}
$checkboxCheckToContinue_CheckedChanged = {
Update-NavButtons
}
$radiobuttonOption_CheckedChanged = {
if ($this.Checked)
{
Update-NavButtons
}
}
$listbox1_SelectedIndexChanged = {
Update-NavButtons
}
$listbox2_SelectedIndexChanged = {
Update-NavButtons
}
$checkboxIncludeACASScanRepor_CheckedChanged = {
Update-NavButtons
}
#region Control Helper Functions