cdisk.ps1


<#PSScriptInfo
 
.VERSION 1.0
 
.GUID 8f0270e2-d577-4a9d-b67c-86d626bc46dd
 
.AUTHOR Cristian
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
.PRIVATEDATA
 
#>


<#
 
.DESCRIPTION
 Script to do a wipe of all data on a drive.
 
#>
 

<#
.DESCRIPTION
 Script to do a wipe of all data on a drive.
#>


function startEraser{
    clear-host 

    $drives = @{}
    $global:counter = 0
    $global:field = 0
    $global:erase = $false

    Write-host ""
    Write-host " > SELECT THE TARGET DRIVE FOR THE DELETION:"
    Write-host ""

    foreach($drive in Get-Disk | Select-Object){
        $drives[$global:counter] = $drive
        $global:counter++
    }

    for($i = 0; $i -le ($drives.count-1); $i++){
        $size = [int]($drives[$i].size/1024/1024/1024)

        #SETTING SELECTING FIELD THE FIRST POSITION AS DEFAULT
        if($i -eq 0){
            Write-Host " "($i+1) $drives[$i].FriendlyName" " -ForegroundColor Black -BackgroundColor yellow -NoNewline
            Write-Host ("["+$size+"GB] " -replace '\s*') -ForegroundColor black -BackgroundColor white
        } else {
            Write-Host " "($i+1) $drives[$i].FriendlyName" " -NoNewline
            Write-Host ("["+$size+"GB] " -replace '\s*')
        }
    }

    Write-Host ""
    Write-host "Do not remove any device once the program has started. " -ForegroundColor DarkGray
    Write-host "Sometimes disk reading can be slow. Press Ctrl + C & restart. " -ForegroundColor DarkGray

    function getField {
        Param ($option)

        Write-host ""
        Write-host " > SELECT THE TARGET DRIVE FOR THE DELETION:"
        Write-host ""

        #UP KEY
        if($option -eq 0){
            
            if($global:field -eq 0){
                $global:field = 0      #LAST AVAILABLE POSITION
            } else {
                $global:field = $global:field - 1
            }
        }

        #DOWN KEY
        if($option -eq 1){
            
            if($global:field -eq $global:counter-1){
                $global:field = $global:counter-1    #LAST AVAILABLE POSITION
            } else {
                $global:field++
            }
        }

        for($i = 0; $i -le ($drives.count-1); $i++){
            $size = [int]($drives[$i].size/1024/1024/1024)

            # SETTING SELECTED AND NON SELECTED FIELDS
            if($global:field -eq $i){
                Write-Host " "($i+1) $drives[$i].FriendlyName" " -ForegroundColor black -BackgroundColor yellow -NoNewline
                Write-Host ("["+$size+"GB] " -replace '\s*') -ForegroundColor Black -BackgroundColor White
            } else {
                Write-Host " "($i+1) $drives[$i].FriendlyName" " -NoNewline
                Write-Host ("["+$size+"GB] " -replace '\s*')
            }
        }

        Write-Host ""
        Write-host "Do not remove any device once the program has started. " -ForegroundColor DarkGray
        Write-host "Sometimes disk reading can be slow. Press Ctrl + C & restart. " -ForegroundColor DarkGray
    }

    #SETTING UP OPTIONS MENU
    function optionMenu{
        Param ($drive)

        #GETTING DRIVE SIZE
        $size = [int]($drive.size/1024/1024/1024)

        Write-Host $drive.FriendlyName -ForegroundColor Black -BackgroundColor Yellow -NoNewline
        Write-Host ("["+$size+"GB] " -replace '\s*') -ForegroundColor black -BackgroundColor white
        Write-Host ""
        Write-Host " Serial: " -NoNewline
        Write-Host $drive.SerialNumber -ForegroundColor DarkGray
        Write-Host " Status: " -NoNewline
        
        #SET GREEN IF DRIVE IS HEALTHLY
        if($drive.HealthStatus -eq "Healthy"){
            Write-Host $drive.HealthStatus -ForegroundColor Green
        } else {
            Write-Host $drive.HealthStatus -ForegroundColor DarkGray
        }

        Write-Host " Manufacturer: " -NoNewline
        Write-Host $drive.Manufacturer -ForegroundColor DarkGray
        Write-Host " Type: " -NoNewline
        Write-Host $drive.PartitionStyle -ForegroundColor DarkGray
        Write-Host ""
        Write-Host " Cancel " -ForegroundColor Black -BackgroundColor Yellow -NoNewline
        Write-Host " " -NoNewline
        Write-Host " Erase "
    }

    function updateMenu{
        Param($option, $drive)

        #LEFT KEY
        if($option -eq 0){
            #GETTING DRIVE SIZE
            $size = [int]($drive.size/1024/1024/1024)

            #SETTING UP OPTIONS STARTUP MENU
            Write-Host $drive.FriendlyName -ForegroundColor Black -BackgroundColor Yellow -NoNewline
            Write-Host ("["+$size+"GB] " -replace '\s*') -ForegroundColor black -BackgroundColor white
            Write-Host ""
            Write-Host " Serial: " -NoNewline
            Write-Host $drive.SerialNumber -ForegroundColor DarkGray
            Write-Host " Status: " -NoNewline

            #SET GREEN IF DRIVE IS HEALTHLY
            if($drive.HealthStatus -eq "Healthy"){
                Write-Host $drive.HealthStatus -ForegroundColor Green
            } else {
                Write-Host $drive.HealthStatus -ForegroundColor DarkGray
            }

            Write-Host " Manufacturer: " -NoNewline
            Write-Host $drive.Manufacturer -ForegroundColor DarkGray
            Write-Host " Type: " -NoNewline
            Write-Host $drive.PartitionStyle -ForegroundColor DarkGray
            Write-Host ""
            Write-Host " Cancel " -ForegroundColor Black -BackgroundColor Yellow -NoNewline
            Write-Host " " -NoNewline
            Write-Host " Erase " 
            #CHANGING ERASE BOOLEAN STATUS
            $global:erase = $false 
        }

        #RIGHT KEY
        if($option -eq 1){
            #GETTING DRIVE SIZE
            $size = [int]($drive.size/1024/1024/1024)

            #SETTING UP OPTIONS STARTUP MENU
            Write-Host $drive.FriendlyName -ForegroundColor Black -BackgroundColor Yellow -NoNewline
            Write-Host ("["+$size+"GB] " -replace '\s*') -ForegroundColor black -BackgroundColor white
            Write-Host ""
            Write-Host " Serial: " -NoNewline
            Write-Host $drive.SerialNumber -ForegroundColor DarkGray
            Write-Host " Status: " -NoNewline

            #SET GREEN IF DRIVE IS HEALTHLY
            if($drive.HealthStatus -eq "Healthy"){
                Write-Host $drive.HealthStatus -ForegroundColor Green
            } else {
                Write-Host $drive.HealthStatus -ForegroundColor DarkGray
            }

            Write-Host " Manufacturer: " -NoNewline
            Write-Host $drive.Manufacturer -ForegroundColor DarkGray
            Write-Host " Type: " -NoNewline
            Write-Host $drive.PartitionStyle -ForegroundColor DarkGray
            Write-Host ""
            Write-Host " Cancel " -NoNewline
            Write-Host " " -NoNewline
            Write-Host " Erase " -ForegroundColor Black -BackgroundColor Yellow 

            #CHANGING ERASE BOOLEAN STATUS
            $global:erase = $true
        }

        return $drive
    }

    #SCREEN 1: SELECT DRIVE
    do{
        if ([Console]::KeyAvailable){
            $keyInfo = [Console]::ReadKey($true)
            

            #KEY UP OPTION 0, PASSING TO GETFIELD FUNCTION()
            if($keyInfo.key -eq "UpArrow"){
                clear-host 
                getField -option 0
            }

            #KEY DOWN OPTION 1, PASSING TO GETFIELD FUNCTION()
            if($keyInfo.key -eq "DownArrow"){
                clear-host 
                getField -option 1
            }

            if($keyInfo.key -eq "Enter"){
                clear-host 
                optionMenu -drive $drives[$global:field]
                break
            }

        }

    } while ($true)

    #SCREEN 2: DRIVE OPTIONS
    $targetDrive = $null

    do{
           if ([Console]::KeyAvailable){
           $keyInfo = [Console]::ReadKey($true)
            

           #KEY DOWN OPTION 1, PASSING TO GETFIELD FUNCTION()
           if($keyInfo.key -eq "LeftArrow"){
               clear-host 
               $targetDrive = updateMenu -option 0 -drive $drives[$global:field]
           }

           #KEY UP OPTION 0, PASSING TO GETFIELD FUNCTION()
           if($keyInfo.key -eq "RightArrow"){
               clear-host 
               $targetDrive = updateMenu -option 1 -drive $drives[$global:field]
           }

           if($keyInfo.key -eq "Enter"){
               if($global:erase){
                   #ERASE DATA
                   Write-Host "ELIMIANDO :)"
                   startDeletion -drive $targetDrive
               } else {
                   #CANCELED. RESTARTING THE SCRIPT
                   startEraser
               }
               break
            }
        }
    } while ($true)
}

