Created
October 31, 2019 00:35
Prototype wrapper for DD
This file contains 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
$ChocoInventory = choco list -lo | |
if ("$ChocoInventory" -notmatch 'qemu-img') { | |
choco install qemu-img -y | |
} | |
if ("$ChocoInventory" -notmatch '\ git\ ') { | |
choco install git -y | |
} | |
$env:path += ';c:\program files\git\usr\bin' | |
class ValidatePathExistsAttribute : System.Management.Automation.ValidateArgumentsAttribute | |
{ | |
[void] Validate([object]$arguments, [System.Management.Automation.EngineIntrinsics]$engineIntrinsics) | |
{ | |
$path = $arguments | |
if([string]::IsNullOrWhiteSpace($path)) | |
{ | |
Throw [System.ArgumentNullException]::new() | |
} | |
if(-not (Test-Path -Path $path)) | |
{ | |
Throw [System.IO.FileNotFoundException]::new() | |
} | |
} | |
} | |
function Convert-RawImageToVHDX { | |
Param( | |
[Parameter(Mandatory)] | |
[ValidatePathExists()] | |
[System.IO.FileInfo]$Path, | |
[Parameter()] | |
[System.IO.FileInfo]$OutputPath | |
) | |
if (-not $OutputPath) { | |
$OutputPath = -join((($path.Directory,$path.basename) -join [IO.Path]::DirectorySeparatorChar),'.vhdx') | |
} | |
qemu-img.exe convert "$Path" -O vhdx -o subformat=dynamic "$OutputPath" | |
} | |
function Backup-PhysicalDrive { | |
Param( | |
[Parameter(Mandatory)] | |
[ValidateSet('ToImage','ToDrive')] | |
[string]$Mode | |
) | |
#$env:Path += ';C:\Program Files\Git\usr\bin' | |
$DiskDrives = Gwmi Win32_diskdrive | select PSComputerName,index,Caption,DeviceID,pnpdeviceID,BytesPerSector,InterfaceType,Size,TotalSectors,SerialNumber | |
$SourceDrives = $DiskDrives | Out-GridView -OutputMode Multiple -Title 'Select a Source Drive' | |
$ChangeSet = $( | |
if ($mode -eq 'ToImage') { | |
$SourceDrives | Select *,@{N='OutputName';e={Read-Host -Prompt "Output Filename/Path for $($_.Caption) at index $($_.index) (should end in .img)"}} | |
} elseif ($Mode -eq 'ToDrive'){ | |
$SourceDrives | Select *,@{N='OutputName';e={ | |
$ThisSource = $_.DeviceID | |
$DiskDrives | Out-GridView -OutputMode Single -Title "Select a target drive to which $ThisSource will be cloned" | select -expandproperty DeviceID | |
}} | |
$SourceDrives | ?{$_.DeviceID -eq $_.OutputName} | |
} | |
) | |
$ChangeSet | %{ | |
[scriptblock]::Create(('dd if={0} of={1} bs={2}' -f $_.DeviceID,$_.OutputName,'512k' <#$_.BytesPerSector#>)).Invoke() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note:
Writing to disk as in the 'ToDrive' scenario is not currently working. Am trying to figure out why.
Error thrown is that the target is a directory not a file.