Public/ConvertTo-Gif.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
<#
.SYNOPSIS
    Convert a set of images into a GIF
.DESCRIPTION
    Convert a set of images (png or jpeg) into a GIF to share with the world!
.PARAMETER FilePath
    Name and location to save the generated gif
.EXAMPLE
    ConvertTo-Gif -FilePath "c:\users\msadministrator\Desktop\mynewgif.gif"
.NOTES
    Name: ConvertTo-Gif
    Author: Josh Rickard (MSAdministrator)
    DateCreated: 07/07/2018
#>

function ConvertTo-Gif {
    [CmdletBinding(DefaultParameterSetName = 'Parameter Set 1',
        PositionalBinding = $false,
        HelpUri = '',
        ConfirmImpact = 'Medium')]
    Param (
        # Name and location to save the generated gif
        [Parameter(
            Mandatory = $True,
            ValueFromPipelineByPropertyName = $true)]
        [ValidateScript( {
                if ($_ -notmatch "(\.gif)") {
                    throw "The file specified in the path argument must be type of gif"
                }
                return $true
            } )
        ]
        [string]$FilePath
    )
    
    $ScreenShotImages = (Get-ChildItem -Path "$env:TEMP\CaptureIT\" -Depth 1).FullName

    try{
        if (-not(Get-ChildItem -Path $FilePath -ErrorAction SilentlyContinue)){
            New-Item -Path $FilePath -Force
        }
    }
    catch{
        Write-Error -ErrorRecord $Error[0] -RecommendedAction 'Ensure that you have access to create files in the provided filepath location'
    }
    
    try {
        Write-Verbose -Message 'Creating GitWriter object'

        $GifWriterObject = Import-GifWriterClass

        foreach ($image in $ScreenShotImages) {
            [System.Drawing.Image]$imageObject = [System.Drawing.Image]::FromFile("$image")
            $GifWriterObject.WriteFrame([System.Drawing.Image]$imageObject)

            $imageObject = $null
        }
        
        $GifWriterObject.Dispose()
         
        $returnObject = @{
            Gif                = $FilePath
            'Screenshot Count' = $ScreenShotImages.Count
        }
        Write-Output -InputObject $returnObject
    }
    catch {
        Write-Error -ErrorRecord $Error[0]
        exit -1
    }    
}