How often have you worked on a project were you needed to generate files in big quantity in order to simulate a data usage ? Plus, on some projects you need just one or two big files, just to measure bandwidth, or validate a backup procedure, and on some other projects, you actually need a huge number of small files.
Well, i was facing that situation the other day, and started to write a function in order to create files randomly with “humanly understandable” names.
Find here the code for the function “Create-Files” which can help you fulfill theses needs 🙂
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
Function Create-Files{
<#
.SYNOPSIS
Generates a number of dumb files for a specific size.
.DESCRIPTION
Generates a defined number of files until reaching a maximum size.
.PARAMETER Totalsize
Specify the total size you would all the files combined should use on the harddrive.
This parameter accepts the following size values (KB,MB,GB,TB)
5MB
3GB
200KB
.PARAMETER NumberOfFiles
Specify a number of files that need to be created. This can be used to generate a big number of small files in order to simulate
User backup specefic behaviour.
.PARAMETER FilesTypes
This parameter is not mandatory, but two choices are valid:
Office : Will generate files with the following extensions: “.pptx”,“.docx”,“.doc”,“.xls”,“.docx”,“.doc”,“.pdf”,“.ppt”,“.pptx”,“.dot”
Multimedia : Will create random files with the following extensions : “.avi”,“.midi”,“.mov”,“.mp3”,“.mp4”,“.mpeg”,“.mpeg2”,“.mpeg3”,“.mpg”,“.ogg”,“.ram”,“.rm”,“.wma”,“.wmv”
If Filestypes parameter is not set, by default, the script will create both office and multimedia type of files.
.PARAMETER Path
Specify a path where the files should be generated.
.PARAMETER Whatif
Permits to launch this script in “draft” mode. This means it will only show the results without really making generating the files.
.PARAMETER Verbose
Allow to run the script in verbose mode for debbuging purposes.
.EXAMPLE
.Create-Files.ps1 -totalsize 50MB -NumberOfFiles 13 -Path C:UsersSvangulick
Will generate randonmly 13 files for a total of 50mb in the path c:userssvangulick
.EXAMPLE
.Create-Files.ps1 -totalsize 5GB -NumberOfFiles 3 -Path C:UsersSvangulick
Will generate randonmly 3 files for a total of 5Gigabytes in the path c:userssvangulick
.NOTES
-Author: Stéphane van Gulick
-Email : Svangulick@gmail.com
-Version: 1.0
-History:
-Creation V0.1 : SVG
-First final draft V0.5 : SVG
-Corrected minor bugs V0.6 : SVG
-Functionalized the script V0.8 : SVG
-Simplified code V1.0 : SVG
.LINK
http://www.PowerShellDistrict.com
#>
[cmdletbinding()]
param(
[Parameter(mandatory=$true)]$NumberOfFiles,
[Parameter(mandatory=$true)]$path,
[Parameter(mandatory=$true)]$TotalSize
)
begin{
Write-verbose “Generating files”
$AllCreatedFilles = @()
function Create-FileName{
[CmdletBinding(SupportsShouldProcess=$true)]
param(
[Parameter(mandatory=$false)][validateSet(“Multimedia”,“Office”,“all”,“”)][String]$filesType=$all
)
begin {
$AllExtensions = @()
$MultimediaExtensions = “.avi”,“.midi”,“.mov”,“.mp3”,“.mp4”,“.mpeg”,“.mpeg2”,“.mpeg3”,“.mpg”,“.ogg”,“.ram”,“.rm”,“.wma”,“.wmv”
$OfficeExtensions = “.pptx”,“.docx”,“.doc”,“.xls”,“.docx”,“.doc”,“.pdf”,“.ppt”,“.pptx”,“.dot”
$AllExtensions = $MultimediaExtensions + $OfficeExtensions
$extension = $null
}
process{
Write-Verbose “Creating file Name”
#$Extension = $MultimediaFiles | Get-Random -Count 1
switch ($filesType)
{
“Multimedia”{$extension = $MultimediaExtensions | Get-Random}
“Office”{$extension = $OfficeExtensions | Get-Random }
default
{
$extension = $AllExtensions | Get-Random
}
}
Get-Verb | Select-Object verb | Get-Random -Count 2 | %{$Name+= $_.verb}
$FullName = $name + $extension
Write-Verbose “File name created : $FullName”
}
end{
return $FullName
}
}
}
#—————-Process———————————————–
process{
$FileSize = $TotalSize / $NumberOfFiles
$FileSize = [Math]::Round($FileSize, 0)
while ($TotalFileSize -lt $TotalSize) {
$TotalFileSize = $TotalFileSize + $FileSize
$FileName = Create-FileName -filesType $filesType
Write-verbose “Creating : $filename of $FileSize”
Write-Verbose “Filesize = $filesize”
$FullPath = Join-Path $path -ChildPath $fileName
Write-Verbose “Generating file : $FullPath of $Filesize”
try{
fsutil.exe file createnew $FullPath $FileSize | Out-Null
}
catch{
$_
}
$FileCreated = “”
$Properties = @{‘FullPath’=$FullPath;‘Size’=$FileSize}
$FileCreated = New-Object -TypeName psobject -Property $properties
$AllCreatedFilles += $FileCreated
Write-verbose “$($AllCreatedFilles) created $($FileCreated)”
}
}
end{
Write-Output $AllCreatedFilles
}
}
|