Private/New-HPSTmpFile.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
Function New-HPSTmpFile {

    <#
        .SYNOPSIS
        n/a

        .DESCRIPTION
        n/a

        .PARAMETER Extension
        n/a

        .EXAMPLE
        New-HPSTmpFile -Extension inf

        .INPUTS
        System.String

        .OUTPUTS
        System.String

        .LINK
        https://hardening.thomas-illiet.fr/Private/New-HPSTmpFile/

        .LINK
        https://github.com/thomas-illiet/Hardening/blob/stable/Hardening/Private/New-HPSTmpFile.ps1

        .NOTES
        - File Name : New-HPSTmpFile.ps1
        - Author : Thomas ILLIET
    #>


    [CmdletBinding( HelpUri = "https://hardening.thomas-illiet.fr/Private/New-HPSTmpFile/" )]
    [OutputType( [System.String] )]
    Param(
        [Parameter(Mandatory = $true)]
        [System.String]
        $Extension
    )

    begin {
        Write-Verbose "[$($MyInvocation.MyCommand.Name)] Function started"
    }

    process {
        # Set the name and path of the temporary file
        $Path     = [System.IO.Path]::GetTempPath()
        $FileName = [System.Guid]::NewGuid().ToString() + '.' + $Extension
        $FilePath = [System.IO.Path]::Combine($Path, $FileName)

        # Create a temporary file
        if( (Test-Path -Path $FilePath) -eq $False ) {
            Return (New-Item -Path $FilePath -ItemType File).FullName
        }
        else {
            Write-Error "Cannot create a temporary file when that file already exists : ${FilePath}"
        }
    }

    end {
        Write-Verbose "[$($MyInvocation.MyCommand.Name)] Complete"
    }
}