FakeFile.psm1

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
103
104

Enum OfficeExtensions {
    pptx
    docx
    xls
    doc
    pdf
    ppt
    dot
}

Enum MediaExtensions {
    mp3
    mp4
    jpg
    mpeg2
}

Class FakeFileGenerator {
    [System.IO.DirectoryInfo]$FolderPath = $PSScriptRoot
    [int]$TotalSize = 1MB
    [Int]$NumberOfFiles = 5

    FakeFileGenerator(){}

    [System.IO.FileInfo[]]Create(){
        
        $SingleSize = $this.TotalSize / $this.NumberOfFiles
        $out = new-object byte[] $SingleSize
        $AllFiles = @()

        for ($i = $This.NumberOfFiles; $i -gt 0; $i--) {
            $FileNAme = $this.GetFileName()
            $FileFullPath = Join-Path -Path $this.FolderPath -ChildPath $FileName
            $Null = [IO.File]::WriteAllBytes($FileFullPath, $out)
            $AllFiles += [System.IO.FileInfo]$FileFullPath 
        }
        
        Return $AllFiles
        
    }

    [String] GetFileName(){
        $Array = (Get-Verb | Select-Object verb | Get-Random -Count 2).verb
        
        $FName = $Array -join ""
        Return ($FName + $This.GetExtension())
    }

    [String] GetExtension(){
        REturn ( "." + ([Enum]::GetNames("OfficeExtensions") | Get-Random))
        
    }

    [Void]SetFolderPath([System.IO.DirectoryInfo]$FolderPath){
        $This.FolderPath = $FolderPath
    }

    [Void]SetTotalSize([Int]$TotalSize){
        $this.TotalSize = $TotalSize
    }

    [Void]SetNumberOfFiles([Int]$NumberOfFiles){
        $this.NumberOfFiles = $NumberOfFiles
    }
   

}

Function New-FakeFile {
    <#
    .SYNOPSIS
        Generates random files with meaningfull names of a specific total size.
    .DESCRIPTION
        I wrote this function during a live coding session for psconfeu 2020 of a function to a class.
        This module represents the result of this exercise.
        The talk can be found here -> https://youtu.be/sQ8hnEovZpM
        By default, the function will create the file in the current directory ($pwd)
    .EXAMPLE
        New-FakeFile -NumberOfFiles 4 -TotalSize 32MB
    .OUTPUTS
        [System.Io.FileInfo[]]
    .NOTES
        Author:Stephane van Gulick
 
    .LINK
        https://github.com/Stephanevg/FakeFile
    #>

    Param(
        $NumberOfFiles = 1,
        $TotalSize = 1MB,
        $FolderPath = ($pwd).Path
    )

    $Generator = [FakeFileGenerator]::New()
    $Generator.SetNumberOfFiles($NumberOfFiles)
    $Generator.SetTotalSize($TotalSize)
    $Generator.SetFolderPath($FolderPath)
    $Generator.Create()

}