Get-HTSupportInfo.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
function Get-HTSupportInfo {
    [CmdletBinding()]
    param (

        # If Set, logs will be delete before Collecting Info.
        [switch]
        $ClearLogsFirst,

        #If Set, Only logs will be collected, overrides FilePatterns Option.
        [switch]
        $OnlyLogs,

        # Base Folder Path To Gather Log Files.
        [Parameter(Position=1)]
        [string]
        $BasePath = 'C:\Program Files\CSM',

        # File Filter For Files default is "*.log"
        [Parameter(Position=2)]
        [string[]]
        $FilePatterns = @('*.log', '*.db', '*.config')



    )
    
    process {

        $invokeInfo = @()
        $invokeInfo += "Command Invoked at $(Get-Date)"
        $invokeInfo += "Base path is set as $BasePath"
        $invokeInfo += "File Patterns is set as $FilePatterns"

        if($ClearLogsFirst)
        {
            $invokeInfo += "Clear Logs First switch is set. Starting to delete Logs $(Get-Date)"
            Clear-HTLogFiles
            $invokeInfo += "Logs Cleared, starting to wait for collection confirmation $(Get-Date)"
            Read-Host -Prompt "When Ready Press Enter to Collect Support Information..."
            $invokeInfo += "Starting to Collect Logs $(Get-Date)"
            
        }

        if($OnlyLogs) {$FilePatterns = @('*.log'); $invokeInfo += "Only Logs Switch Set"}

        $dateSuffix = Get-Date -Format 'ddMMyyyyHHmmss'
        $hostname = hostname
        $tempFolder = New-HTTempFolder -FolderName "$hostname-$dateSuffix"

        foreach($FileFilter in $FilePatterns)
        {
            $filterFriendlyName = $FileFilter -replace "[\*\.]"
            Write-Progress -Activity "Searching $filterFriendlyName Files" -Status "Searching" -PercentComplete 0;
            $files = Get-ChildItem -Path $BasePath -Filter $FileFilter -Recurse
            $total = $files.Count
            $progress = 0

            foreach($f in $files)
            {
                $percentComplete = $progress/$total*100
                Write-Progress -Activity "Copying $filterFriendlyName Files" -Status $f.FullName -PercentComplete $percentComplete;
    
                $dir = $f.Directory
                $splitted = $dir.FullName.Split('\')
    
                $t = $splitted[-3] + '-' + $splitted[-2] + '-' + $splitted[-1] 
                $t = $t + '-' + $f.Name
                $tempPatternFolder = Join-Path $tempFolder $filterFriendlyName

                
                if(!$(Test-Path $tempPatternFolder))
                {
                    Write-Verbose "$tempPatternFolder not exists creating..."
                    New-Item $tempPatternFolder -ItemType Directory | Out-Null
                }

                $t = Join-Path $tempPatternFolder $t
                Write-Verbose "Full Name: $($f.FullName) Target: $t" 
                Copy-Item $f.FullName $t
                $progress++
            }
        }

        $invokeInfo += "Collection Finished at $(Get-Date)"
        Out-File -InputObject $invokeInfo -FilePath $(Join-Path $tempFolder "InvokeInfo.txt") -Encoding utf8
        Compress-Archive -Path $tempFolder -DestinationPath "SupportInfo-$dateSuffix"
        Remove-HTTempFolder -FolderName $tempFolder.Name
    }

}