Wednesday, 18 June 2014

PowerShell Create a Large file with random content and fixed size

In order to test storage its sometime handy to be able to generate large files with random content. This allows us to test such things as

Deduplication engines
Thin Provisioning
SDelete
VMWware space reclaimation VMFKTOOL
Vmware PunchtoZero features

To this end i created this powershell script to create random content into a file. There are some thirdparty applications and other utils out there but i found them wanting and often not containing unique random data. There tended to be alot of whitespace. 

Powershell means no install required and we can customise. 
This script grabs a file full of content and randomly picks out the data and then writes it to a file until the file is the desired size. This script is not the quickest but it should be fast enough. It created a 1GB file for me in about 2 minutes.

You will need to create a c:\temp\content.txt file containing random words or sentences. 

$Input = get-content C:\temp\content.txt
$OutputFile = "Abigfile.txt"
$stream = [System.IO.StreamWriter] $Outputfile
   do {
   $Counter = 0
       do {
           $Counter=$Counter+1
           $a = $Input | get-random
           $Text = $Text + $a
           }
       While ($Counter -le 50)
   $stream.WriteLine($text)
   $FileSize = (Get-Item $Outputfile).length
   }
   While ($FileSize -le 2000000000)
$stream.close()
$WshShell = new-object -comobject Wscript.shell
$WshShell.Popup("Complete",5,"Title")

No comments:

Post a Comment