Get PSADT App Version from Deploy-Applicaiton.ps1

I am working on a function to get the $AppVersion from a PSADT Deploy-Application.ps1 file. I have a working solution but I think it could be improved.

I search for “appVersion =” so if that space was removed my search would fail.

Also I get the appversion with substring operations that would fail if the script had anything but $appversion = '9.9.9'. For instance, these would fail

$appversion = '9.9.9'#2spaces $appversion ='9.9.9'#no spaces 

Basically, I am looking for tips to make this function less “brittle” and more error handling. Thoughts on the overall style/readability?

function Get-PSADTAppVersion { param ( [Parameter(Mandatory = $true)] [string] [ValidateScript({ if(-Not (Test-Path -Path "$_") ){ throw "Folder does not exist" } if(-Not (Test-Path -Path "$_" -PathType Container) ){ throw "The PackageRootFolder argument must be a folder. Files are not allowed." } return $true })] $PackageRootFolder, [string] [ValidateScript({ if(-Not (Test-Path -Path "$PackageRootFolder$InstallScript") ){ throw "The PackageRootFolder argument path does not exist" } if(-Not (Test-Path -Path "$PackageRootFolder$InstallScript" -PathType Leaf) ){ throw "The InstallScript argument must be a file." } return $true })] $InstallScript = "Deploy-Application.ps1" #defaults to this ) $AppVersionFromFile = Select-String -Path ("$PackageRootFolder$InstallScript") -Pattern "appVersion =" $AppVersionFromFile = "$($AppVersionFromFile[0])" $CurrentAppVersion = $AppVersionFromFile.Substring($AppVersionFromFile.LastIndexOf("=")+3) $CurrentAppVersion = $CurrentAppVersion.Substring(0,$CurrentAppVersion.Length-1) return $CurrentAppVersion } 

submitted by /u/NathanTheGr8
[link] [comments]

Leave a Reply