Skip to content

Instantly share code, notes, and snippets.

@Chirishman
Created October 31, 2019 00:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Chirishman/772e3208ec0bad25dfc47b5fbfc3fd39 to your computer and use it in GitHub Desktop.
Save Chirishman/772e3208ec0bad25dfc47b5fbfc3fd39 to your computer and use it in GitHub Desktop.
Prototype wrapper for DD
$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()
}
}
@Chirishman
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment