Public/Start-ActiveWindowCapture.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
<#
.SYNOPSIS
    Capture the active window
.DESCRIPTION
    Capture the active window
.PARAMETER Milliseconds
    Milliseconds between screenshot
.PARAMETER ImageType
    Image type to capture
.EXAMPLE
    Start-ActiveWindowCapture
.EXAMPLE
    Start-ActiveWindowCapture -Milliseconds 500 -ImageType jpeg
.NOTES
    This was borrowed from Boe Prox in his Take-ScreenShot PowerShell Function This function has used some of Boe Prox's Take-ScreenShot PowerShell function
        Name: Take-ScreenShot
        Author: Boe Prox
        DateCreated: 07/25/2010
        Modified Author: Josh Rickard (MSAdministrator)
        Modified Date: 07/07/2018
#>

function Start-ActiveWindowCapture {
    [CmdletBinding(DefaultParameterSetName = 'Parameter Set 1',
        SupportsShouldProcess = $true,
        PositionalBinding = $false,
        HelpUri = '',
        ConfirmImpact = 'Medium')]
    Param (
        # Milliseconds between screenshot
        [Parameter(
            Mandatory = $false,
            ValueFromPipelineByPropertyName = $true)]
        [int]$Milliseconds = '1000',

        # Image type to capture
        [Parameter(
            Mandatory = $false,
            ValueFromPipelineByPropertyName = $true)]
        [ValidateSet('jpeg', 'png')]
        [string]$ImageType = 'png'
    )

    begin {
        Write-Verbose -Message 'Starting Active Window Capture'

        try {
            Write-Debug -Message 'Getting ScreenCapture Class'
            $ScreenCaptureObject = Import-ScreenCaptureClass -ErrorAction Stop
        }
        catch {
            Write-Error -ErrorRecord $Error[0]
        }
    }
    process {
        $varCount = 1
        try {
            if ($pscmdlet.ShouldProcess("Active Window", "Capturing")) {

                do {
                    Write-Output "Press the 'x' key to stop capturing."

                    Write-Verbose "Taking screenshot of the active window"

                    Write-Verbose -Message 'Saving screenshots of the active window'
                    $TempFileLocation = "$env:TEMP\CaptureIT\ScreenCapture$varCount.$ImageType"

                    Write-Verbose -Message "Creating temporary screenshot: $TempFileLocation"
                    New-Item -Path $TempFileLocation -Force | Out-Null

                    Write-Verbose "Creating activewindow file: $TempFileLocation"
                    $ScreenCaptureObject.CaptureActiveWindowToFile($TempFileLocation, $ImageType)

                    Write-Debug -Message 'Incremeting varCount by 1'
                    $varCount++

                    while ([console]::KeyAvailable -eq $false) {
                        Start-Sleep -Milliseconds 100
                    }

                    $CKI = [console]::ReadKey($true)
                    Write-Output "You pressed the $($CKI.Key) key."

                    if ($CKI.Key -eq 'x') {
                        Write-Progress -Activity 'Creating GIF' -Status 'Creating....'
                        ConvertTo-Gif -FilePath $script:GifFilePath
                        Write-Progress -Activity 'Creating GIF' -Status 'Complete!'
                        return
                    }

                } while ($CKI.Key -ne 'x')
            }
        }
        catch {
            Write-Error -ErrorRecord $Error[0]
            exit -1
        }
    }
    end {
        Write-Verbose -Message 'Captured Active Window successfully'
        Write-Output $true
    }
}