From e0d3a79951057a424f99a7369c91722f8754206a Mon Sep 17 00:00:00 2001 From: nathanmcnulty Date: Sat, 15 Jun 2019 14:21:24 +0100 Subject: [PATCH] Merge in changes from Get-Bootcamp script in #24 A version of the script is in a comment on #24, merge in differences. https://github.com/timsutton/brigadier/issues/24#issuecomment-295011284 --- Get-Bootcamp.ps1 | 62 ++++++++++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 33 deletions(-) diff --git a/Get-Bootcamp.ps1 b/Get-Bootcamp.ps1 index 899c970..b39da22 100644 --- a/Get-Bootcamp.ps1 +++ b/Get-Bootcamp.ps1 @@ -1,6 +1,3 @@ -# Rewrite from Python to PowerShell - Requires PowerShell v3 or greater -# 16/4/7 - Removd DMG2IMG components, removed plist as we can take arguments instead, preparing for ability to pass ProductID as an array -# To do: Add logging, include fallback in case BITS is not installed: https://blog.jourdant.me/3-ways-to-download-files-with-powershell/ [CmdletBinding()] Param( [string]$Model = (Get-WmiObject -Class Win32_ComputerSystem).Model, @@ -8,19 +5,21 @@ Param( [string]$OutputDir = "$env:TEMP", [switch]$KeepFiles, [array]$ProductId, - [string]$Mst, [string]$SUCATALOG_URL = 'http://swscan.apple.com/content/catalogs/others/index-10.11-10.10-10.9-mountainlion-lion-snowleopard-leopard.merged-1.sucatalog', - [string]$SEVENZIP_URL = 'http://7-zip.org/a/7z1514-x64.msi' + [string]$SEVENZIP_URL = 'http://www.7-zip.org/a/7z1604-x64.msi' ) +# Disable Invoke-WebRequest progress bar to speed up download due to bug +$ProgressPreference = "SilentlyContinue" + # Create Output Directory if it does not exist if (!(Test-Path $OutputDir)) { New-Item -Path $OutputDir -ItemType Directory -Force } -# Check if 7zip 15.14 is installed. If not, download and install it. +# Check if at least 7zip 15.14 is installed. If not, download and install it. $7z = "$env:ProgramFiles\7-Zip\7z.exe" -if (Test-Path $7z) { $7zInstalled = $true; [decimal]$7zVersion = (Get-ItemProperty $7z).VersionInfo.FileVersion } -if ($7zVersion -lt 15.14) { - Start-BitsTransfer -Source $SEVENZIP_URL -Destination "$OutputDir\$($SEVENZIP_URL.Split('/')[-1])" -ErrorAction Stop +if (Test-Path $7z) { $7zInstalled = $true } +if ([decimal](Get-ItemProperty $7z).VersionInfo.FileVersion -lt 15.14) { + Invoke-WebRequest -Uri $SEVENZIP_URL -OutFile "$OutputDir\$($SEVENZIP_URL.Split('/')[-1])" -ErrorAction Stop Start-Process -FilePath $env:SystemRoot\System32\msiexec.exe -ArgumentList "/i $OutputDir\$($SEVENZIP_URL.Split('/')[-1]) /qb- /norestart" -Wait -Verbose } @@ -36,7 +35,7 @@ $sucatalog.plist.dict.dict.dict | Where-Object { $_.String -match "Bootcamp" } | } } if ($bootcamplist.Length -gt 1) { - Write-Output "Found more than 1 supported Bootcamp ESD. Selecting newest based on posted date" + Write-warning "Found more than 1 supported Bootcamp ESD. Selecting newest based on posted date which may not always be correct" $bootcamplist | ForEach-Object { if ($_.date -gt $latestdate) { $latestdate = $_.date @@ -50,37 +49,34 @@ Start-BitsTransfer -Source $download -Destination "$OutputDir\BootCampESD.pkg" - if (Test-Path -Path "$OutputDir\BootCampESD.pkg") { # Extract the bootcamp installer Invoke-Command -ScriptBlock { - cmd /c $7z -o"$OutputDir" -y e "$OutputDir\BootCampESD.pkg" - cmd /c $7z -o"$OutputDir" -y e "$OutputDir\Payload~" + & $7z -o"$OutputDir" -y e "$OutputDir\BootCampESD.pkg" + & $7z -o"$OutputDir" -y e "$OutputDir\Payload~" # If just downloading, put the extracted installers on the desktop - if ($Install) { cmd /c $7z -o"$OutputDir" -y x "$OutputDir\WindowsSupport.dmg" } else { if ($OutputDir -eq "$env:TEMP") { cmd /c $7z -o"$env:USERPROFILE\Desktop\$version" -y x "$OutputDir\WindowsSupport.dmg" }} + if ($Install) { + & $7z -o"$OutputDir" -y x "$OutputDir\WindowsSupport.dmg" + } + else { + if ($OutputDir -eq "$env:TEMP") { & $7z -o"$env:USERPROFILE\Desktop\$version" -y x "$OutputDir\WindowsSupport.dmg" } else { & $7z -o"$OutputDir" -y x "$OutputDir\WindowsSupport.dmg" } + } } -} else { Write-Output "BootCampESD.pkg could not be found"; exit } + } +else { Write-Warning "BootCampESD.pkg could not be found"; exit } # Uninstall 7zip if we installed it if ($7zInstalled -ne $true) { Start-Process -FilePath $env:SystemRoot\System32\msiexec.exe -ArgumentList "/x $OutputDir\$($SEVENZIP_URL.Split('/')[-1]) /qb- /norestart" -Wait } -# Testing for iMac14,1 issue with Realtek Audio driver hanging the installation -#"Bootcamp","Drivers" | ForEach-Object { if (Test-Path -Path "$OutputDir\$_") { (Get-ChildItem -Path "$OutputDir\$_" -Recurse -Include RealtekSetup.exe -ErrorAction SilentlyContinue).FullName }} | Move-Item -Destination $OutputDir - -# Find Bootcamp.msi and install matching based on OS architecture -[array]$BootCampMSI = "Bootcamp","Drivers" | ForEach-Object { if (Test-Path -Path "$OutputDir\$_") { (Get-ChildItem -Path "$OutputDir\$_" -Recurse -Include BootCamp*.msi).FullName }} -if ($BootCampMSI.Length -gt 1) { - # Check OS architecture and install correct version - if ((Get-WmiObject -Class Win32_OperatingSystem).OSArchitecture -eq "64-bit") { - $BootCampMSI = $BootCampMSI | Where-Object { $_ -match "64" } - } else { - $BootCampMSI = $BootCampMSI | Where-Object { $_ -notmatch "64" } - } -} - # Install Bootcamp and use MST if specified (I uploaded one that I had to use to fix the latest ESD on an iMac14,1) if ($Install) { - if ($mst -ne "") { - Copy-Item -Path $Mst -Destination $($BootCampMSI.TrimEnd("\BootCamp.msi")) - Start-Process -FilePath $env:SystemRoot\System32\msiexec.exe -ArgumentList "/i $BootCampMSI TRANSFORMS=$($Mst.Split('\')[-1]) /qb- /norestart /l*v $env:SystemDrive\BootcampInstall.log" -Verbose -Wait - } else { Start-Process -FilePath $env:SystemRoot\System32\msiexec.exe -ArgumentList "/i $BootCampMSI /qb- /norestart /l*v $env:SystemDrive\BootcampInstall.log" -Verbose -Wait } -} else { exit } + # Install Bootcamp + $scaction = New-ScheduledTaskAction -Execute "msiexec.exe" -Argument "/i $OutputDir\Bootcamp\Drivers\Apple\BootCamp.msi /qn /norestart" + $sctrigger = New-ScheduledTaskTrigger -At ((Get-Date).AddSeconds(15)) -Once + $scprincipal = New-ScheduledTaskPrincipal "SYSTEM" -RunLevel Highest + $scsettings = New-ScheduledTaskSettingsSet + $sctask = New-ScheduledTask -Action $scaction -Principal $scprincipal -Trigger $sctrigger -Settings $scsettings + Register-ScheduledTask "Install Bootcamp" -InputObject $sctask -User "SYSTEM" + do { Write-Output "Sleeping 20 seconds"; Start-Sleep -Seconds 20 } while (Get-Process -Name "msiexec" -ErrorAction SilentlyContinue) +} +else { exit } # Clean up if ($KeepFiles) { exit } else { Remove-Item -Path "$OutputDir\*" -Recurse -Force -ErrorAction SilentlyContinue } \ No newline at end of file