Differences
This shows you the differences between two versions of the page.
windows_wiki:powershell_-_write_zeroes_to_drive [2016/06/13 15:24] vitrek [Write Zeroes to Drive] |
windows_wiki:powershell_-_write_zeroes_to_drive [2019/05/25 23:50] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Powershell - Write Zeroes To Drive ====== | ||
- | |||
- | **General Information** | ||
- | |||
- | This script is intended for usage when you have a clustered or shared volume that is connected to a san that is reporting full even though Windows has data free on the drive. | ||
- | |||
- | This behavior is due to windows not wiping freespace on the drive. Most san's do not understand free space if the space does not contain only 0's | ||
- | |||
- | ====== Write Zeroes to Drive ====== | ||
- | |||
- | Section 1 content. | ||
- | < | ||
- | |||
- | <# | ||
- | .SYNOPSIS | ||
- | | ||
- | | ||
- | |||
- | .DESCRIPTION | ||
- | | ||
- | | ||
- | This allows a storage appliance that is thin provisioned to mark that drive | ||
- | space as unused and reclaim the space on the physical disks. | ||
- | |||
- | .PARAMETER Root | ||
- | The folder to create the zeroed out file in. This can be a drive root (c:\) | ||
- | or a mounted folder (m: | ||
- | | ||
- | |||
- | .PARAMETER PercentFree | ||
- | A float representing the percentage of total volume space to leave free. The | ||
- | | ||
- | |||
- | .EXAMPLE | ||
- | | ||
- | |||
- | This will create a file of all zeroes called c: | ||
- | c drive up to 95% of its capacity. | ||
- | |||
- | .EXAMPLE | ||
- | | ||
- | |||
- | This will create a file of all zeroes called | ||
- | | ||
- | | ||
- | |||
- | .EXAMPLE | ||
- | | ||
- | |||
- | This will get a list of all local disks (type=3) and fill each one up to 95% | ||
- | of their capacity with zeroes. | ||
- | |||
- | .NOTES | ||
- | You must be running as a user that has permissions to write to the root of the | ||
- | | ||
- | using the default Windows permissions on the C drive. | ||
- | #> | ||
- | param( | ||
- | [Parameter(Mandatory=$true, | ||
- | [ValidateNotNullOrEmpty()] | ||
- | [Alias(" | ||
- | $Root, | ||
- | [Parameter(Mandatory=$false)] | ||
- | [ValidateRange(0, | ||
- | $PercentFree =.05 | ||
- | ) | ||
- | process{ | ||
- | #Convert the $Root value to a valid WMI filter string | ||
- | $FixedRoot = ($Root.Trim(" | ||
- | $FileName = " | ||
- | $FilePath = Join-Path $Root $FileName | ||
- | | ||
- | #Check and make sure the file doesn' | ||
- | if( (Test-Path $FilePath) ) { | ||
- | Write-Error -Message "The file $FilePath already exists, please delete the file and try again" | ||
- | } else { | ||
- | #Get a reference to the volume so we can calculate the desired file size later | ||
- | $Volume = gwmi win32_volume -filter " | ||
- | if($Volume) { | ||
- | #I have not tested for the optimum IO size ($ArraySize), | ||
- | $ArraySize = 64kb | ||
- | #Calculate the amount of space to leave on the disk | ||
- | $SpaceToLeave = $Volume.Capacity * $PercentFree | ||
- | #Calculate the file size needed to leave the desired amount of space | ||
- | $FileSize = $Volume.FreeSpace - $SpacetoLeave | ||
- | #Create an array of zeroes to write to disk | ||
- | $ZeroArray = new-object byte[]($ArraySize) | ||
- | | ||
- | #Open a file stream to our file | ||
- | $Stream = [io.File]:: | ||
- | #Start a try/finally block so we don't leak file handles if any exceptions occur | ||
- | try { | ||
- | #Keep track of how much data we've written to the file | ||
- | $CurFileSize = 0 | ||
- | while($CurFileSize -lt $FileSize) { | ||
- | #Write the entire zero array buffer out to the file stream | ||
- | $Stream.Write($ZeroArray, | ||
- | #Increment our file size by the amount of data written to disk | ||
- | $CurFileSize += $ZeroArray.Length | ||
- | } | ||
- | } finally { | ||
- | #always close our file stream, even if an exception occurred | ||
- | if($Stream) { | ||
- | $Stream.Close() | ||
- | } | ||
- | #always delete the file if we created it, even if an exception occurred | ||
- | if( (Test-Path $FilePath) ) { | ||
- | del $FilePath | ||
- | } | ||
- | } | ||
- | } else { | ||
- | Write-Error " | ||
- | } | ||
- | } | ||
- | } | ||
- | |||
- | </ | ||
- | See https:// | ||