Skip to content

Commit b205c44

Browse files
committed
refac: reworked fixStoreIssues.cmd
1 parent 3da4a04 commit b205c44

File tree

1 file changed

+99
-76
lines changed

1 file changed

+99
-76
lines changed
Lines changed: 99 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,26 @@
11
<# : batch section
22
@echo off & setlocal enabledelayedexpansion
33

4-
:: Parse /silent argument
5-
set "silent=0"
6-
for %%A in (%*) do (
7-
if /i "%%~A"=="/silent" set "silent=1"
8-
)
9-
10-
:: Check if running as TrustedInstaller (re-entry point)
11-
whoami /user | find /i "S-1-5-18" > nul 2>&1 && (
12-
goto :main
13-
)
4+
:: Check if running as TrustedInstaller
5+
whoami /user | find /i "S-1-5-18" > nul 2>&1 && goto :main
146

15-
:: Check if Gaming Services is installed
167
sc query GamingServices >nul 2>&1
178
if %errorlevel% equ 0 (
18-
echo === Checking Gaming Services ===
19-
echo Gaming Services is installed, removing...
20-
winget uninstall 9MWPM2CQNLHN --silent
21-
echo Gaming Services removed.
9+
echo === Checking Gaming Services ===
10+
echo Gaming Services is installed, removing...
11+
winget uninstall 9MWPM2CQNLHN --silent
12+
echo Gaming Services removed.
2213
)
2314

24-
set "silentArg="
25-
if "!silent!"=="1" set "silentArg=/silent"
26-
call %SYSTEMROOT%\AtlasModules\Scripts\RunAsTI.cmd "%~f0" !silentArg!
27-
15+
call %SYSTEMROOT%\AtlasModules\Scripts\RunAsTI.cmd "%~f0" %*
2816
exit /b
2917

3018
:main
31-
if "!silent!"=="1" (
32-
powershell -NoProfile -ExecutionPolicy Bypass -Command "$env:SILENT='1'; iex((gc '%~f0' -Raw))"
33-
) else (
34-
start /wait "Fix Store Issues" powershell -NoProfile -ExecutionPolicy Bypass -Command "iex((gc '%~f0' -Raw))"
35-
)
19+
start /wait "Fix Store Issues" powershell -NoProfile -ExecutionPolicy Bypass -Command "iex((gc '%~f0' -Raw))"
3620
exit /b
3721
#>
3822

3923
# ======================== PowerShell ========================
40-
# ======================== Force GamingServicesNet ========================
41-
# Attempt to forcibly remove GamingServicesNet service
42-
$gamingService = 'GamingServicesNet'
43-
$svcRegPath = "HKLM\\SYSTEM\\CurrentControlSet\\Services\\$gamingService"
44-
Write-Host "`n=== Attempting to forcibly remove GamingServicesNet ===" -ForegroundColor Magenta
45-
try {
46-
# Stop the service if running
47-
$svc = Get-Service -Name $gamingService -ErrorAction SilentlyContinue
48-
if ($svc -and $svc.Status -ne 'Stopped') {
49-
Stop-Service -Name $gamingService -Force -ErrorAction SilentlyContinue
50-
Write-Host " Stopped GamingServicesNet." -ForegroundColor Green
51-
}
52-
# Remove service using sc.exe
53-
$null = & cmd.exe /c "sc.exe delete $gamingService"
54-
Write-Host " Issued sc delete command for GamingServicesNet." -ForegroundColor Green
55-
# Remove registry key
56-
$null = & cmd.exe /c "reg.exe delete \"$svcRegPath\" /f"
57-
Write-Host " Deleted registry key for GamingServicesNet." -ForegroundColor Green
58-
} catch {
59-
Write-Host " WARNING: Could not forcibly remove GamingServicesNet: $($_.Exception.Message)" -ForegroundColor Yellow
60-
}
61-
62-
6324
Add-Type -MemberDefinition @"
6425
[DllImport("kernel32.dll", SetLastError = true)]
6526
public static extern IntPtr GetStdHandle(int nStdHandle);
@@ -75,10 +36,14 @@ $mode = 0
7536
$mode = $mode -band (-bnot 0x0040) # Remove ENABLE_QUICK_EDIT_MODE
7637
[void][Win32.Console]::SetConsoleMode($handle, $mode)
7738

