Skip to content

Commit 2b3c500

Browse files
committed
support Powershell 2
1 parent 5615def commit 2b3c500

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

Set-WindowsCbsEdition.ps1

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
3636
Sets the script to stage the current edition instead of removing it.
3737
#>
3838

39-
#Requires -RunAsAdministrator
4039

4140
param (
4241
[Parameter()]
@@ -49,6 +48,11 @@ param (
4948
[Switch]$StageCurrent
5049
)
5150

51+
if (-Not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
52+
Write-Error "Administrator rights are required to change the edition!"
53+
return
54+
}
55+
5256
function Get-AssemblyIdentity {
5357
param (
5458
[String]$PackageName
@@ -195,7 +199,7 @@ https://github.com/Gamers-Against-Weed/Set-WindowsCbsEdition
195199
$removalCandidates = @();
196200
$installCandidates = @{};
197201

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

209-
if($false -eq ($packageName -in $installCandidates[$packageEdition])) {
213+
if($false -eq ($installCandidates[$packageEdition] -contains $packageName)) {
210214
$installCandidates[$packageEdition] = $installCandidates[$packageEdition] + @($packageName)
211215
}
212216
}
213217

214-
if((($state -eq 0x50) -or ($state -eq 0x70)) -and ($false -eq ($packageName -in $removalCandidates))) {
218+
if((($state -eq 0x50) -or ($state -eq 0x70)) -and ($false -eq ($removalCandidates -contains $packageName))) {
215219
$removalCandidates = $removalCandidates + @($packageName)
216220
}
217221
}
@@ -221,7 +225,7 @@ if($getTargetsParam) {
221225
Exit
222226
}
223227

224-
if($false -eq ($SetEdition -in $installCandidates.Keys)) {
228+
if($false -eq ($installCandidates.Keys -contains $SetEdition)) {
225229
Write-Error "The system cannot be upgraded to `"$SetEdition`""
226230
Exit 1
227231
}

0 commit comments

Comments
 (0)