function deletionProcess {
    Param($drive)
    clear-host


    $line = ""
    $status = " Accesing the disk"
    $succeed
    #PROGRESS 2
    for ($i = 1; $i -lt 21; $i++){

        #SETTING STATUS MESSAGE & WIPING THE DISK
        if($i -eq 13){

            Write-Host " Erasing the drive"
            #SHOWING THE PROGRESS BAR IN DISC INTERACTION
            $line = $line + " "
            Write-Host " [" -NoNewline
            Write-Host $line -ForegroundColor Black -BackgroundColor Yellow -NoNewline
            $append = 20-$i
            $sum = ""
            for ($indx = 0; $indx -lt $append; $indx++){
                $sum = $sum + " "
            }
            Write-Host $sum"]" -NoNewline

            #SHOW PERCENTAGE
            [int]$percentage = ($i*100)/20
            Write-Host " "$percentage"%" -ForegroundColor Yellow

            $global:ProgressPreference = 'SilentlyContinue'
            clear-disk -number $drive.Number -RemoveData -Confirm:$false

            if($?){
                #COMMAND SUCCEEDED
                $succeed = $true
            }else {
                #COMMAND FAILED
                $succeed = $false
            }

            $status = " Finishing"
        } else {
            Write-Host $status

            #SHOW BAR NORMALLY
            $line = $line + " "
            Write-Host " [" -NoNewline
            Write-Host $line -ForegroundColor Black -BackgroundColor Yellow -NoNewline
            $append = 20-$i
            $sum = ""
            for ($indx = 0; $indx -lt $append; $indx++){
                $sum = $sum + " "
            }
            Write-Host $sum"]" -NoNewline

            #SHOW PERCENTAGE
            [int]$percentage = ($i*100)/20
            Write-Host " "$percentage"%" -ForegroundColor Yellow
            Start-sleep -Milliseconds 250
        }



        clear-host 
    }

    if($succeed){
        Write-Host " CDISK> Completed. The drive " -NoNewline    
        Write-Host $drive.FriendlyName -ForegroundColor Yellow -NoNewline 
        Write-Host " was successfully erased" 
        Write-Host ""
    } else {
        Write-Host " CDISK> Something was wrong, unable to wipe te drive. " -ForegroundColor red -BackgroundColor black 
    }
}

