From 78eb8339b7eaca2e275f7a944984c54ae63ac206 Mon Sep 17 00:00:00 2001 From: Clayton Tyger Date: Sun, 28 Jan 2024 07:49:26 -0500 Subject: [PATCH] Cleaned up Excel Validation --- .../Public/Export-CT365ProdTeamstoExcel.ps1 | 20 ++++++++----- .../Public/Export-CT365ProdUserToExcel.ps1 | 30 +++++++++---------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/Functions/Public/Export-CT365ProdTeamstoExcel.ps1 b/Functions/Public/Export-CT365ProdTeamstoExcel.ps1 index cae49e5..be1cd43 100644 --- a/Functions/Public/Export-CT365ProdTeamstoExcel.ps1 +++ b/Functions/Public/Export-CT365ProdTeamstoExcel.ps1 @@ -32,15 +32,21 @@ function Export-CT365ProdTeamsToExcel { param ( [Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)] [ValidateScript({ - switch ($psitem){ - {-not(([System.IO.Path]::GetExtension($psitem)) -match "(.xlsx)")}{ - "Invalid file format: '$PSitem'. Use .xlsx" + $isValid = $false + $extension = [System.IO.Path]::GetExtension($_) + $directory = [System.IO.Path]::GetDirectoryName($_) + + if ($extension -ne '.xlsx') { + throw "The file $_ is not an Excel file (.xlsx). Please specify a file with the .xlsx extension." } - Default{ - $true + elseif (-not (Test-Path -Path $directory -PathType Container)) { + throw "The directory $directory does not exist. Please specify a valid directory." } - } - })] + else { + $isValid = $true + } + return $isValid + })] [string]$FilePath, [Parameter(Mandatory)] diff --git a/Functions/Public/Export-CT365ProdUserToExcel.ps1 b/Functions/Public/Export-CT365ProdUserToExcel.ps1 index c0b9992..9c6ed5f 100644 --- a/Functions/Public/Export-CT365ProdUserToExcel.ps1 +++ b/Functions/Public/Export-CT365ProdUserToExcel.ps1 @@ -46,21 +46,21 @@ function Export-CT365ProdUserToExcel { param ( [Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)] [ValidateScript({ - $isValid = $false - $extension = [System.IO.Path]::GetExtension($_) - $directory = [System.IO.Path]::GetDirectoryName($_) - - if ($extension -ne '.xlsx') { - throw "The file $_ is not an Excel file (.xlsx). Please specify a file with the .xlsx extension." - } - elseif (-not (Test-Path -Path $directory -PathType Container)) { - throw "The directory $directory does not exist. Please specify a valid directory." - } - else { - $isValid = $true - } - return $isValid - })] + $isValid = $false + $extension = [System.IO.Path]::GetExtension($_) + $directory = [System.IO.Path]::GetDirectoryName($_) + + if ($extension -ne '.xlsx') { + throw "The file $_ is not an Excel file (.xlsx). Please specify a file with the .xlsx extension." + } + elseif (-not (Test-Path -Path $directory -PathType Container)) { + throw "The directory $directory does not exist. Please specify a valid directory." + } + else { + $isValid = $true + } + return $isValid + })] [string]$FilePath, [Parameter()]