39+
$Desktop = (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders' -Name 'Common Desktop').'Common Desktop'
40+
$LogFile = "$Desktop\fixStoreIssues.txt"
41+
Start-Transcript -Path $LogFile -Force | Out-Null
42+
7843
$Silent = $env:SILENT -eq '1'
7944

8045
$TargetFile = 'C:\ProgramData\Microsoft\Windows\AppRepository\StateRepository-Deployment.srd'
81-
$RootServices = @('GamingServicesNet', 'ClipSVC','AppXSvc', 'StateRepository')
46+
$RootServices = @('ClipSVC', 'AppXSvc', 'StateRepository')
8247

8348
function Get-AllDependents {
8449
param([string]$ServiceName)
@@ -93,7 +58,7 @@ function Get-AllDependents {
9358
while ($queue.Count -gt 0) {
9459
$current = $queue.Dequeue()
9560
$deps = Get-Service -Name $current -ErrorAction SilentlyContinue |
96-
Select-Object -ExpandProperty DependentServices
61+
Select-Object -ExpandProperty DependentServices
9762

9863
foreach ($dep in $deps) {
9964
if ($result.Add($dep.Name)) {
@@ -121,9 +86,6 @@ foreach ($root in $RootServices) {
12186
[void]$allServiceNames.Add($dep)
12287
}
12388
}
124-
# Remove GamingServicesNet and GamingServices from the list if forcibly handled
125-
$allServiceNames.Remove('GamingServicesNet') | Out-Null
126-
$allServiceNames.Remove('GamingServices') | Out-Null
12789

12890

12991
$originalStartTypes = @{}
@@ -141,15 +103,25 @@ $hasFailed = $false
141103
try {
142104

143105
# ========================================================
144-
# 2. Disable every service via Set-Service
106+
# 2. Disable every service via sc.exe
145107
# ========================================================
146108
Write-Host "`n=== Disabling all services ===" -ForegroundColor Cyan
147109

148110
foreach ($svc in $allServiceNames) {
149-
Set-Service -Name $svc -StartupType Disabled -ErrorAction Stop
111+
sc.exe config $svc start= disabled | Out-Null
112+
if ($LASTEXITCODE -eq 5) {
113+
# Access denied - save original SD, grant access, retry, then restore SD
114+
$sdOutput = (sc.exe sdshow $svc | Where-Object { $_ -match '^D:' }) -join ''
115+
$defaultSD = 'D:(A;;CCLCSWRPWPDTLOCRRC;;;SY)(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;BA)(A;;CCLCSWLOCRRC;;;IU)(A;;CCLCSWLOCRRC;;;SU)'
116+
sc.exe sdset $svc $defaultSD | Out-Null
117+
sc.exe config $svc start= $scValue | Out-Null
118+
if ($sdOutput) {
119+
sc.exe sdset $svc $sdOutput | Out-Null
120+
}
121+
}
122+
if ($LASTEXITCODE -ne 0) { throw "Failed to disable $svc" }
150123
Write-Host " Disabled: $svc"
151124
}
152-
153125
# ========================================================
154126
# 3. Stop every service — dependents first, then roots.
155127
# ========================================================
@@ -163,18 +135,18 @@ try {
163135
$s.Refresh()
164136
if ($s.Status -eq 'Stopped') { continue }
165137
Write-Host " Sending stop to dependent: $name ..."
166-
try { Stop-Service $s } catch {
138+
try { sc.exe stop $name | Out-Null } catch {
167139
Write-Host " Warning: $($_.Exception.InnerException.Message)" -ForegroundColor Yellow
168140
}
169141
}
170-
142+
171143
foreach ($name in $RootServices) {
172144
$s = Get-Service -Name $name -ErrorAction SilentlyContinue
173145
if ($null -eq $s) { continue }
174146
$s.Refresh()
175147
if ($s.Status -eq 'Stopped') { continue }
176148
Write-Host " Sending stop to root: $name ..."
177-
try { Stop-Service $s } catch {
149+
try { sc.exe stop $name | Out-Null } catch {
178150
Write-Host " Warning: $($_.Exception.InnerException.Message)" -ForegroundColor Yellow
179151
}
180152
}
@@ -188,14 +160,23 @@ try {
188160
if ($null -eq $s) { continue }
189161
$s.Refresh()
190162
if ($s.Status -eq 'Stopped') { continue }
191-
Write-Warning "$name is still $($s.Status) - waiting up to 30s ..."
192-
try {
193-
$s.WaitForStatus('Stopped', [TimeSpan]::FromSeconds(30))
194-
} catch {
195-
$s.Refresh()
196-
if ($s.Status -ne 'Stopped') {
197-
$stubborn += $name
198-
Write-Host " FAILED: $name did not stop ($($s.Status))" -ForegroundColor Red
163+
while ($s.Status -ne 'Stopped') {
164+
Write-Warning "$name is still $($s.Status) - waiting up to 10s ..."
165+
try {
166+
$s.WaitForStatus('Stopped', [TimeSpan]::FromSeconds(10))
167+
}
168+
catch {
169+
$s.Refresh()
170+
if ($s.Status -ne 'Stopped') {
171+
Write-Host " $name still running, attempting to stop again..." -ForegroundColor Yellow
172+
try { sc.exe stop $name | Out-Null } catch {}
173+
Start-Sleep -Seconds 5
174+
$s.Refresh()
175+
if ($s.Status -ne 'Stopped') {
176+
$stubborn += $name
177+
Write-Host " FAILED: $name did not stop ($($s.Status))" -ForegroundColor Red
178+
}
179+
}
199180
}
200181
}
201182
}
@@ -212,27 +193,53 @@ try {
212193
if (Test-Path -LiteralPath $TargetFile) {
213194
Remove-Item -LiteralPath $TargetFile -Force -ErrorAction Stop
214195
Write-Host " Deleted: $TargetFile" -ForegroundColor Green
215-
} else {
196+
}
197+
else {
216198
Write-Host " File not found (already absent): $TargetFile" -ForegroundColor Yellow
217199
}
218200

219-
} catch {
201+
}
202+
catch {
220203
$hasFailed = $true
221204
Write-Host "`n!!! ERROR: $($_.Exception.Message)" -ForegroundColor Red
222-
} finally {
205+
}
206+
finally {
223207

224208
# ========================================================
225209
# 5. ALWAYS restore original start types via Set-Service
226210
# ========================================================
227211
Write-Host "`n=== Restoring original start types ===" -ForegroundColor Cyan
228212

213+
$startTypeMap = @{
214+
'Automatic' = 'auto'
215+
'Manual' = 'demand'
216+
'Disabled' = 'disabled'
217+
'Boot' = 'boot'
218+
'System' = 'system'
219+
}
220+
229221
foreach ($svc in $allServiceNames) {
230222
$origValue = $originalStartTypes[$svc]
231-
try {
232-
Set-Service -Name $svc -StartupType $origValue -ErrorAction Stop
223+
$scValue = $startTypeMap[$origValue.ToString()]
224+
if (-not $scValue) {
225+
Write-Host " FAILED to restore ${svc}: unknown start type '$origValue'" -ForegroundColor Red
226+
continue
227+
}
228+
sc.exe config $svc start= $scValue | Out-Null
229+
if ($LASTEXITCODE -eq 5) {
230+
# Access denied - save original SD, grant access, retry, then restore SD
231+
$sdOutput = (sc.exe sdshow $svc | Where-Object { $_ -match '^D:' }) -join ''
232+
$defaultSD = 'D:(A;;CCLCSWRPWPDTLOCRRC;;;SY)(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;BA)(A;;CCLCSWLOCRRC;;;IU)(A;;CCLCSWLOCRRC;;;SU)'
233+
sc.exe sdset $svc $defaultSD | Out-Null
234+
sc.exe config $svc start= $scValue | Out-Null
235+
if ($sdOutput) {
236+
sc.exe sdset $svc $sdOutput | Out-Null
237+
}
238+
}
239+
if ($LASTEXITCODE -ne 0) {
240+
Write-Host " FAILED to restore ${svc} (sc.exe exit code $LASTEXITCODE)" -ForegroundColor Red
241+
} else {
233242
Write-Host " Restored: $svc -> StartType = $origValue"
234-
} catch {
235-
Write-Host " FAILED to restore ${svc}: $($_.Exception.Message)" -ForegroundColor Red
236243
}
237244
}
238245

@@ -243,18 +250,34 @@ try {
243250
try {
244251
Start-Service -Name 'StateRepository' -ErrorAction Stop
245252
Write-Host " StateRepository is running." -ForegroundColor Green
246-
} catch {
253+
}
254+
catch {
247255
Write-Host " FAILED to start StateRepository: $($_.Exception.Message)" -ForegroundColor Red
248256
}
249257
}
250258

251259
if ($hasFailed) {
252260
Write-Host "`n=== Finished WITH ERRORS. Review output above. ===" -ForegroundColor Red
253-
} else {
261+
}
262+
else {
263+
# ========================================================
264+
# 7. Run wsreset.exe -i to reinstall Store
265+
# ========================================================
266+
Write-Host "`n=== Running wsreset.exe -i (this may take a moment) ===" -ForegroundColor Cyan
267+
$wsresetProcess = Start-Process -FilePath 'wsreset.exe' -ArgumentList '-i' -PassThru -Wait
268+
if ($wsresetProcess.ExitCode -ne 0) {
269+
Write-Host " wsreset.exe -i exited with code $($wsresetProcess.ExitCode)" -ForegroundColor Yellow
270+
} else {
271+
Write-Host " wsreset.exe -i completed successfully." -ForegroundColor Green
272+
}
273+
254274
Write-Host "`n=== Done. ===" -ForegroundColor Green
255275
}
256276

277+
Stop-Transcript | Out-Null
278+
Write-Host " Log saved to: $LogFile"
279+
257280
if (-not $Silent) {
258281
Write-Host ""
259282
pause
260-
}
283+
}

0 commit comments

Comments
 (0)