diff --git a/scripts/vm-guest-tools.ps1 b/scripts/vm-guest-tools.ps1 new file mode 100644 index 00000000..474ed1d8 --- /dev/null +++ b/scripts/vm-guest-tools.ps1 @@ -0,0 +1,79 @@ +if (!( Test-Path "C:\Windows\Temp\7z1900-x64.msi")) { + [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; (New-Object System.Net.WebClient).DownloadFile('https://www.7-zip.org/a/7z1900-x64.msi', 'C:\Windows\Temp\7z1900-x64.msi') + } + if (!(Test-Path "C:\Windows\Temp\7z1900-x64.msi")) { + Start-Sleep 5; [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; (New-Object System.Net.WebClient).DownloadFile('https://www.7-zip.org/a/7z1900-x64.msi', 'C:\Windows\Temp\7z1900-x64.msi') + } + cmd /c msiexec /qb /i C:\Windows\Temp\7z1900-x64.msi + + if ("$env:PACKER_BUILDER_TYPE" -eq "vmware-iso") { + + Write-Host "Using VMware" + if (Test-Path "C:\Users\vagrant\windows.iso") { + Move-Item -force C:\Users\vagrant\windows.iso C:\Windows\Temp + } + + if (!(Test-Path "C:\Windows\Temp\windows.iso")) { + Try { + # Disabling the progress bar speeds up IWR https://github.com/PowerShell/PowerShell/issues/2138 + $ProgressPreference = 'SilentlyContinue' + $pageContentLinks = (Invoke-WebRequest('https://softwareupdate.vmware.com/cds/vmw-desktop/ws') -UseBasicParsing).Links | where-object {$_.href -Match "[0-9]"} | Select-Object href | % { $_.href.Trim('/') } + $versionObject = $pageContentLinks | %{ new-object System.Version ($_) } | sort-object -Descending | select-object -First 1 -Property:Major,Minor,Build + $newestVersion = $versionObject.Major.ToString()+"."+$versionObject.Minor.ToString()+"."+$versionObject.Build.ToString() | out-string + $newestVersion = $newestVersion.TrimEnd("`r?`n") + + $nextURISubdirectoryObject = (Invoke-WebRequest("https://softwareupdate.vmware.com/cds/vmw-desktop/ws/$newestVersion/") -UseBasicParsing).Links | where-object {$_.href -Match "[0-9]"} | Select-Object href | where-object {$_.href -Match "[0-9]"} + $nextUriSubdirectory = $nextURISubdirectoryObject.href | Out-String + $nextUriSubdirectory = $nextUriSubdirectory.TrimEnd("`r?`n") + $newestVMwareToolsURL = "https://softwareupdate.vmware.com/cds/vmw-desktop/ws/$newestVersion/$nextURISubdirectory/windows/packages/tools-windows.tar" + Write-Host "The latest version of VMware tools has been determined to be downloadable from $newestVMwareToolsURL" + [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; (New-Object System.Net.WebClient).DownloadFile("$newestVMwareToolsURL", 'C:\Windows\Temp\vmware-tools.tar') + } Catch { + Write-Host "Unable to determine the latest version of VMware tools. Falling back to hardcoded URL." + (New-Object System.Net.WebClient).DownloadFile('https://softwareupdate.vmware.com/cds/vmw-desktop/ws/15.5.5/16285975/windows/packages/tools-windows.tar', 'C:\Windows\Temp\vmware-tools.tar') + } + cmd /c "C:\PROGRA~1\7-Zip\7z.exe" x C:\Windows\Temp\vmware-tools.tar -oC:\Windows\Temp + Move-Item c:\windows\temp\VMware-tools-windows-*.iso c:\windows\temp\windows.iso + Try { Remove-Item "C:\Program Files (x86)\VMWare" -Recurse -Force -ErrorAction Stop } Catch { Write-Host "Directory didn't exist to be removed." } + } + + cmd /c "C:\PROGRA~1\7-Zip\7z.exe" x "C:\Windows\Temp\windows.iso" -oC:\Windows\Temp\VMWare + cmd /c C:\Windows\Temp\VMWare\setup.exe /S /v"/qn REBOOT=R\" + + Remove-Item -Force "C:\Windows\Temp\vmware-tools.tar" + Remove-Item -Force "C:\Windows\Temp\windows.iso" + Remove-Item -Force -Recurse "C:\Windows\Temp\VMware" + } + + if ("$env:PACKER_BUILDER_TYPE" -eq "virtualbox-iso") { + Write-Host "Using Virtualbox" + if (Test-Path "C:\Users\vagrant\VBoxGuestAdditions.iso") { + Move-Item -Force C:\Users\vagrant\VBoxGuestAdditions.iso C:\Windows\Temp + } + + if (!(Test-Path "C:\Windows\Temp\VBoxGuestAdditions.iso")) { + Try { + $pageContentLinks = (Invoke-WebRequest('https://download.virtualbox.org/virtualbox') -UseBasicParsing).Links | where-object {$_.href -Match "[0-9]"} | Select-Object href | where-object {$_.href -NotMatch "BETA"} | where-object {$_.href -NotMatch "RC"} | where-object {$_.href -Match "[0-9]\.[0-9]"} | % { $_.href.Trim('/') } + $versionObject = $pageContentLinks | %{ new-object System.Version ($_) } | sort-object -Descending | select-object -First 1 -Property:Major,Minor,Build + $newestVersion = $versionObject.Major.ToString()+"."+$versionObject.Minor.ToString()+"."+$versionObject.Build.ToString() | out-string + $newestVersion = $newestVersion.TrimEnd("`r?`n") + + $nextURISubdirectoryObject = (Invoke-WebRequest("https://download.virtualbox.org/virtualbox/$newestVersion/") -UseBasicParsing).Links | Select-Object href | where-object {$_.href -Match "GuestAdditions"} + $nextUriSubdirectory = $nextURISubdirectoryObject.href | Out-String + $nextUriSubdirectory = $nextUriSubdirectory.TrimEnd("`r?`n") + $newestVboxToolsURL = "https://download.virtualbox.org/virtualbox/$newestVersion/$nextUriSubdirectory" + Write-Host "The latest version of VirtualBox tools has been determined to be downloadable from $newestVboxToolsURL" + [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; (New-Object System.Net.WebClient).DownloadFile("$newestVboxToolsURL", 'C:\Windows\Temp\VBoxGuestAdditions.iso') + } Catch { + Write-Host "Unable to determine the latest version of VBox tools. Falling back to hardcoded URL." + [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; (New-Object System.Net.WebClient).DownloadFile('https://download.virtualbox.org/virtualbox/6.1.8/VBoxGuestAdditions_6.1.8.iso', 'C:\Windows\Temp\VBoxGuestAdditions.iso') + } + } + + cmd /c ""C:\PROGRA~1\7-Zip\7z.exe" x C:\Windows\Temp\VBoxGuestAdditions.iso -oC:\Windows\Temp\virtualbox" + Get-ChildItem "C:\Windows\Temp\virtualbox\cert\" -Filter vbox*.cer | Foreach-Object { C:\Windows\Temp\virtualbox\cert\VBoxCertUtil add-trusted-publisher $_.FullName --root $_.FullName } + cmd /c C:\Windows\Temp\virtualbox\VBoxWindowsAdditions.exe /S + cmd /c rd /S /Q "C:\Windows\Temp\virtualbox" + } + + cmd /c msiexec /qb /x C:\Windows\Temp\7z1900-x64.msi \ No newline at end of file diff --git a/windows_10.json b/windows_10.json index c51f6410..8c847719 100644 --- a/windows_10.json +++ b/windows_10.json @@ -195,13 +195,13 @@ "execute_command": "{{ .Vars }} cmd /c \"{{ .Path }}\"", "remote_path": "/tmp/script.bat", "scripts": [ - "./scripts/vm-guest-tools.bat", "./scripts/enable-rdp.bat" ], "type": "windows-shell" }, { "scripts": [ + "./scripts/vm-guest-tools.ps1", "./scripts/debloat-windows.ps1" ], "type": "powershell" diff --git a/windows_2019.json b/windows_2019.json index 2243d675..8ea341f9 100644 --- a/windows_2019.json +++ b/windows_2019.json @@ -146,13 +146,13 @@ { "execute_command": "{{ .Vars }} cmd /c \"{{ .Path }}\"", "scripts": [ - "./scripts/vm-guest-tools.bat", "./scripts/enable-rdp.bat" ], "type": "windows-shell" }, { "scripts": [ + "./scripts/vm-guest-tools.ps1", "./scripts/debloat-windows.ps1" ], "type": "powershell" diff --git a/windows_2019_core.json b/windows_2019_core.json index 668972df..82e1074e 100644 --- a/windows_2019_core.json +++ b/windows_2019_core.json @@ -97,13 +97,13 @@ "provisioners": [ { "scripts": [ - "./scripts/vm-guest-tools.bat", "./scripts/enable-rdp.bat" ], "type": "windows-shell" }, { "scripts": [ + "./scripts/vm-guest-tools.ps1", "./scripts/debloat-windows.ps1" ], "type": "powershell" diff --git a/windows_2019_core_ami.json b/windows_2019_core_ami.json index 2a5578f8..c320e1df 100644 --- a/windows_2019_core_ami.json +++ b/windows_2019_core_ami.json @@ -54,13 +54,13 @@ "provisioners": [ { "scripts": [ - "./scripts/vm-guest-tools.bat", "./scripts/enable-rdp.bat" ], "type": "windows-shell" }, { "scripts": [ + "./scripts/vm-guest-tools.ps1", "./scripts/debloat-windows.ps1" ], "type": "powershell" diff --git a/windows_2019_docker.json b/windows_2019_docker.json index 8e391c28..b72c90ba 100644 --- a/windows_2019_docker.json +++ b/windows_2019_docker.json @@ -185,13 +185,13 @@ { "execute_command": "{{ .Vars }} cmd /c \"{{ .Path }}\"", "scripts": [ - "./scripts/vm-guest-tools.bat", "./scripts/enable-rdp.bat" ], "type": "windows-shell" }, { "scripts": [ + "./scripts/vm-guest-tools.ps1", "./scripts/debloat-windows.ps1", "./scripts/docker/set-winrm-automatic.ps1" ], diff --git a/windows_2019_hyperv.json b/windows_2019_hyperv.json index 1250fdc7..e10f9537 100644 --- a/windows_2019_hyperv.json +++ b/windows_2019_hyperv.json @@ -107,13 +107,13 @@ { "execute_command": "{{ .Vars }} cmd /c \"{{ .Path }}\"", "scripts": [ - "./scripts/vm-guest-tools.bat", "./scripts/enable-rdp.bat" ], "type": "windows-shell" }, { "scripts": [ + "./scripts/vm-guest-tools.ps1", "./scripts/debloat-windows.ps1" ], "type": "powershell" diff --git a/windows_server_1809.json b/windows_server_1809.json index f0ce936b..e682a45a 100644 --- a/windows_server_1809.json +++ b/windows_server_1809.json @@ -104,13 +104,13 @@ { "execute_command": "{{ .Vars }} cmd /c \"{{ .Path }}\"", "scripts": [ - "./scripts/vm-guest-tools.bat", "./scripts/enable-rdp.bat" ], "type": "windows-shell" }, { "scripts": [ + "./scripts/vm-guest-tools.ps1", "./scripts/debloat-windows.ps1" ], "type": "powershell" diff --git a/windows_server_1809_docker.json b/windows_server_1809_docker.json index 75da1309..5794927d 100644 --- a/windows_server_1809_docker.json +++ b/windows_server_1809_docker.json @@ -108,13 +108,13 @@ { "execute_command": "{{ .Vars }} cmd /c \"{{ .Path }}\"", "scripts": [ - "./scripts/vm-guest-tools.bat", "./scripts/enable-rdp.bat" ], "type": "windows-shell" }, { "scripts": [ + "./scripts/vm-guest-tools.ps1", "./scripts/docker/2016/install-containers-feature.ps1", "./scripts/docker/set-winrm-delayed-automatic.ps1" ], diff --git a/windows_server_1903.json b/windows_server_1903.json index a00dd010..bdc9af95 100644 --- a/windows_server_1903.json +++ b/windows_server_1903.json @@ -104,13 +104,13 @@ { "execute_command": "{{ .Vars }} cmd /c \"{{ .Path }}\"", "scripts": [ - "./scripts/vm-guest-tools.bat", "./scripts/enable-rdp.bat" ], "type": "windows-shell" }, { "scripts": [ + "./scripts/vm-guest-tools.ps1", "./scripts/debloat-windows.ps1" ], "type": "powershell" diff --git a/windows_server_1903_docker.json b/windows_server_1903_docker.json index 660f0191..bab468c8 100644 --- a/windows_server_1903_docker.json +++ b/windows_server_1903_docker.json @@ -108,13 +108,13 @@ { "execute_command": "{{ .Vars }} cmd /c \"{{ .Path }}\"", "scripts": [ - "./scripts/vm-guest-tools.bat", "./scripts/enable-rdp.bat" ], "type": "windows-shell" }, { "scripts": [ + "./scripts/vm-guest-tools.ps1", "./scripts/docker/2016/install-containers-feature.ps1", "./scripts/docker/set-winrm-delayed-automatic.ps1" ], diff --git a/windows_server_1909.json b/windows_server_1909.json index fb970d1a..9796d7cd 100644 --- a/windows_server_1909.json +++ b/windows_server_1909.json @@ -104,13 +104,13 @@ { "execute_command": "{{ .Vars }} cmd /c \"{{ .Path }}\"", "scripts": [ - "./scripts/vm-guest-tools.bat", "./scripts/enable-rdp.bat" ], "type": "windows-shell" }, { "scripts": [ + "./scripts/vm-guest-tools.ps1", "./scripts/debloat-windows.ps1" ], "type": "powershell" diff --git a/windows_server_1909_docker.json b/windows_server_1909_docker.json index b8701837..b20229b8 100644 --- a/windows_server_1909_docker.json +++ b/windows_server_1909_docker.json @@ -108,13 +108,13 @@ { "execute_command": "{{ .Vars }} cmd /c \"{{ .Path }}\"", "scripts": [ - "./scripts/vm-guest-tools.bat", "./scripts/enable-rdp.bat" ], "type": "windows-shell" }, { "scripts": [ + "./scripts/vm-guest-tools.ps1", "./scripts/docker/2016/install-containers-feature.ps1", "./scripts/docker/set-winrm-delayed-automatic.ps1" ], diff --git a/windows_server_2004.json b/windows_server_2004.json index 4f08f6e2..e841d3e6 100644 --- a/windows_server_2004.json +++ b/windows_server_2004.json @@ -104,13 +104,13 @@ { "execute_command": "{{ .Vars }} cmd /c \"{{ .Path }}\"", "scripts": [ - "./scripts/vm-guest-tools.bat", "./scripts/enable-rdp.bat" ], "type": "windows-shell" }, { "scripts": [ + "./scripts/vm-guest-tools.ps1", "./scripts/debloat-windows.ps1" ], "type": "powershell" diff --git a/windows_server_insider.json b/windows_server_insider.json index 8c1ff37d..1871e3a4 100644 --- a/windows_server_insider.json +++ b/windows_server_insider.json @@ -101,11 +101,16 @@ { "execute_command": "{{ .Vars }} cmd /c \"{{ .Path }}\"", "scripts": [ - "./scripts/vm-guest-tools.bat", "./scripts/enable-rdp.bat" ], "type": "windows-shell" }, + { + "scripts": [ + "./scripts/vm-guest-tools.ps1" + ], + "type": "powershell" + }, { "restart_timeout": "{{user `restart_timeout`}}", "type": "windows-restart" diff --git a/windows_server_insider_docker.json b/windows_server_insider_docker.json index f6890e01..5a60d4bc 100644 --- a/windows_server_insider_docker.json +++ b/windows_server_insider_docker.json @@ -101,13 +101,13 @@ "provisioners": [ { "scripts": [ - "./scripts/vm-guest-tools.bat", "./scripts/enable-rdp.bat" ], "type": "windows-shell" }, { "scripts": [ + "./scripts/vm-guest-tools.ps1", "./scripts/docker/2016/install-containers-feature.ps1", "./scripts/docker/set-winrm-delayed-automatic.ps1" ],