functions/get-axserverxppilhash.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111

<#
    .SYNOPSIS
        Get hash values for XppIL files
         
    .DESCRIPTION
        Generate the file hash values for each *.netmodule & *.dll file in the XppIL folder
         
    .PARAMETER InstanceName
        Name of the instance that you are working against
         
        Default value can be configured with the Set-AxActiveAosConfig cmdlet
         
    .PARAMETER OutputPath
        Path to the folder where the output file must be saved
         
        Default value is: "C:\temp\ax2012.tools"
         
    .PARAMETER FileName
        Name of the output as you want it to be named
         
        If left empty the output file will be named based on the following pattern: "SERVERNAME_INSTANCENAME_XppIL_HashValue.txt"
         
    .EXAMPLE
        PS C:\> Get-AxServerXppILHash -InstanceName "AXTEST"
         
        This will generate the file hash values for the XppIL files for the AXTEST AOS Instance.
        It will work against the Instance AXTEST.
        It will save the output file to the default folder location "C:\temp\ax2012.tools".
        The file will named "SERVER_AXTEST_XppIL_HashValues.txt"
         
    .EXAMPLE
        PS C:\> Get-AxServerXppILHash
         
        This will generate the file hash values for the XppIL files for the default AOS Instance.
        It will work against the default AOS Instance that has been configured with Set-AxActiveAosConfig.
        It will save the output file to the default folder location "C:\temp\ax2012.tools".
        The file will named "SERVER_INSTANCENAME_XppIL_HashValues.txt"
         
        The default value of the instance name can be configured with the Set-AxActiveAosConfig cmdlet.
         
    .NOTES
        Tags: XppIL, Hash, Files
         
        Author: Mötz Jensen (@Splaxi)
         
        All credits goes to Kenneth Madsen (@KennethGrupp) for providing detailed examples on how to achieve this the best way using powershell
         
#>


function Get-AxServerXppILHash {
    [CmdletBinding()]
    param (
        [string] $InstanceName = $Script:ActiveAosInstancename,

        [string] $OutputPath = $Script:DefaultTempPath,

        [string] $FileName
    )
    
    if ([String]::IsNullOrEmpty($InstanceName)) {
        $messageString = "Instance name was <c='em'>empty</c>. You either need to supply it with <c='em'>-InstanceName</c> parameter or configure it using the <c='em'>Set-AxActiveAosConfig</c> cmdlet."
        Write-PSFMessage -Level Host -Message $messageString
        Stop-PSFFunction -Message "Stopping because of missing instance name parameter." -Exception $([System.Exception]::new($($messageString -replace '<[^>]+>', '')))
        return
    }

    if ($InstanceName -eq "*") {
        $messageString = "Instance name cannot be <c='em'>*</c>. You either need to supply an instance name that will only result in <c='em'>one</c> AOS instance."
        Write-PSFMessage -Level Host -Message $messageString
        Stop-PSFFunction -Message "Stopping because of missing instance name parameter." -Exception $([System.Exception]::new($($messageString -replace '<[^>]+>', '')))
        return
    }

    if (-not (Test-PathExists -Path $Path -Type Container -Create)) { return }

    Invoke-TimeSignal -Start

    if ([String]::IsNullOrEmpty($FileName)) {
        $filename = "$($env:COMPUTERNAME)_$InstanceName`_XppIL_HashValues.txt"
    }
    
    $outputFile = Join-Path $OutputPath $FileName

    Write-PSFMessage -Level Verbose -Message "Removing old output file: $outputFile" -Target $outputFile
    Remove-Item -Path $outputFile -Force -WarningAction SilentlyContinue -ErrorAction SilentlyContinue

    $basePath = (Get-AxAosInstance -InstanceName $InstanceName).BinDirectory

    $fileExtensions = @("*.netmodule", "*.dll")

    $searchPath = (Join-Path $basePath "XppIL")

    Write-PSFMessage -Level Verbose -Message "Working against the $InstanceName instance with the path: $searchPath" -Target $searchPath
    $xppFiles = Get-ChildItem -Path $searchPath -Include $fileExtensions -Recurse
    
    $resList = New-Object System.Collections.Generic.List[object]

    foreach ($item in $xppFiles) {
        $resList.Add($(Get-FileHash -Path $item.FullName))
    }

    Write-PSFMessage -Level Verbose -Message "Generating the hash values and saving the output to path: $outputFile" -Target $outputFile
    $resList.ToArray() | Sort-Object Path | Format-Table Hash, Path -Wrap -AutoSize | Out-String -Width 4000 | Out-File $outputFile -Encoding utf8

    [PSCustomObject]@{
        Path = $outputFile
    }

    Invoke-TimeSignal -End
}