Skip to content

Commit

Permalink
support Powershell 2
Browse files Browse the repository at this point in the history
  • Loading branch information
ave9858 committed Feb 2, 2023
1 parent 5615def commit 2b3c500
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions Set-WindowsCbsEdition.ps1
Expand Up @@ -36,7 +36,6 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
Sets the script to stage the current edition instead of removing it.
#>

#Requires -RunAsAdministrator

param (
[Parameter()]
Expand All @@ -49,6 +48,11 @@ param (
[Switch]$StageCurrent
)

if (-Not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-Error "Administrator rights are required to change the edition!"
return
}

function Get-AssemblyIdentity {
param (
[String]$PackageName
Expand Down Expand Up @@ -195,7 +199,7 @@ https://github.com/Gamers-Against-Weed/Set-WindowsCbsEdition
$removalCandidates = @();
$installCandidates = @{};

$packages = Get-ChildItem -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\Packages' | select Name | where name -Match '^.*\\Microsoft-Windows-.*Edition~'
$packages = Get-ChildItem -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\Packages' | select Name | where { $_.name -match '^.*\\Microsoft-Windows-.*Edition~' }
foreach($package in $packages) {
$state = (Get-ItemProperty -Path "Registry::$($package.Name)").CurrentState
$packageName = ($package.Name -split '\\')[-1]
Expand All @@ -206,12 +210,12 @@ foreach($package in $packages) {
$installCandidates[$packageEdition] = @()
}

if($false -eq ($packageName -in $installCandidates[$packageEdition])) {
if($false -eq ($installCandidates[$packageEdition] -contains $packageName)) {
$installCandidates[$packageEdition] = $installCandidates[$packageEdition] + @($packageName)
}
}

if((($state -eq 0x50) -or ($state -eq 0x70)) -and ($false -eq ($packageName -in $removalCandidates))) {
if((($state -eq 0x50) -or ($state -eq 0x70)) -and ($false -eq ($removalCandidates -contains $packageName))) {
$removalCandidates = $removalCandidates + @($packageName)
}
}
Expand All @@ -221,7 +225,7 @@ if($getTargetsParam) {
Exit
}

if($false -eq ($SetEdition -in $installCandidates.Keys)) {
if($false -eq ($installCandidates.Keys -contains $SetEdition)) {
Write-Error "The system cannot be upgraded to `"$SetEdition`""
Exit 1
}
Expand Down

0 comments on commit 2b3c500

Please sign in to comment.