-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNew-JunkFile.ps1
368 lines (306 loc) · 15.3 KB
/
New-JunkFile.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
function New-Junkfile {
<#
.SYNOPSIS
Create temp files
.DESCRIPTION
Create email, word, text, pdf or csv files for a lab for mallow and migration purposes
.PARAMETER OutputPath
Default save location
.PARAMETER EmailOutputPath
Save path for email files
.PARAMETER ExcelOutputPath
Save path excel documents
.PARAMETER TextOutputPath
Save path text documents
.PARAMETER PdfOutputPath
Save path for Pdf files
.PARAMETER WordOutputPath
Save path for word documents
.PARAMETER DefaultType
Default file type
.PARAMETER FileSize
File size you want to generate
.PARAMETER NumberOfWords
Number of words per sentence
.PARAMETER NumberOfFilesToCreate
How many files to create
.EXAMPLE
PS C:\> New-Junkfile -DefaultType Text -FileSize StupidLarge -NumberOfFilesToCreate 1 -NumberOfWords 100
Will create 1 extremely large text file
.EXAMPLE
PS C:\> New-Junkfile -DefaultType Word, Excel -FileSize Large -NumberOfFilesToCreate 25 -NumberOfWords 5
Will create 25 large Word and Excel documents
.EXAMPLE
PS C:\> New-Junkfile -DefaultType Word, Excel, Pdf, Email -FileSize Massive -NumberOfFilesToCreate 50 -NumberOfWords 5
Will create 50 massive Word, Excel, exported Pdf's and eml files
.NOTES
Speed on file creation
Fastest - Emails
Text Files
Word & PDF
Slowest Excel
#>
[CmdletBinding()]
param (
[parameter(Position = '0')]
[string]
$OutputPath = "c:\temp\JunkFiles",
[string]
$EmailOutputPath = "c:\temp\JunkFiles\Emails\",
[string]
$ExcelOutputPath = "c:\temp\JunkFiles\ExcelFiles\",
[string]
$TextOutputPath = "c:\temp\JunkFiles\TextFiles\",
[string]
$PdfOutputPath = "c:\temp\JunkFiles\PdfFiles\",
[string]
$WordOutputPath = "c:\temp\JunkFiles\WordFiles\",
[parameter(Position = '1')]
[ValidateSet('Email', 'Excel', 'Pdf', 'Text', 'Word')]
[object]
$DefaultType = 'Word',
[parameter(Position = '2', ParameterSetName = 'EmailSet')]
[string]
$MailFrom = "Administrator@Contoso.com",
[parameter(Position = '3', ParameterSetName = 'EmailSet')]
[string]
$MailTo = "Administrator@Contoso.com",
[parameter(Position = '4', ParameterSetName = 'FileSet')]
[ValidateSet('Tiny', 'Small', 'Medium', 'Large', 'Massive', 'StupidLarge')]
[string]
$FileSize = "Tiny",
[parameter(Position = '5', ParameterSetName = 'FileSet')]
[ValidateRange(1, 100)]
[int]
$NumberOfWords = 5,
[parameter(Position = '6', ParameterSetName = 'FileSet')]
[Int]
$NumberOfFilesToCreate = 1
)
begin {
Write-Host -ForegroundColor Green "Starting file generation process"
$directories = @($OutputPath, $EmailOutputPath, $ExcelOutputPath, $TextOutputPath, $WordOutputPath, $PdfOutputPath)
$script:paragraph = ''
$script:counter = 0
$script:emailCounter = 0
try {
Write-Verbose "Checking directory structure of: $($OutputPath)"
foreach ($directory in $directories) {
if (-NOT (Test-Path -Path $directory)) {
$null = New-Item -Path $directory -ItemType Directory -ErrorAction Stop
Write-Verbose "Creating directory structure for: $($OutputPath)"
}
else {
Write-Verbose "Directory: $($OutputPath) found!"
}
}
}
catch {
Write-Host -ForegroundColor Red 'ERROR: $_'
}
}
process {
try {
if ($DefaultType -eq 'Word' -or $DefaultType -eq 'Excel') {
Write-Verbose 'Searching GAC for assembly Microsoft.Office.Interop.Word'
if (-NOT ([AppDomain]::CurrentDomain.GetAssemblies() | Where-Object { $_.Location -Like '*Microsoft.Office.Interop.Word*' })) {
$currentLocation = Get-Location
Set-Location (Join-Path -Path $env:HOMEDRIVE -ChildPath '\')
Write-Verbose 'Searching for assembly Microsoft.Office.Interop.Word'
# Find the assembly so we can load it into the GAC
$found = Get-ChildItem -Recurse -Filter 'Microsoft.Office.Interop.Word.dll' -ErrorAction SilentlyContinue
if ($found) {
Write-Verbose -Message "Assembly 'Microsoft.Office.Interop.Word.dll' found! Loading assembly"
}
else {
Write-Host -ForegroundColor Red "Assembly 'Microsoft.Office.Interop.Word.dll' NOT found! Trying to install from the NuGet repository!"
Write-Host -ForegroundColor Red "Installing and registering Nuget as the Package Sourec / Provider!"
Find-PackageProvider -Name NuGet | Install-PackageProvider -Force
Register-PackageSource -Name nuget.org -Location https://www.nuget.org/api/v2 -ProviderName NuGet
Install-Package -Name Microsoft.Office.Interop.Word -Source 'nuget.org' -Force
return
}
Add-Type -Path (Join-Path -Path $found.Directory.FullName -ChildPath 'Microsoft.Office.Interop.Word.dll')
Set-Location $currentLocation
}
else {
Write-Verbose "Assembly 'Microsoft.Office.Interop.Word.dll' found and is already loaded"
}
}
}
catch {
Write-Host -ForegroundColor Red "ERROR: $_"
return
}
# Generate random document text
Write-Host -ForegroundColor Green "File size selection: $($FileSize)`r`nNumber of words to use: $($NumberOfWords)"
1..$NumberOfWords | ForEach-Object {
$text = -join ((65..90) + (97..122) | Get-Random -Count 5 | ForEach-Object { [char]$_ })
$script:paragraph += $text + " " }
if ($FileSize -eq "Small") { $script:paragraph += $script:paragraph * 500 }
elseif ($FileSize -eq "Medium") { $script:paragraph += $script:paragraph * 750 }
elseif ($FileSize -eq "Large") { $script:paragraph += $script:paragraph * 5000 }
elseif ($FileSize -eq "Massive") { $script:paragraph += $script:paragraph * 50000 }
elseif ($FileSize -eq "StupidLarge") { $script:paragraph += $script:paragraph * 1000000 }
Write-Host -ForegroundColor Green "Number of files to create: $($NumberOfFilesToCreate)"
foreach ($fileType in $DefaultType) {
1..$NumberOfFilesToCreate | ForEach-Object {
$filename = ""
(65..90) + (97..122) | Get-Random -Count 10 | ForEach-Object { $filename += [char]$_ }
switch ($fileType) {
"Email" {
try {
$mailMessage = New-Object System.Net.Mail.MailMessage
$mailMessage.From = New-Object System.Net.Mail.MailAddress($MailFrom)
$mailMessage.To.Add($MailTo)
$mailMessage.Subject = "Test .eml file-$script:counter"
$mailMessage.Body = $script:paragraph
$smtpClient = New-Object System.Net.Mail.SmtpClient
$smtpClient.DeliveryMethod = [System.Net.Mail.SmtpDeliveryMethod]::SpecifiedPickupDirectory
$smtpClient.PickupDirectoryLocation = $EmailOutputPath
$smtpClient.Send($mailMessage)
$smtpClient.Dispose()
$mailMessage.Dispose()
Write-Verbose "Email $($emailCounter) created"
$emailCounter ++
}
catch {
Write-Host -ForegroundColor Red "Word Error: $_"
return
}
}
"Excel" {
try {
$null = Start-Job -ScriptBlock {
param ( $ExcelOutputPath, $filename, $script:counter)
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $false
$workbook = $excel.Workbooks.Add()
$worksheet = $workbook.Worksheets.Item(1)
$worksheet.Name = 'Worksheet 1'
$row = 1
$column = 1
$worksheet.Cells.Item($row, $column) = 'Row 1'
# Replace spaces with comma's and split the string to covert string to an array - each item separated by the comma
$temp = $Script:paragraph -replace ' ', ','
$records = $temp.split(',')
$i = 3
# SLOWEST and MOST expensive operation right here
foreach ($record in $records) {
$excel.cells.item($i, 1) = $record
$i++
}
$xlsFile = "ExcelDoc-$fileName-$script:counter.xls"
[string]$savePath = Join-Path -Path $ExcelOutputPath -ChildPath $xlsFile
$workbook.SaveAs($savePath)
$excel.Quit()
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)
} -ArgumentList $ExcelOutputPath, $filename, $script:counter
}
catch {
Write-Host -ForegroundColor Red "Excel Error: $_"
return
}
}
"Text" {
try {
$txtFile = "TextDoc-$fileName-$script:counter.txt"
$null = New-Item -Path $TextOutputPath -Name $txtFile -ItemType File -Value $paragraph -ErrorAction Stop
Write-Verbose "Text doc created: {0}"
}
catch {
Write-Host -ForegroundColor Red "Text Error: $_"
return
}
}
"Pdf" {
try {
$customObject = New-Document
$pdFile = "testpdf-$fileName-$script:counter.pdf"
[string]$savePath = Join-Path -Path $PdfOutputPath -ChildPath $pdFile
$saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatPDF")
$customObject.document.SaveAs($savePath, $saveFormat)
$customObject.document.Close([Microsoft.Office.Interop.Word.WdSaveOptions]::wdDoNotSaveChanges)
$customObject.word.Quit()
Write-Verbose "Pdf created: $($pdFile)"
}
catch {
Write-Host -ForegroundColor Red "PDF Error: $_"
return
}
}
"Word" {
try {
$customObject = New-Document
$wordFile = "WordDoc-$fileName-$script:counter.docx"
[string]$savePath = Join-Path -Path $WordOutputPath -ChildPath $wordFile
$saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatDocumentDefault")
$customObject.document.SaveAs($savePath)
$customObject.document.Close()
$customObject.word.Quit()
Write-Verbose "Word doc created: $($wordFile)"
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($customObject.word)
}
catch {
Write-Host -ForegroundColor Red "Word Error: $_"
return
}
}
}
$script:counter ++
}
}
# All jobs are kicked off. Clean up all completed and failed jobs
Write-Host -ForegroundColor Green "Cleaning up background jobs"
while (Get-Job | Where-Object PSJobTypeName -eq 'BackgroundJob') {
foreach ($job in (Get-Job)) {
if (($job.PsJobTypeName -eq 'BackgroundJob') -and ($job.state -eq 'Completed') -or ($job.state -eq 'Failed')) {
Write-Verbose "Removing Job Id: $($Job.id)"
Remove-Job -Id $Job.id
}
}
}
}
end {
Write-Host -ForegroundColor Green "File generation process complete!"
}
}
function New-Document {
<#
.SYNOPSIS
Create word document
.DESCRIPTION
Creates a word document that can be exported as pdf
.PARAMETER EnableException
Disables user-friendly warnings and enables the throwing of exceptions. This is less user friendly, but allows catching exceptions in calling scripts.
.EXAMPLE
None
.NOTES
Internal function
#>
[OutputType('PSCustomObject')]
[cmdletbinding()]
param()
process {
try {
# Create a Word document instance
$word = New-Object -ComObject word.application
$word.Visible = $false
$script:DisposeOfWordObject = $true
$document = $word.documents.add()
$selection = $word.Selection
$selection.Style = "Normal"
$Selection.TypeParagraph()
$selection.TypeText($script:paragraph)
}
catch {
Write-Host -ForegroundColor Red "Word Application Error: $_"
return
}
# Return the document
[PSCustomObject] @{
Document = $document
Word = $word
}
}
}