install-qemu-img.ps1


<#PSScriptInfo
.VERSION 1.1
.GUID f938f650-4b25-4c77-9a27-9509888ceb1c
.AUTHOR Karsten.Bott@labbuildr.com
.COMPANYNAME
.COPYRIGHT
.TAGS
.LICENSEURI
.PROJECTURI
.ICONURI
.EXTERNALMODULEDEPENDENCIES
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES
#>


<#
.DESCRIPTION
 Download and Install qemu-img utilities
#>
 
Param(
)
if (!$Downloadlocation)
    {
    $Downloadlocation = $env:TEMP
    }
$myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)

$adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator
    
# Check to see if we are currently running "as Administrator"
if ($OldShell.IsPresent -or !$myWindowsPrincipal.IsInRole($adminRole))
    {
    # $arguments = "-Downloadlocation $Downloadlocation"
    $newProcess = New-object System.Diagnostics.ProcessStartInfo "PowerShell"
    $newProcess.Arguments = "-noexit $PSScriptRoot/$($myinvocation.MyCommand)" 
    Write-Host $newProcess.Arguments
    $newProcess.Verb = "runas"
    [System.Diagnostics.Process]::Start($newProcess) 
    exit
    }       
$Cloudbase_URL = "https://cloudbase.it"    
$QEMU_Base_URL ="$Cloudbase_URL/qemu-img-windows"
Write-Host "[==>]Analyzing $QEMU_Base_URL" -NoNewline
$Req = Invoke-WebRequest -UseBasicParsing -Uri $QEMU_Base_URL
$DownloadLink =  ($req.Links | where outerhtml -match "qemu-img-win-x64").href
$file = split-path -Leaf $DownloadLink
$folder = "qemu"
$Requestlink = "$Cloudbase_URL$DownloadLink"
Write-Host -ForegroundColor Green "[done]"
$Outfile = Join-Path $Downloadlocation $file
Write-Host "[==>]Downloading $file" -NoNewline
Invoke-WebRequest  $Requestlink -OutFile $Outfile
Unblock-File $Outfile
Write-Host -ForegroundColor Green "[done]"
Expand-Archive $Outfile -DestinationPath "$env:ProgramFiles\$Folder" -Force -Confirm:$false
$env:Path += ";$env:ProgramFiles\$folder"
$oldPath = (Get-ItemProperty -Path "Registry::HKEY_CURRENT_USER\Environment" -Name PATH).Path
if (!($oldPath -match $folder))
    {

        Write-Host "[==>]Adding $folder path to Environment" -NoNewline
        [Environment]::SetEnvironmentVariable( "Path", $env:Path, [System.EnvironmentVariableTarget]::Machine )
        Write-Host -ForegroundColor Green "[done]"
    }
else {
    Write-Host "Path to $folder already in Environment"
}