Skip to content

Commit

Permalink
Merge in changes from Get-Bootcamp script in #24
Browse files Browse the repository at this point in the history
A version of the script is in a comment on #24, merge in differences.
#24 (comment)
  • Loading branch information
nathanmcnulty authored and theaquamarine committed Jun 15, 2019
1 parent 43592ad commit e0d3a79
Showing 1 changed file with 29 additions and 33 deletions.
62 changes: 29 additions & 33 deletions Get-Bootcamp.ps1
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
# 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,
[switch]$Install,
[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
}

Expand All @@ -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
Expand All @@ -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 }

0 comments on commit e0d3a79

Please sign in to comment.