From 969035c1a216b46f80eee1c0b870c01aca7bb827 Mon Sep 17 00:00:00 2001 From: thejsa Date: Fri, 26 Oct 2018 10:47:57 +0100 Subject: [PATCH 1/4] Add operating system and edition checks to WSL, Docker, Hyper-V, and developer mode enable scripts --- scripts/Docker.ps1 | 17 ++++++++--- scripts/HyperV.ps1 | 16 ++++++++++- scripts/RemoveDefaultApps.ps1 | 9 ++++-- scripts/SystemConfiguration.ps1 | 7 +++-- scripts/WSL.ps1 | 51 +++++++++++++++++---------------- 5 files changed, 64 insertions(+), 36 deletions(-) diff --git a/scripts/Docker.ps1 b/scripts/Docker.ps1 index c2e7bbdd..f516b25e 100644 --- a/scripts/Docker.ps1 +++ b/scripts/Docker.ps1 @@ -1,4 +1,13 @@ -Enable-WindowsOptionalFeature -Online -FeatureName containers -All -RefreshEnv -choco install -y docker-for-windows -choco install -y vscode-docker +if( + # 64-bit edition of Windows + [Environment]::Is64BitOperatingSystem -and + # Editions with Hyper-V + # (See also: https://docs.microsoft.com/en-gb/windows/desktop/api/sysinfoapi/nf-sysinfoapi-getproductinfo) + ((Get-WmiObject Win32_OperatingSystem).OperatingSystemSKU -in 0x06, 0x10, 0x12, 0x50, 0x8, 0xC, 0x79, 0x7A, 0x04, 0x46, 0x48, 0x1B, 0x54, 0x7D, 0x81, 0x7E, 0x82, 0x0A, 0x0E, 0x2A, 0xA1, 0xA2, 0x30, 0x45, 0x31, 0x67, 0x18, 0x4F, 0x07, 0x0D, 0x01, 0x47, 0x1C) -and + # Windows 10 Anniversary Update or later + ((Get-WmiObject Win32_OperatingSystem).BuildNumber -ge 14393)) { + Enable-WindowsOptionalFeature -Online -FeatureName containers -All + RefreshEnv + choco install -y docker-for-windows + choco install -y vscode-docker +} diff --git a/scripts/HyperV.ps1 b/scripts/HyperV.ps1 index 1350cd37..579b45b0 100644 --- a/scripts/HyperV.ps1 +++ b/scripts/HyperV.ps1 @@ -1 +1,15 @@ -choco install -y Microsoft-Hyper-V-All --source="'windowsFeatures'" +# Install Hyper-V +if( + # 64-bit edition of Windows + [Environment]::Is64BitOperatingSystem -and + # Editions with Hyper-V + # (See also: https://docs.microsoft.com/en-gb/windows/desktop/api/sysinfoapi/nf-sysinfoapi-getproductinfo) + ((Get-WmiObject Win32_OperatingSystem).OperatingSystemSKU -in 0x06, 0x10, 0x12, 0x50, 0x8, 0xC, 0x79, 0x7A, 0x04, 0x46, 0x48, 0x1B, 0x54, 0x7D, 0x81, 0x7E, 0x82, 0x0A, 0x0E, 0x2A, 0xA1, 0xA2, 0x30, 0x45, 0x31, 0x67, 0x18, 0x4F, 0x07, 0x0D, 0x01, 0x47, 0x1C) -and + ( + # Server 2008 or later + ([Environment]::OSVersion.Version -ge (New-Object 'Version' 6,0) -and (Get-WmiObject Win32_OperatingSystem).ProductType -gt 1) -or + # Windows 8 or later on client operating systems + ([Environment]::OSVersion.Version -ge (New-Object 'Version' 6,2)) + )) { + choco install -y Microsoft-Hyper-V-All --source="'windowsFeatures'" +} \ No newline at end of file diff --git a/scripts/RemoveDefaultApps.ps1 b/scripts/RemoveDefaultApps.ps1 index c24a0734..35f68ab3 100644 --- a/scripts/RemoveDefaultApps.ps1 +++ b/scripts/RemoveDefaultApps.ps1 @@ -59,6 +59,9 @@ $applicationList = @( "*.AdobePhotoshopExpress" ); -foreach ($app in $applicationList) { - removeApp $app -} \ No newline at end of file +# If on Windows 8 / Server 2012 or later... +if([Environment]::OSVersion.Version -ge (New-Object 'Version' 6,2)) { + foreach ($app in $applicationList) { + removeApp $app + } +} diff --git a/scripts/SystemConfiguration.ps1 b/scripts/SystemConfiguration.ps1 index e70b2dda..cac0412d 100644 --- a/scripts/SystemConfiguration.ps1 +++ b/scripts/SystemConfiguration.ps1 @@ -1,4 +1,5 @@ - - #--- Enable developer mode on the system --- -Set-ItemProperty -Path HKLM:\Software\Microsoft\Windows\CurrentVersion\AppModelUnlock -Name AllowDevelopmentWithoutDevLicense -Value 1 +# (if on Windows 8 / Server 2012 or later) +if([Environment]::OSVersion.Version -ge (New-Object 'Version' 6,2)) { + Set-ItemProperty -Path HKLM:\Software\Microsoft\Windows\CurrentVersion\AppModelUnlock -Name AllowDevelopmentWithoutDevLicense -Value 1 +} \ No newline at end of file diff --git a/scripts/WSL.ps1 b/scripts/WSL.ps1 index fbf87b9b..f5f785a3 100644 --- a/scripts/WSL.ps1 +++ b/scripts/WSL.ps1 @@ -1,30 +1,31 @@ -choco install -y Microsoft-Windows-Subsystem-Linux --source="'windowsfeatures'" +if ((Get-WmiObject Win32_OperatingSystem).BuildNumber -ge 14316) { + choco install -y Microsoft-Windows-Subsystem-Linux --source="'windowsfeatures'" -#--- Ubuntu --- -# TODO: Move this to choco install once --root is included in that package -Invoke-WebRequest -Uri https://aka.ms/wsl-ubuntu-1804 -OutFile ~/Ubuntu.appx -UseBasicParsing -Add-AppxPackage -Path ~/Ubuntu.appx -# run the distro once and have it install locally with root user, unset password + #--- Ubuntu --- + # TODO: Move this to choco install once --root is included in that package + Invoke-WebRequest -Uri https://aka.ms/wsl-ubuntu-1804 -OutFile ~/Ubuntu.appx -UseBasicParsing + Add-AppxPackage -Path ~/Ubuntu.appx + # run the distro once and have it install locally with root user, unset password -RefreshEnv -Ubuntu1804 install --root -Ubuntu1804 run apt update -Ubuntu1804 run apt upgrade -y + RefreshEnv + Ubuntu1804 install --root + Ubuntu1804 run apt update + Ubuntu1804 run apt upgrade -y -<# -NOTE: Other distros can be scripted the same way for example: + <# + NOTE: Other distros can be scripted the same way for example: -#--- SLES --- -# Install SLES Store app -Invoke-WebRequest -Uri https://aka.ms/wsl-sles-12 -OutFile ~/SLES.appx -UseBasicParsing -Add-AppxPackage -Path ~/SLES.appx -# Launch SLES -sles-12.exe - -# --- openSUSE --- -Invoke-WebRequest -Uri https://aka.ms/wsl-opensuse-42 -OutFile ~/openSUSE.appx -UseBasicParsing -Add-AppxPackage -Path ~/openSUSE.appx -# Launch openSUSE -opensuse-42.exe -#> + #--- SLES --- + # Install SLES Store app + Invoke-WebRequest -Uri https://aka.ms/wsl-sles-12 -OutFile ~/SLES.appx -UseBasicParsing + Add-AppxPackage -Path ~/SLES.appx + # Launch SLES + sles-12.exe + # --- openSUSE --- + Invoke-WebRequest -Uri https://aka.ms/wsl-opensuse-42 -OutFile ~/openSUSE.appx -UseBasicParsing + Add-AppxPackage -Path ~/openSUSE.appx + # Launch openSUSE + opensuse-42.exe + #> +} \ No newline at end of file From a5daee4ff17d9380745d0464cf89598835bee2dc Mon Sep 17 00:00:00 2001 From: thejsa Date: Fri, 26 Oct 2018 10:58:18 +0100 Subject: [PATCH 2/4] Check that Windows is a 64-bit edition before installing WSL --- scripts/WSL.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/WSL.ps1 b/scripts/WSL.ps1 index f5f785a3..ec1d2bf2 100644 --- a/scripts/WSL.ps1 +++ b/scripts/WSL.ps1 @@ -1,4 +1,4 @@ -if ((Get-WmiObject Win32_OperatingSystem).BuildNumber -ge 14316) { +if ([Environment]::Is64BitOperatingSystem -and (Get-WmiObject Win32_OperatingSystem).BuildNumber -ge 14316) { choco install -y Microsoft-Windows-Subsystem-Linux --source="'windowsfeatures'" #--- Ubuntu --- From 8ca3b61ec81842c80c55b445f5c3f9df2b3a1477 Mon Sep 17 00:00:00 2001 From: thejsa Date: Fri, 26 Oct 2018 11:05:45 +0100 Subject: [PATCH 3/4] Add check to ensure if operating system is server then WSL is only installed if build >= 12699 --- scripts/WSL.ps1 | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/scripts/WSL.ps1 b/scripts/WSL.ps1 index ec1d2bf2..9b1f73de 100644 --- a/scripts/WSL.ps1 +++ b/scripts/WSL.ps1 @@ -1,4 +1,12 @@ -if ([Environment]::Is64BitOperatingSystem -and (Get-WmiObject Win32_OperatingSystem).BuildNumber -ge 14316) { +if ( + [Environment]::Is64BitOperatingSystem -and ( + ( + ( + (Get-WmiObject Win32_OperatingSystem).ProductType -lt 2) -and (Get-WmiObject Win32_OperatingSystem).BuildNumber -ge 14316 + ) -or + (Get-WmiObject Win32_OperatingSystem).BuildNumber -ge 16299 + ) + ) { choco install -y Microsoft-Windows-Subsystem-Linux --source="'windowsfeatures'" #--- Ubuntu --- From f3bad0f455af77a8698199734d0e8d2f146c2fc6 Mon Sep 17 00:00:00 2001 From: thejsa Date: Fri, 16 Nov 2018 22:29:13 +0000 Subject: [PATCH 4/4] Split compat checks into cleaner helper functions --- dev_app.ps1 | 2 + dev_app_desktop_.NET.ps1 | 2 + dev_app_desktop_cplusplus.ps1 | 2 + dev_ml_windows.ps1 | 2 + dev_ml_wsl.ps1 | 2 + dev_web.ps1 | 2 + dev_web_nodejs.ps1 | 2 + devops_azure.ps1 | 2 + scripts/CompatibilityChecks.ps1 | 78 +++++++++++++++++++++++++++++++++ scripts/Docker.ps1 | 6 +-- scripts/HyperV.ps1 | 13 +----- scripts/WSL.ps1 | 4 +- 12 files changed, 98 insertions(+), 19 deletions(-) create mode 100644 scripts/CompatibilityChecks.ps1 diff --git a/dev_app.ps1 b/dev_app.ps1 index 3a5e7c22..aeb0c7e9 100644 --- a/dev_app.ps1 +++ b/dev_app.ps1 @@ -21,6 +21,8 @@ function executeScript { iex ((new-object net.webclient).DownloadString("$helperUri/$script")) } +executeScript "CompatibilityChecks.ps1"; + #--- Setting up Windows --- executeScript "SystemConfiguration.ps1"; executeScript "FileExplorerSettings.ps1"; diff --git a/dev_app_desktop_.NET.ps1 b/dev_app_desktop_.NET.ps1 index cb8c4e60..7e11829c 100644 --- a/dev_app_desktop_.NET.ps1 +++ b/dev_app_desktop_.NET.ps1 @@ -21,6 +21,8 @@ function executeScript { iex ((new-object net.webclient).DownloadString("$helperUri/$script")) } +executeScript "CompatibilityChecks.ps1"; + #--- Setting up Windows --- executeScript "SystemConfiguration.ps1"; executeScript "FileExplorerSettings.ps1"; diff --git a/dev_app_desktop_cplusplus.ps1 b/dev_app_desktop_cplusplus.ps1 index e0669d4c..d173b1e8 100644 --- a/dev_app_desktop_cplusplus.ps1 +++ b/dev_app_desktop_cplusplus.ps1 @@ -21,6 +21,8 @@ function executeScript { iex ((new-object net.webclient).DownloadString("$helperUri/$script")) } +executeScript "CompatibilityChecks.ps1"; + #--- Setting up Windows --- executeScript "SystemConfiguration.ps1"; executeScript "FileExplorerSettings.ps1"; diff --git a/dev_ml_windows.ps1 b/dev_ml_windows.ps1 index 32997d5d..a36cbd66 100644 --- a/dev_ml_windows.ps1 +++ b/dev_ml_windows.ps1 @@ -21,6 +21,8 @@ function executeScript { iex ((new-object net.webclient).DownloadString("$helperUri/$script")) } +executeScript "CompatibilityChecks.ps1"; + #--- Setting up Windows --- executeScript "SystemConfiguration.ps1"; executeScript "FileExplorerSettings.ps1"; diff --git a/dev_ml_wsl.ps1 b/dev_ml_wsl.ps1 index b744dd3d..1676bdfb 100644 --- a/dev_ml_wsl.ps1 +++ b/dev_ml_wsl.ps1 @@ -21,6 +21,8 @@ function executeScript { iex ((new-object net.webclient).DownloadString("$helperUri/$script")) } +executeScript "CompatibilityChecks.ps1"; + #--- Setting up Windows --- executeScript "SystemConfiguration.ps1"; executeScript "FileExplorerSettings.ps1"; diff --git a/dev_web.ps1 b/dev_web.ps1 index 2d001c5a..d9a82b10 100644 --- a/dev_web.ps1 +++ b/dev_web.ps1 @@ -21,6 +21,8 @@ function executeScript { iex ((new-object net.webclient).DownloadString("$helperUri/$script")) } +executeScript "CompatibilityChecks.ps1"; + #--- Setting up Windows --- executeScript "FileExplorerSettings.ps1"; executeScript "SystemConfiguration.ps1"; diff --git a/dev_web_nodejs.ps1 b/dev_web_nodejs.ps1 index 55ba0be2..b336eb01 100644 --- a/dev_web_nodejs.ps1 +++ b/dev_web_nodejs.ps1 @@ -21,6 +21,8 @@ function executeScript { iex ((new-object net.webclient).DownloadString("$helperUri/$script")) } +executeScript "CompatibilityChecks.ps1"; + #--- Setting up Windows --- executeScript "SystemConfiguration.ps1"; executeScript "FileExplorerSettings.ps1"; diff --git a/devops_azure.ps1 b/devops_azure.ps1 index 2ea6071c..c8649581 100644 --- a/devops_azure.ps1 +++ b/devops_azure.ps1 @@ -22,6 +22,8 @@ function executeScript { iex ((new-object net.webclient).DownloadString("$helperUri/$script")) } +executeScript "CompatibilityChecks.ps1"; + #--- Setting up Windows --- executeScript "FileExplorerSettings.ps1"; executeScript "SystemConfiguration.ps1"; diff --git a/scripts/CompatibilityChecks.ps1 b/scripts/CompatibilityChecks.ps1 new file mode 100644 index 00000000..639f4f36 --- /dev/null +++ b/scripts/CompatibilityChecks.ps1 @@ -0,0 +1,78 @@ +# Helper functions for compatibility checks + +# Returns $true if running a 64-bit OS. +# TODO: checks for non-x86-based architectures, eg. ARM64 +function Is-64Bit +{ + return [Environment]::Is64BitOperatingSystem +} + +# Returns $true if running on a server edition of Windows. +function Is-Server +{ + return ((Get-WmiObject Win32_OperatingSystem).ProductType -gt 1) +} + +# Returns $true if system has hardware virtualization enabled. +function Is-Virtualization-Capable +{ + return (Get-WmiObject Win32_Processor).VirtualizationFirmwareEnabled +} + +# Returns $true if system should be capable of Hyper-V virtualization. +function Is-HyperV-Capable +{ + # Ensure we're running on a 64-bit operating system + if(-Not (Is-64Bit)) { + return $false + } + + # Ensure hardware virtualization is available and enabled + if(-Not (Is-Virtualization-Capable)) { + return $false + } + + # Ensure product SKU supports Hyper-V + # Source for SKU IDs: https://docs.microsoft.com/en-gb/windows/desktop/api/sysinfoapi/nf-sysinfoapi-getproductinfo + if((Get-WmiObject Win32_OperatingSystem).OperatingSystemSKU -NotIn 0x06, 0x10, 0x12, 0x50, 0x8, 0xC, 0x79, 0x7A, 0x04, 0x46, 0x48, 0x1B, 0x54, 0x7D, 0x81, 0x7E, 0x82, 0x0A, 0x0E, 0x2A, 0xA1, 0xA2, 0x30, 0x45, 0x31, 0x67, 0x18, 0x4F, 0x07, 0x0D, 0x01, 0x47, 0x1C) + { + return $false + } + + $BuildVersion = [System.Environment]::OSVersion.Version + + # Ensure that we're not running on XP + if($BuildVersion.Major -lt '6') + { + return $false + } + + # Windows Server 2008/R2 and variants + if($BuildVersion.Major -eq '6' -and $BuildVersion.Minor -lt '2') + { + # Ensure we aren't running on a client edition + if(-Not (Is-Server)) { + return $false + } + + return $true + } + + # Windows 8/8.1 and Windows Server 2012/R2 + if($BuildVersion.Major -ge '6' -and $BuildVersion.Minor -ge '2') + { + # Client Hyper-V requires SLAT + if(-Not (Is-Server)) { + return (Get-WmiObject Win32_Processor).SecondLevelAddressTranslationExtensions + } + + return $true + } + + # Windows 10 / Windows Server 2016 and later + if($BuildVersion.Major -ge '10') + { + # Ensure SLAT is supported + return (Get-WmiObject Win32_Processor).SecondLevelAddressTranslationExtensions + } +} \ No newline at end of file diff --git a/scripts/Docker.ps1 b/scripts/Docker.ps1 index f516b25e..e97b067d 100644 --- a/scripts/Docker.ps1 +++ b/scripts/Docker.ps1 @@ -1,9 +1,5 @@ if( - # 64-bit edition of Windows - [Environment]::Is64BitOperatingSystem -and - # Editions with Hyper-V - # (See also: https://docs.microsoft.com/en-gb/windows/desktop/api/sysinfoapi/nf-sysinfoapi-getproductinfo) - ((Get-WmiObject Win32_OperatingSystem).OperatingSystemSKU -in 0x06, 0x10, 0x12, 0x50, 0x8, 0xC, 0x79, 0x7A, 0x04, 0x46, 0x48, 0x1B, 0x54, 0x7D, 0x81, 0x7E, 0x82, 0x0A, 0x0E, 0x2A, 0xA1, 0xA2, 0x30, 0x45, 0x31, 0x67, 0x18, 0x4F, 0x07, 0x0D, 0x01, 0x47, 0x1C) -and + (Is-HyperV-Capable) -and # Windows 10 Anniversary Update or later ((Get-WmiObject Win32_OperatingSystem).BuildNumber -ge 14393)) { Enable-WindowsOptionalFeature -Online -FeatureName containers -All diff --git a/scripts/HyperV.ps1 b/scripts/HyperV.ps1 index 579b45b0..98634716 100644 --- a/scripts/HyperV.ps1 +++ b/scripts/HyperV.ps1 @@ -1,15 +1,4 @@ # Install Hyper-V -if( - # 64-bit edition of Windows - [Environment]::Is64BitOperatingSystem -and - # Editions with Hyper-V - # (See also: https://docs.microsoft.com/en-gb/windows/desktop/api/sysinfoapi/nf-sysinfoapi-getproductinfo) - ((Get-WmiObject Win32_OperatingSystem).OperatingSystemSKU -in 0x06, 0x10, 0x12, 0x50, 0x8, 0xC, 0x79, 0x7A, 0x04, 0x46, 0x48, 0x1B, 0x54, 0x7D, 0x81, 0x7E, 0x82, 0x0A, 0x0E, 0x2A, 0xA1, 0xA2, 0x30, 0x45, 0x31, 0x67, 0x18, 0x4F, 0x07, 0x0D, 0x01, 0x47, 0x1C) -and - ( - # Server 2008 or later - ([Environment]::OSVersion.Version -ge (New-Object 'Version' 6,0) -and (Get-WmiObject Win32_OperatingSystem).ProductType -gt 1) -or - # Windows 8 or later on client operating systems - ([Environment]::OSVersion.Version -ge (New-Object 'Version' 6,2)) - )) { +if(Is-HyperV-Capable) { choco install -y Microsoft-Hyper-V-All --source="'windowsFeatures'" } \ No newline at end of file diff --git a/scripts/WSL.ps1 b/scripts/WSL.ps1 index 9b1f73de..0af3503a 100644 --- a/scripts/WSL.ps1 +++ b/scripts/WSL.ps1 @@ -1,8 +1,8 @@ if ( - [Environment]::Is64BitOperatingSystem -and ( + (Is-64Bit) -and ( ( ( - (Get-WmiObject Win32_OperatingSystem).ProductType -lt 2) -and (Get-WmiObject Win32_OperatingSystem).BuildNumber -ge 14316 + (-Not (Is-Server)) -and (Get-WmiObject Win32_OperatingSystem).BuildNumber -ge 14316 ) -or (Get-WmiObject Win32_OperatingSystem).BuildNumber -ge 16299 )