Эх сурвалжийг харах

Some Fixing of Admin elevation

Tobias Simetsreiter 4 жил өмнө
parent
commit
c5a75db6fb

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+dist

+ 14 - 7
WinImager/LibWinImager.psm1

@@ -1,19 +1,21 @@
 
 Add-Type -AssemblyName "System.Windows.Forms"
 
-Function BecomeAdmin($invocation){
+$PSScriptRoot |Out-Host
+
+Function BecomeAdmin($script){
     if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))  
     {  
-        $arguments = "-ExecutionPolicy Bypass & '" +$invocation.mycommand.definition + "'"
+        $arguments = "-ExecutionPolicy Bypass & '" +$env:WINIMAGER + "' -Script " + $script
 		Hide-Console
         Start-Process powershell -Wait -Verb runAs -ArgumentList $arguments
         Break
     }
 }
 
-Function LogFilePath($invocation,$name){
+Function LogFilePath($name){
     $logfilename = $name+"_"+([System.DateTime]::Now).ToFileTime()+".log"
-    [System.IO.Path]::Combine($invocation.MyCommand.Definition, "..", "..","log", $logfilename)
+    [System.IO.Path]::Combine($PSScriptRoot, "log", $logfilename)
 }
 
 Function GuiMessageBox
@@ -136,7 +138,7 @@ Function GuiSaveFileName
     param(
         $initialDirectory = "C:\",
         $title = "Save Image File",
-        $filter = "Images (*.img;*.afp;*.usb;*.iso)| *.img;*.afp;*.usb;*.iso"
+        $filter = "Images (*.img;*.afp;*.raw;*.usb;*.iso)| *.img;*.afp;*.raw;*.usb;*.iso"
     )
     $SaveFileDialog = New-Object System.Windows.Forms.SaveFileDialog
     $SaveFileDialog.initialDirectory = $initialDirectory
@@ -146,12 +148,17 @@ Function GuiSaveFileName
     $SaveFileDialog.filename
 }
 
-Function GuiOpenFileName($initialDirectory = "C:\", $title = "Select Image File")
+Function GuiOpenFileName
 {  
+    param(
+        $initialDirectory = "C:\",
+        $title = "Select Image File",
+        $filter = "Images (*.img;*.afp;*.raw;*.usb;*.iso)| *.img;*.afp;*.raw;*.usb;*.iso"
+    )
 
     $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
     $OpenFileDialog.initialDirectory = $initialDirectory
-    $OpenFileDialog.filter = "Images (*.img;*.afp;*.usb;*.iso)| *.img;*.afp;*.usb;*.iso|All files (*.*)| *.*"
+    $OpenFileDialog.filter = $filter + "|All files (*.*)| *.*"
     $OpenFileDialog.Title = $title
     $OpenFileDialog.ShowDialog() | Out-Null
     $OpenFileDialog.filename

+ 32 - 14
WinImager/WinImager.ps1

@@ -1,19 +1,37 @@
+param(
+    $Script = $null
+)
+$env:WINIMAGER = $PSCommandPath
+$env:PSLIB = [System.IO.Path]::Combine($PSScriptRoot, "LibWinImager.psm1")
+Import-Module -Name $env:PSLIB
 
-$libpath = [System.IO.Path]::Combine($MyInvocation.MyCommand.Definition, "..", "LibWinImager.psm1")
-Import-Module -Name $libpath
+if ($Script -eq $null){
 
-BecomeAdmin $MyInvocation
+    $items = Get-ChildItem ([System.IO.Path]::Combine($PSScriptRoot,"bin"))
+    $items
 
-$items = Get-ChildItem ([System.IO.Path]::Combine($MyInvocation.MyCommand.Definition, "..","bin"))
-$items
-""
-$dia = GuiSelectDialog ($items|foreach {$_.Name}) -Title "Select Command" -Label "Select Command"
-$result = $dia.form.ShowDialog()
-$result
+    ""
 
-if ($result -eq "OK"){
-    $it = ($items| where {$_.Name -eq $dia.listbox.selecteditem}).FullName
-    &$it
-} else {
-    "Abort"
+    $dia = GuiSelectDialog ($items|foreach {$_.Name}) -Title "Select Command" -Label "Select Command"
+    $result = $dia.form.ShowDialog()
+    $result
+
+    if ($result -eq "OK"){
+        $Script = ($items| where {$_.Name -eq $dia.listbox.selecteditem}).FullName
+    } else {
+        "Abort"
+        Break
+    }
+
+}
+
+$logpath = LogFilePath ([io.path]::GetFileNameWithoutExtension($Script))
+$logpath
+
+try{
+    &$Script | Tee-Object -FilePath $logpath
+} catch {
+    Write-Error $_.Exception | Tee-Object -FilePath $logpath -Append
+    pause
 }
+

+ 4 - 12
WinImager/bin/DiskToImage.ps1

@@ -1,11 +1,8 @@
 
-$libpath = [System.IO.Path]::Combine($MyInvocation.MyCommand.Definition, "..", "..", "LibWinImager.psm1")
+Import-Module -Name $env:PSLIB
 
-Import-Module -Name $libpath
-
-$logpath = LogFilePath $MyInvocation "ImageToDisk"
-
-BecomeAdmin $MyInvocation
+$PSCommandPath
+BecomeAdmin $PSCommandPath
 
 Function Main()
 {
@@ -50,9 +47,4 @@ Would you like to contine?
     }
 }
 
-try{
-    Main | Tee-Object -FilePath $logpath
-} catch {
-    Write-Error $_.Exception | Tee-Object -FilePath $logpath -Append
-    pause
-}
+Main

+ 2 - 9
WinImager/bin/FixCHS.ps1

@@ -3,9 +3,7 @@ $libpath = [System.IO.Path]::Combine($MyInvocation.MyCommand.Definition, "..", "
 
 Import-Module -Name $libpath
 
-$logpath = LogFilePath $MyInvocation "FixCHS"
-
-BecomeAdmin $MyInvocation
+BecomeAdmin $PSCommandPath
 
 Function Main()
 {
@@ -43,9 +41,4 @@ Would you like to contine?
     }
 }
 
-try{
-    Main | Tee-Object -FilePath $logpath
-} catch {
-    Write-Error $_.Exception | Tee-Object -FilePath $logpath -Append
-}
-pause
+Main

+ 5 - 13
WinImager/bin/ImageToDisk.ps1

@@ -1,11 +1,8 @@
 
-$libpath = [System.IO.Path]::Combine($MyInvocation.MyCommand.Definition, "..", "..", "LibWinImager.psm1")
+Import-Module -Name $env:PSLIB
 
-Import-Module -Name $libpath
-
-$logpath = LogFilePath $MyInvocation "ImageToDisk"
-
-BecomeAdmin $MyInvocation
+$PSCommandPath
+BecomeAdmin $PSCommandPath
 
 Function Main()
 {
@@ -63,15 +60,10 @@ Write image
 to another Drive?
 "@
 		$dialog
-        $continue = AskContinue $dialog
+        $continue = GuiMessageBox $dialog
 
     }
 
 }
 
-try{
-    Main | Tee-Object -FilePath $logpath
-} catch {
-    Write-Error $_.Exception | Tee-Object -FilePath $logpath -Append
-    pause
-}
+Main

+ 7 - 13
WinImager/bin/ImageToVDI.ps1

@@ -1,9 +1,5 @@
 
-$libpath = [System.IO.Path]::Combine($MyInvocation.MyCommand.Definition, "..", "..", "LibWinImager.psm1")
-
-Import-Module -Name $libpath
-
-$logpath = LogFilePath $MyInvocation "ImageToVDI"
+Import-Module -Name $env:PSLIB
 
 Function Main()
 {
@@ -38,16 +34,14 @@ Function Main()
     }
 
     &$VBOXMANAGE convertfromraw $filename $vdiname --format VDI
-    GuiMessageBox "OK" -Buttons "Ok"
-    pause
+
+    if($LASTEXITCODE -ne 0){
+        GuiMessageBox "Success" -Buttons "Ok"
+    } else {
+        GuiMessageBox "Error" -Buttons "Ok"
+    }
     Break
 }
 
 Main
 
