accept a collection of paths

This commit is contained in:
Edward Miller
2024-05-21 16:15:12 -05:00
parent 66c0fee1e9
commit 1c6be383cc

View File

@@ -3,20 +3,22 @@ function ConvertTo-ExcelXlsx {
param param
( (
[parameter(Mandatory = $true, ValueFromPipeline)] [parameter(Mandatory = $true, ValueFromPipeline)]
[string]$Path, [string[]]$Path,
[parameter(Mandatory = $false)] [parameter(Mandatory = $false)]
[switch]$Force [switch]$Force
) )
process { process {
if (-Not ($Path | Test-Path) ) { try {
foreach ($singlePath in $Path) {
if (-Not ($singlePath | Test-Path) ) {
throw "File not found" throw "File not found"
} }
if (-Not ($Path | Test-Path -PathType Leaf) ) { if (-Not ($singlePath | Test-Path -PathType Leaf) ) {
throw "Folder paths are not allowed" throw "Folder paths are not allowed"
} }
$xlFixedFormat = 51 #Constant for XLSX Workbook $xlFixedFormat = 51 #Constant for XLSX Workbook
$xlsFile = Get-Item -Path $Path $xlsFile = Get-Item -Path $singlePath
$xlsxPath = "{0}x" -f $xlsFile.FullName $xlsxPath = "{0}x" -f $xlsFile.FullName
if ($xlsFile.Extension -ne ".xls") { if ($xlsFile.Extension -ne ".xls") {
@@ -38,12 +40,15 @@ function ConvertTo-ExcelXlsx {
} }
} }
if ($null -eq $Excel)
{
try { try {
$Excel = New-Object -ComObject "Excel.Application" $Excel = New-Object -ComObject "Excel.Application"
} }
catch { catch {
throw "Could not create Excel.Application ComObject. Please verify that Excel is installed." throw "Could not create Excel.Application ComObject. Please verify that Excel is installed."
} }
}
try { try {
$Excel.Visible = $false $Excel.Visible = $false
@@ -54,13 +59,20 @@ function ConvertTo-ExcelXlsx {
$workbook.SaveAs($xlsxPath, $xlFixedFormat) $workbook.SaveAs($xlsxPath, $xlFixedFormat)
} }
} }
catch {
Write-Error ("Failed to convert {0} to XLSX." -f $xlsFile.FullName)
throw
}
finally { finally {
if ($null -ne $workbook) { if ($null -ne $workbook) {
$workbook.Close() $workbook.Close()
} }
}
}
}
finally {
$Excel.Quit() $Excel.Quit()
$Excel = $null
} }
} }
} }