Last active
May 27, 2021 10:58
-
-
Save gravejester/0744a4df8daba48138431b6a0ee03e20 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function New-DummyFile { | |
<# | |
.SYNOPSIS | |
Create dummy file(s). | |
.DESCRIPTION | |
Quickly create a set of dummy files, with completely randomized names, | |
size and folder structure. | |
.EXAMPLE | |
'C:\Demo' | New-DummyFile -Files 10 -min 1kb -max 10kb | |
.EXAMPLE | |
New-DummyFile -Path 'C:\Demo' -Files 20 -MinFileSize 1mb -MaxFileSize 50mb -MaxSubFolders 3 | |
.EXAMPLE | |
New-DummyFile -Path 'C:\Demo' -Files 100 -Min 1GB -Max 1GB -NoFilesInRoot -MaxSubFolders 5 | |
.LINK | |
https://communary.wordpress.com/ | |
.NOTES | |
Author: Øyvind Kallstad | |
Date: 05.07.2016 | |
Version: 1.0 | |
Dependencies: New-Password | |
#> | |
[CmdletBinding()] | |
param ( | |
# Path to use as a root for creating dummy file(s). | |
[Parameter(Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] | |
[ValidateNotNullOrEmpty()] | |
[string] $Path = (Get-Location).Path, | |
# Number of files to create. Default value is 1. | |
[Parameter(Position = 1)] | |
[ValidateRange(1,[uint32]::MaxValue)] | |
[uint32] $Files = 1, | |
# File extension to use for generated files. Default value is 'df'. | |
[Parameter()] | |
[ValidateNotNullOrEmpty()] | |
[string] $FileExtension = 'df', | |
# Maximum number of sub folders to create. Default value is 0, meaning no sub folders will be created. | |
[Parameter()] | |
[ValidateRange(0,[uint32]::MaxValue)] | |
[uint32] $MaxSubFolders = 0, | |
[Parameter()] | |
[switch] $NoFilesInRoot, | |
# Minimum file size for the created file(s). | |
[Parameter()] | |
[ValidateRange(0,[uint32]::MaxValue)] | |
[uint32] $MinFileSize = 0, | |
# Maximum file size for the created file(s). | |
[Parameter()] | |
[Alias('Max')] | |
[ValidateRange(0,[uint32]::MaxValue)] | |
[uint32] $MaxFileSize = 0 | |
) | |
function New-File { | |
[CmdletBinding()] | |
param ( | |
[Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] | |
[ValidateNotNullOrEmpty()] | |
[string] $Path, | |
[Parameter()] | |
[ValidateRange(0,[uint32]::MaxValue)] | |
[uint32] $Size = 0 | |
) | |
try { | |
$fs = New-Object -TypeName System.IO.FileStream -ArgumentList ($Path, [System.IO.FileMode]::CreateNew) | |
$fs.SetLength($Size) | |
} | |
catch { | |
Write-Warning $_.Exception.Message | |
} | |
finally { | |
$fs.Close() | |
} | |
} | |
# Check dependencies | |
if (-not (Test-Path -Path 'function:\New-Password')) { | |
Write-Warning 'This function is will not work without New-Password. (https://github.com/gravejester/Communary.ToolBox)' | |
break | |
} | |
# Validate path | |
if (-not ($resolvedPath = Resolve-Path -Path $Path)) { | |
Write-Warning "Unable to resolve '$($resolvedPath)'" | |
} | |
else { | |
$Path = $resolvedPath | |
} | |
# NoFilesInRoot switch cannot be used together with MaxSubFolders = 0, for obvious reasons | |
if ($NoFilesInRoot -and $MaxSubFolders -eq 0) { | |
Write-Warning 'When using the NoFilesInRoot parameter, the MaxSubFolders value must be larger than 0.' | |
break | |
} | |
# validate min/max file size values | |
if ($MaxFileSize -lt $MinFileSize) { | |
Write-Warning 'MaxFileSize cannot be less than MinFileSize.' | |
break | |
} | |
# figure out how many files to create | |
if ($NoFilesInRoot) { | |
[uint32]$filesToCreate = 0 | |
} | |
elseif ($Files -eq 1) { | |
[uint32]$filesToCreate = 1 | |
} | |
elseif ($MaxSubFolders -eq 0) { | |
[uint32]$filesToCreate = $Files | |
} | |
else { | |
[uint32]$filesToCreate = Get-Random -Minimum 1 -Maximum $Files | |
} | |
$filesLeftToCreate = $Files - $filesToCreate | |
if ($filesToCreate -gt 0) { | |
# create files | |
for ($i = 1; $i -le $filesToCreate; $i++) { | |
$thisFileName = (New-Password -Type Pronounceable -WarningAction SilentlyContinue -Verbose:$false) + ".$($FileExtension)" | |
$thisFilePath = Join-Path -Path $Path -ChildPath $thisFileName | |
Write-Verbose "Creating $(Join-Path -Path $Path -ChildPath $thisFileName)" | |
if (-not(Test-Path -Path $Path)) { | |
$null = New-Item -Path $Path -ItemType Directory -Force | |
} | |
# figure out the file size | |
if ($MaxFileSize -eq $MinFileSize) { | |
[uint32]$thisFileSize = $MinFileSize | |
} | |
else { | |
[uint32]$thisFileSize = Get-Random -Minimum $MinFileSize -Maximum $MaxFileSize | |
} | |
New-File -Path $thisFilePath -Size $thisFileSize | |
} | |
} | |
# no point in creating sub folders if we are not going | |
# to be creating any more files | |
if ($filesLeftToCreate -gt 0) { | |
# figure out how many folders to create here | |
if ($MaxSubFolders -gt 1) { | |
[uint32]$foldersToCreate = Get-Random -Minimum 1 -Maximum $MaxSubFolders | |
} | |
else { | |
[uint32]$foldersToCreate = $MaxSubFolders | |
} | |
# randomly spread the remaining files accross the number of sub folders | |
$filesArray = @() | |
for ($i = 0; $i -lt $foldersToCreate; $i++) { | |
if ($i -eq ($foldersToCreate - 1)) { | |
$filesArray += $filesLeftToCreate | |
} | |
else { | |
if ($filesLeftToCreate -gt 1) { | |
[uint32]$randomNumber = Get-Random -Minimum 1 -Maximum $filesLeftToCreate | |
$filesArray += $randomNumber | |
$filesLeftToCreate = $filesLeftToCreate - $randomNumber | |
} | |
elseif ($filesLeftToCreate -eq 1) { | |
$filesArray += $filesLeftToCreate | |
$filesLeftToCreate = $filesLeftToCreate - 1 | |
} | |
} | |
} | |
if ($foldersToCreate -gt 0) { | |
# create folders | |
for ($i = 0; $i -lt $foldersToCreate; $i++) { | |
$thisFolderName = New-Password -Type Pronounceable -WarningAction SilentlyContinue -Verbose:$false | |
$thisPath = Join-Path $Path -ChildPath $thisFolderName | |
if ($filesArray[$i] -gt 0) { | |
New-DummyFile -Path $thisPath -Files $filesArray[$i] -Verbose:$VerbosePreference -MaxSubFolders $MaxSubFolders -MinFileSize $MinFileSize -MaxFileSize $MaxFileSize | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment