Skip to content

Instantly share code, notes, and snippets.

@JohnLBevan
Last active March 4, 2020 10:36
Show Gist options
  • Select an option

  • Save JohnLBevan/efbae9592ded883cc647d228e4aead92 to your computer and use it in GitHub Desktop.

Select an option

Save JohnLBevan/efbae9592ded883cc647d228e4aead92 to your computer and use it in GitHub Desktop.
Create a binary file of a given size full of random data - useful when we want to test things like disk io / network performance of copying files, ensuring there are no patterns allowing for unwanted optimizations to kick in
function New-DummyFile {
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')]
Param (
# full path to file to be created
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[string]$Path
,
# the size of the file to be created (you can use shorthand terms like 1GB to populate this)
[Parameter(Mandatory = $true)]
[UInt64]$FileSizeBytes # use uint to avoid negatives; use int64 to ensure we can hold values in terrabytes
,
# create the file without any prompts, regardless of ConfirmPreference
[Parameter()]
[switch]$Force
)
Begin {
[Int32]$DefaultPageSize = 1MB
[Boolean]$ClobberYesToAll = $Force.IsPresent
[Boolean]$ClobberNoToAll = $false
}
Process {
[Boolean]$fileExists = Test-Path -Path $Path -PathType Leaf
if ((-not $fileExists) -or ($PSCmdlet.ShouldContinue("File already exists: $Path. Overwrite?", 'Overwrite File?', [ref]$ClobberYesToAll, [ref]$ClobberNoToAll))) { # if the file exists then prompt the user on whether to delete it
# create a new file
$splat = $PSBoundParameters
$splat.Remove('FileSizeBytes') | Out-Null
$splat.Add('ItemType', 'File')
if (!$splat.ContainsKey('Force')) {$splat.Add('Force', $true)} # to reach this point the user's confirmed overwrite above, so we can treat the rest as forced
$newItem = New-Item @splat
# write some random data to it. Check that the new item exists since what-if may have caused no output, in which case we don't want to risk anything here
if ($null -ne $newItem) {
try {
[System.IO.BinaryWriter]$writer = New-Object -TypeName 'System.IO.BinaryWriter' -ArgumentList ([System.IO.File]::Open(($newItem.FullName), [System.IO.FileMode]::Create))
[UInt64]$NoOfFullPages = [Math]::Floor($FileSizeBytes / $DefaultPageSize)
[Int32[]]$pageSizes = @(@($DefaultPageSize) * $NoOfFullPages)
[Int32]$Remainder = $FileSizeBytes - ($NoOfFullPages * $DefaultPageSize)
if ($Remainder -gt 0) {$pageSizes += @($Remainder)}
[System.Random]$rnd = New-Object -TypeName 'System.Random'
foreach ($page in $pageSizes) {
Write-Verbose "Writing page of $page bytes $($page.GetType()) to $($newItem.FullName)"
[Byte[]]$data = New-Object -TypeName 'Byte[]' -ArgumentList $page
$rnd.NextBytes($data);
$writer.Write($data)
}
$writer.Flush()
} finally {
if ($null -ne $writer) {
$writer.Dispose()
}
}
}
}
}
}
New-DummyFile -Path 'c:\temp\one.dat' -FileSizeBytes ((1GB)+5) -Verbose
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment