Kaynağa Gözat

add reverse imager

Tobias Simetsreiter 4 yıl önce
ebeveyn
işleme
6990387952

+ 67 - 0
WinImager/DiskToImage.ps1

@@ -0,0 +1,67 @@
+
+$libpath = [System.IO.Path]::Combine($MyInvocation.MyCommand.Definition, "..", "LibWinImager.psm1")
+$logfilename = "DiskToImage_"+([System.DateTime]::Now).ToFileTime()+".log"
+$logpath = [System.IO.Path]::Combine($MyInvocation.MyCommand.Definition, "..", "log", $logfilename)
+Import-Module -Name $libpath
+
+BecomeAdmin $MyInvocation
+
+Function Main()
+{
+
+    $ErrorActionPreference = "Stop"
+
+    $disk = GuiPhysDev
+    $disknum = $disk.Number
+    $diskname = $disk.FriendlyName
+    $diskserial = $disk.SerialNumber
+
+    if ((-not $disknum -is [int]) -or $disknum -eq $null -or $disknum -lt 0){
+        Write-Output "No Disk Selected"
+    } else {
+
+        $filename = GuiSaveFileName
+        $filename
+        if (($filename -eq $null) -or ($filename -eq "")){
+            Break
+        }
+
+        $dialog = @"
+Going to write Image of drive:
+
+"${diskname} ${diskserial}" 
+
+to File:
+
+"${filename}"
+
+Would you like to contine?
+"@ 
+        $dialog
+        $continue = AskContinue $dialog
+
+
+        if ($continue -eq "Yes"){
+
+            WinDD ("\\.\PHYSICALDRIVE{0}" -f $disknum) $filename -Create $true
+        }
+    }
+
+    $dialog = @"
+Write image 
+
+"${filename}"
+
+to another Drive?
+"@
+    $dialog
+    $continue = AskContinue $dialog
+
+}
+
+try{
+    Main | Tee-Object -FilePath $logpath
+} catch {
+    Write-Error $_.Exception | Tee-Object -FilePath $logpath -Append
+    pause
+}

+ 3 - 1
WinImager/FixCHS.ps1

@@ -1,7 +1,9 @@
 
 $libpath = [System.IO.Path]::Combine($MyInvocation.MyCommand.Definition, "..", "LibWinImager.psm1")
+
 $logfilename = "FixCHS_"+([System.DateTime]::Now).ToFileTime()+".log"
 $logpath = [System.IO.Path]::Combine($MyInvocation.MyCommand.Definition, "..", "log", $logfilename)
+
 Import-Module -Name $libpath
 
 BecomeAdmin $MyInvocation
@@ -42,5 +44,5 @@ try{
     Main | Tee-Object -FilePath $logpath
 } catch {
     Write-Error $_.Exception | Tee-Object -FilePath $logpath -Append
-    pause
 }
+pause

+ 3 - 2
WinImager/WinImager.ps1 → WinImager/ImageToDisk.ps1

@@ -1,6 +1,6 @@
 
 $libpath = [System.IO.Path]::Combine($MyInvocation.MyCommand.Definition, "..", "LibWinImager.psm1")
-$logfilename = "WinImager_"+([System.DateTime]::Now).ToFileTime()+".log"
+$logfilename = "ImageToDisk_"+([System.DateTime]::Now).ToFileTime()+".log"
 $logpath = [System.IO.Path]::Combine($MyInvocation.MyCommand.Definition, "..", "log", $logfilename)
 Import-Module -Name $libpath
 
@@ -11,7 +11,8 @@ Function Main()
 
     $ErrorActionPreference = "Stop"
 
-    $filename = GuiFileName
+    $filename = GuiOpenFileName
+    $filename
     if (($filename -eq $null) -or ($filename -eq "")){
         Break
     }

+ 40 - 18
WinImager/LibWinImager.psm1

@@ -1,5 +1,5 @@
 
-[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
+Add-Type -AssemblyName "System.Windows.Forms"
 
 Function BecomeAdmin($invocation){
     if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))  
@@ -21,33 +21,46 @@ Function MessageBox($text)
 }
 
 
-Function WinDD($inFileName, $outFileName, $blocksize = 5Mb) {
+Function WinDD($inFileName, $outFileName, $bs = 5Mb, $create = $false) {
 
     Write-Output ""
     Write-Output "" "Copy $inFileName to $outFileName with blocksize: $blocksize...."
-    $block = [System.Byte[]]::CreateInstance([System.Byte], $blocksize)
+    
+    $blocksize = $bs
+    $block = [System.Byte[]]::CreateInstance([System.Byte], $bs)
 
     try{
         $inFile = [System.IO.File]::Open($inFileName, [System.IO.FileMode]::Open)
-        $outFile = [System.IO.File]::Open($outFileName, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Write)
+
+        if ($inFile.Length -eq $null){
+
+            $inFileLength = (get-disk |
+                Where { ("\\.\PHYSICALDRIVE{0}" -f $_.Number) -eq $inFileName }).Size
+
+        } else {
+
+            $inFileLength = $inFile.Length
+
+        }
+
+        if ($create){
+            $filemode = [System.IO.FileMode]::OpenOrCreate
+        } else {
+            $filemode = [System.IO.FileMode]::Open
+        }
+
+        $outFile = [System.IO.File]::Open($outFileName, $filemode, [System.IO.FileAccess]::Write)
         $outFile.Seek(0,0) | Out-Null
         $inFile.Seek(0,0)  | Out-Null
 
-        $max =          [math]::Round($inFile.Length/1Mb)
+        $max =          [math]::Round($inFileLength/1Mb)
         $starttime =    [System.DateTime]::Now
-        while ($outFile.Position -lt $inFile.Length){
-
-            $remains = ($inFile.Length - $outFile.Position)
-
-            if($remains -lt $blocksize){
-                $blocksize = $remains
-                $block = [System.Byte[]]::CreateInstance([System.Byte], $blocksize)
-            }
+        while ($blocksize -gt 0){
 
-            $inFile.Read( $block, 0, $blocksize)    | Out-Null
+            $blocksize = $inFile.Read( $block, 0, $bs)
             $outFile.Write( $block , 0, $blocksize) | Out-Null
 
-            $percent = [math]::Min(100,[math]::Round(100 * $outFile.Position / $inFile.Length, 1))
+            $percent = [math]::Min(100,[math]::Round(100 * $outFile.Position / $inFileLength, 1))
             $done    = [math]::Round($outFile.Position/1Mb)
             $speed   = [math]::Round($done / ([System.DateTime]::Now - $startTime).TotalSeconds, 2)
             $status  = "{0,5}% {1,6}/{2} Mb {3} Mbps " -f ($percent,$done,$max,$speed)
@@ -69,7 +82,6 @@ Function WinDD($inFileName, $outFileName, $blocksize = 5Mb) {
         try {
             $outFile.Close()
         } catch {
-            $_.Exception
         }
         if ($ex -ne $null){throw $ex}
     }
@@ -113,7 +125,17 @@ y
 
 }
 
-Function GuiFileName($initialDirectory = "C:\", $title = "Select File")
+Function GuiSaveFileName($initialDirectory = "C:\", $title = "Save Image File")
+{  
+    $SaveFileDialog = New-Object System.Windows.Forms.SaveFileDialog
+    $SaveFileDialog.initialDirectory = $initialDirectory
+    $SaveFileDialog.filter = "Images (*.img;*.afp;*.usb;*.iso)| *.img;*.afp;*.usb;*.iso|All files (*.*)| *.*"
+    $SaveFileDialog.Title = $title
+    $SaveFileDialog.ShowDialog() | Out-Null
+    $SaveFileDialog.filename
+}
+
+Function GuiOpenFileName($initialDirectory = "C:\", $title = "Select Image File")
 {  
 
     $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
@@ -122,7 +144,7 @@ Function GuiFileName($initialDirectory = "C:\", $title = "Select File")
     $OpenFileDialog.Title = $title
     $OpenFileDialog.ShowDialog() | Out-Null
     $OpenFileDialog.filename
-} #end function Get-FileName
+}
 
 Function GuiPhysDev()
 {