internal/functions/get-fileversion.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<#
.SYNOPSIS
Get the file version details
 
.DESCRIPTION
Get the file version details for any given file
 
.PARAMETER Path
Path to the file that you want to extract the file version details from
 
.EXAMPLE
Get-FileVersion -Path "C:\Program Files\Microsoft Dynamics AX\60\Server\MicrosoftDynamicsAX\Bin\AxServ32.exe"
 
This will get the file version details for the AX AOS executable (AxServ32.exe).
 
.NOTES
Author: Mötz Jensen (@Splaxi)
 
Inspired by https://blogs.technet.microsoft.com/askpfeplat/2014/12/07/how-to-correctly-check-file-versions-with-powershell/
 
#>

function Get-FileVersion {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory = $true)]
        [string] $Path
    )
    BEGIN { 
        Write-Verbose "Starting the BEGIN section of $($MyInvocation.MyCommand.Name)"
       
        if ([System.String]::IsNullOrEmpty($Path)) {
            Write-Warning "You didn't supply a valid Path ($Path) for Get-FileVersion. Run the script again if you need to continue."
            Write-Error -Message"You didn't supply a valid Path ($Path) for Get-FileVersion. Run the script again if you need to continue." -ErrorAction Stop
        }

        Write-Verbose "End the BEGIN section of $($MyInvocation.MyCommand.Name)"
    }

    PROCESS {
        Write-Verbose "Starting the PROCESS section of $($MyInvocation.MyCommand.Name)"
        $Filepath = Get-Item -Path $Path

        [PSCustomObject]@{
            FileVersion = $Filepath.VersionInfo.FileVersion
            ProductVersion = $Filepath.VersionInfo.ProductVersion
            FileVersionUpdated = "$($Filepath.VersionInfo.FileMajorPart).$($Filepath.VersionInfo.FileMinorPart).$($Filepath.VersionInfo.FileBuildPart).$($Filepath.VersionInfo.FilePrivatePart)"
            ProductVersionUpdated = "$($Filepath.VersionInfo.ProductMajorPart).$($Filepath.VersionInfo.ProductMinorPart).$($Filepath.VersionInfo.ProductBuildPart).$($Filepath.VersionInfo.ProductPrivatePart)"
        }

        Write-Verbose "End the PROCESS section of $($MyInvocation.MyCommand.Name)"        
    }

    END {}
}