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