Unable to open Excel package from UNC location.
AngelusMortiel opened this issue · 1 comments
This issue is referencing at least two instances here:
ImportExcel/Public/Import-Excel.ps1
Line 114 in dc4a5e9
and here:
ImportExcel/Public/Open-ExcelPackage.ps1
Line 32 in dc4a5e9
The cause is the usage of Resolve-Path -Path $path or Test-Path -Path $path, which does not work with UNC path locations.
To resolve this, I would at least request the usage of these cmdlets to include an option for usage of the -LiteralPath parameter instead for those cmdlets.
An example of how this might be done:
function Resolve-DynamicPath {
# I am using params of distinct types if wanting to use ValueFromPipeline, but it's not required.
[CmdletBinding(DefaultParameterSetName='Path')]
param(
[Parameter(ParameterSetName='Path',Mandatory,ValueFromPipeline)]
[System.IO.FileInfo]$Path,
[Parameter(ParameterSetName='LiteralPath',Mandatory,ValueFromPipeline)]
[System.Uri]$LiteralPath
)
switch ($PSCmdlet.ParameterSetName) {
'Path' {
return (Resolve-Path -Path $Path);
}
'LiteralPath' {
return (Resolve-Path -LiteralPath $LiteralPath);
}
}
}function Test-DynamicPath {
# I am using params of distinct types if wanting to use ValueFromPipeline, but it's not required.
[CmdletBinding(DefaultParameterSetName='Path')]
param(
[Parameter(ParameterSetName='Path',Mandatory,ValueFromPipeline)]
[System.IO.FileInfo]$Path,
[Parameter(ParameterSetName='LiteralPath',Mandatory,ValueFromPipeline)]
[System.Uri]$LiteralPath
)
switch ($PSCmdlet.ParameterSetName) {
'Path' {
return (Test-Path -Path $Path);
}
'LiteralPath' {
return (Test-Path -LiteralPath $LiteralPath);
}
}
}Alternative to Test-Path, I use the .Exists property of a [System.IO.FileInfo] object, like this:
[System.IO.FileInfo]$ExcelFile = "\\server\share\folder\excelfile.xlsx"
if ($ExcelFile.Exists) {
"File exists."
else {
"File does not exist."
}Reference: System.IO.FileInfo.Exists Property | Microsoft Learn
The actual issue here is me using square brackets [ ] in my file names, which the -Path parameter does not interpret literally for some reason, despite not being wildcard characters.
While there's still an argument for implementing -LiteralPath or FileInfo.Exists as better validation methods, I don't think it really merits an issue here.