diff --git a/ConvertToExcelXlsx.ps1 b/ConvertToExcelXlsx.ps1 index eb62c65..7d2eb6a 100644 --- a/ConvertToExcelXlsx.ps1 +++ b/ConvertToExcelXlsx.ps1 @@ -1,18 +1,21 @@ Function ConvertTo-ExcelXlsx { -[CmdletBinding()] + <# + .SYNOPSIS + Converts an Excel xls to a xlsx using -ComObject + #> + [CmdletBinding()] Param ( - [parameter(Mandatory=$true, ValueFromPipeline)] + [parameter(Mandatory = $true, ValueFromPipeline)] [string]$Path, - [parameter(Mandatory=$false)] + [parameter(Mandatory = $false)] [switch]$Force ) - Process - { - if(-Not ($Path | Test-Path) ){ - throw "File not found" + Process { + if (-Not ($Path | Test-Path) ) { + throw "File not found" } - if(-Not ($Path | Test-Path -PathType Leaf) ){ + if (-Not ($Path | Test-Path -PathType Leaf) ) { throw "Folder paths are not allowed" } @@ -20,26 +23,29 @@ Function ConvertTo-ExcelXlsx { $xlsFile = Get-Item -Path $Path $xlsxPath = "{0}x" -f $xlsFile.FullName - if($xlsFile.Extension -ne ".xls"){ + if ($xlsFile.Extension -ne ".xls") { throw "Expected .xls extension" } - if(Test-Path -Path $xlsxPath){ - if($Force){ + if (Test-Path -Path $xlsxPath) { + if ($Force) { try { Remove-Item $xlsxPath -Force - } catch { + } + catch { throw "{0} already exists and cannot be removed. The file may be locked by another application." -f $xlsxPath } Write-Verbose $("Removed {0}" -f $xlsxPath) - } else { + } + else { throw "{0} already exists!" -f $xlsxPath } } - try{ + try { $Excel = New-Object -ComObject "Excel.Application" - } catch { + } + catch { throw "Could not create Excel.Application ComObject. Please verify that Excel is installed." }