Hello all,
I have a script that allows the user to select a word file and it will ingest the tables from the file, my end goal is to export it to a csv file.
However, I need to remove all rows that contain the word ‘resolved’ and I need to remove one of the columns with the heading of ‘changed’.
Looking for some guidance.
$date = Get-Date -Format "Y"
$currentFolder = (Get-Location).Path
$tempoutputfile = $currentFolder+"\"+"out_temp.csv"
$outputfile = $date + " "+$name +" " + " Monthly Metrics.csv"
$header = "Security Criteria,Date,Number of Findings,Severity"
########################################################################
### This function lists a menu for the user to choose which organization
########################################################################
# Define an array of names to choose from
$names = @("co1", "co2", "co3", "co4")
# Display the menu and get the user's choice
Write-Host "Which organization are you exporting for?"
for ($i=0; $i -lt $names.Length; $i++) {
Write-Host ("{0}. {1}" -f ($i+1), $names[$i])
}
$org = Read-Host "Enter the number of your choice"
##########################
# Get the chosen selection
$name = $names[$org-1]
#####################################################################################
### This function opens a Windows Dialog Box for the selection of the source file ###
#####################################################################################
Write-Output "################################################"
Write-Output " "
Write-Output "Select the source file."
Write-Output "The starting directory is C:\."
Write-Output " "
Write-Output "################################################"
Function Get-FileName($initialDirectory)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = "docx (*.docx)| *.docx"
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
}
$inputfile = Get-FileName "C:\"
$Word = New-Object -Com Word.Application
$Document = $Word.Documents.Open($inputfile)
#######################
### End of function ###
# Ask the user how many tables need importing and save it to choice variable
$choice = Read-Host "Enter the number of tables in the word document that need to be imported"
# Select the choice amount of tables
$Tables = $Document.Tables | Select-Object -First $choice
foreach($Table in $Tables)
{
$TableCols = $Table.Columns.Count
$TableRows = $Table.Rows.Count
# this gets the list separator character your local Excel expects
$delimiter = [cultureinfo]::CurrentCulture.TextInfo.ListSeparator
for($r = 2; $r -le $TableRows; $r++) {
# capture an array of cell contents
$content = for($c = 1; $c -le $TableCols; $c++) {
# surround each value with quotes to prevent fields that contain the delimiter character would ruin the csv,
# double any double-quotes the value may contain,
# remove the control characters (0x0D 0x07) Word appends to the cell text
# trim the resulting value from leading or trailing whitespace characters
'"{0}"' -f ($Table.Cell($r,$c).Range.Text -replace '"', '""' -replace '[\x00-\x1F\x7F]').Trim()
($Table.Cell($r,$c).Range.Text -replace '[\x00-\x1F\x7F]').Trim()
}
# output this array joined with the delimiter, both on screen and to file
$content -join $delimiter | Add-Content -Path $tempoutputfile -PassThru
}
}
$Document.Close()
$Word.Quit()
# Stop Winword Process
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Document)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Word)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()