Functions/Private/GenerateOutputFolder.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
function GenerateOutputFolder {

    <#
    .SYNOPSIS
    Generates an output folder for the Dockerfile and artifacts.
     
    .PARAMETER Path
    The path where all artifacts and the Dockerfile will be output to
 
    .PARAMETER Force
    Useful when you want to re-use a directory when testing functionality.
    #>

    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess",'')]
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $false)]
        [string] $Path,

        [Parameter(Mandatory = $false)]
        [Switch] $Force

    )

    ### If the user didn't declare a path, then create one for them
    if (!$PSBoundParameters.Keys.Contains('Path')) {
        $Path = '{0}\{1}' -f $env:TEMP, (New-Guid).Guid
        Write-Verbose -Message ('User did not specify output path for discovery artifacts. Using auto-generated directory: {0}' -f $Path)
    }


    if ($PSBoundParameters.Keys.Contains('Force')) {
        Write-Verbose -Message "User specified the Force Parameter. Removing existing items from $Path"
        Remove-Item -Path $Path -Recurse -Force
        
        $null = New-Item -Path $Path -ItemType Directory
        Write-Verbose -Message "User specified path $Path has been created"

        return $Path 
    }
    elseif ((Test-Path -Path $Path) -and (Get-ChildItem -Path $Path)) {
        throw 'The directory specified by the -OutputPath parameter must be empty'
    }
    else {
        ### Create the directory if it doesn't exist
        try {
            (mkdir -Path $Path -ErrorAction Stop).FullName
        }
        catch {
            return $Path
        }
    }

}