-try{
-    Main | Tee-Object -FilePath $logpath
-} catch {
-    Write-Error $_.Exception | Tee-Object -FilePath $logpath -Append
-    pause
-}

+ 3 - 7
WinImager/bin/ResetDisk.ps1

@@ -1,11 +1,7 @@
 
-$libpath = [System.IO.Path]::Combine($MyInvocation.MyCommand.Definition, "..", "..", "LibWinImager.psm1")
+Import-Module -Name $env:PSLIB
 
-Import-Module -Name $libpath
-
-$logpath = LogFilePath $MyInvocation "ResetDisk"
-
-BecomeAdmin $MyInvocation
+BecomeAdmin $PSCommandPath
 
 Function Main()
 {
@@ -31,7 +27,7 @@ to Drive:
 Would you like to contine?
 "@ 
         $dialog
-        $continue = AskContinue $dialog
+        $continue = GuiMessageBox $dialog
 
 
         if ($continue -eq "Yes"){

+ 54 - 0
WinImager/bin/VDIToImage.ps1

@@ -0,0 +1,54 @@
+
+Import-Module -Name $env:PSLIB
+
+Function Main()
+{
+    if ($env:VBOX_INSTALL_PATH -ne $null){
+        $VBOXMANAGE = $env:VBOX_INSTALL_PATH + "VBoxManage.exe"
+    } elseif ($env:VBOX_MSI_INSTALL_PATH -ne $null){
+        $VBOXMANAGE = $env:VBOX_MSI_INSTALL_PATH + "VBoxManage.exe"
+    } else {
+        $dialog = @"
+VirtualBox install Path not found!
+
+Is VirtualBox installed?
+"@ 
+        $dialog
+        GuiMessageBox $dialog -Buttons "Ok"
+        Break
+    }
+    
+    "Select VDI File..."
+
+    $inname = GuiOpenFileName -Filter "Virtualbox Images (*.vdi)| *.vdi"
+
+    if (($inname -eq $null) -or ($inname -eq "")){
+        "No input file selected"
+        Break
+    }
+
+    "Select Image File..."
+    
+    $outname = GuiSaveFileName
+
+    if (($outname -eq $null) -or ($outname -eq "")){
+        "No output file Selected"
+        Break
+    }
+
+    if (Test-Path $outname -PathType Leaf) {
+        "Removing existing file ${outname}"
+        Remove-Item $outname
+    }
+
+    &$VBOXMANAGE clonehd $inname  $outname --format RAW
+
+    if($LASTEXITCODE -ne 0){
+        GuiMessageBox "Success" -Buttons "Ok"
+    } else {
+        GuiMessageBox "Error" -Buttons "Ok"
+    }
+    Break
+}
+
+Main