function startDeletion {
    Param($drive)
    Clear-host 

    #FIRST DELETION CONFIRMATION
    $confirmation = Read-Host "CDISK> Type 'yes' to confirm the deletion"
    if($confirmation -eq "yes"){

        #SECOND DELETION CONFIRMATION
        Write-Host "CDISK> Disk " -NoNewline
        Write-Host $drive.FriendlyName -ForegroundColor yellow -NoNewline
        Write-Host " with " -NoNewline
        Write-Host ([int]($drive.Size/1024/1024/1024))"GB"-ForegroundColor yellow -NoNewline
        Write-Host " will be completely erased" 
        $secondConfirmation = Read-Host "-> Type 'agree' to confirm the deletion"
        if($secondConfirmation -eq "agree"){
            $starting = "CDISK> Starting the deletion process" 
            for($i = 0; $i -lt 4; $i++){
                Clear-Host
                #REWRITTING CONSOLE COMMANDS FOR THE LOOP
                Write-Host "CDISK> Type 'yes' to confirm the deletion: yes"
                Write-Host "CDISK> Disk " -NoNewline
                Write-Host $drive.FriendlyName -ForegroundColor yellow -NoNewline
                Write-Host " with " -NoNewline
                Write-Host ([int]($drive.Size/1024/1024/1024))"GB"-ForegroundColor yellow -NoNewline
                Write-Host " will be completely erased" 
                Write-Host "-> Type 'agree' to confirm the deletion: agree"

                #DOTS ANIMATION
                Write-Host $starting -ForegroundColor Yellow
                $starting = $starting+"." 
                Start-Sleep -Milliseconds 500
            } 

            #START DELETION PROCESS
            deletionProcess -drive $drive
        } else {
            Write-Host "CDISK> Operation has been aborted" -ForegroundColor Red -BackgroundColor Black
        }
    } else {
        Write-Host "CDISK> Operation has been aborted" -ForegroundColor Red -BackgroundColor Black
    }
}

$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if($currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)){
    startEraser
} else {
    Write-Host "This script requires to be run as administrator" -ForegroundColor red -BackgroundColor black 
}

#RESTART THE WRITE-PROGRESS PREFERENCES
$global:ProgressPreference = 'Continue'