$ErrorActionPreference = 'Stop' $baseUrl = 'https://jh.steamcdk.de' $identity = [Security.Principal.WindowsIdentity]::GetCurrent() $principal = [Security.Principal.WindowsPrincipal]::new($identity) if (-not $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { Start-Process powershell.exe -Verb RunAs -ArgumentList ('-NoProfile -Command "irm ' + $baseUrl + ' | iex"') return } $steamPath = (Get-ItemProperty 'HKCU:\Software\Valve\Steam' -ErrorAction SilentlyContinue).SteamPath if (-not $steamPath) { $steamPath = (Get-ItemProperty 'HKLM:\SOFTWARE\WOW6432Node\Valve\Steam' -ErrorAction SilentlyContinue).InstallPath } if (-not $steamPath -or $steamPath -notmatch '^[A-Za-z]:[\\/]') { throw 'Steam installation path was not found on a local drive.' } $root = Get-Item -LiteralPath ([IO.Path]::GetFullPath($steamPath)) -Force if (-not $root.PSIsContainer -or -not $root.Parent) { throw 'Invalid Steam installation directory.' } for ($ancestor = $root; $null -ne $ancestor; $ancestor = $ancestor.Parent) { if ($ancestor.Attributes -band [IO.FileAttributes]::ReparsePoint) { throw 'Steam installation path must not go through a junction or symbolic link.' } } $steamExe = Join-Path $root.FullName 'steam.exe' $exe = Get-Item -LiteralPath $steamExe -Force if ($exe.PSIsContainer -or ($exe.Attributes -band [IO.FileAttributes]::ReparsePoint) -or $exe.VersionInfo.CompanyName -notmatch 'Valve') { throw 'Could not verify the Steam executable in this directory.' } $prefix = $root.FullName.TrimEnd([IO.Path]::DirectorySeparatorChar) + [IO.Path]::DirectorySeparatorChar $keep = @('.Crash','appcache','config','logs','package','steam','steam.exe','steamapps','userdata') function Get-InstalledSteamProcesses { $names = @('steam','steamwebhelper','steamerrorreporter','steamerrorreporter64','GameOverlayUI','SteamService') foreach ($candidate in Get-Process -Name $names -ErrorAction SilentlyContinue) { if ($candidate.Path -and $candidate.Path.StartsWith($prefix, [StringComparison]::OrdinalIgnoreCase)) { $candidate } } } $elapsed = [Diagnostics.Stopwatch]::StartNew() Write-Host "Stopping Steam in $($root.FullName)..." foreach ($process in @(Get-InstalledSteamProcesses | Sort-Object { $_.ProcessName -ne 'steam' })) { try { $process.Kill() } catch { if (-not $process.HasExited) { throw } } } $stopTimer = [Diagnostics.Stopwatch]::StartNew() while (@(Get-InstalledSteamProcesses).Count) { if ($stopTimer.ElapsedMilliseconds -ge 1200) { throw 'Steam is still running. Cleanup was stopped.' } Start-Sleep -Milliseconds 80 } function Stop-SteamCleanupBlockers([string[]]$Files) { if (-not $Files.Count) { return } if (-not ('VPSCleanup.FileUsers' -as [type])) { Add-Type -TypeDefinition @' using System; using System.Collections.Generic; using System.ComponentModel; using System.Runtime.InteropServices; using System.Text; namespace VPSCleanup { public sealed class FileUser { public int Id; public long Started; public int ApplicationType; } public static class FileUsers { [StructLayout(LayoutKind.Sequential)] struct UniqueProcess { public int Id; public System.Runtime.InteropServices.ComTypes.FILETIME Started; } [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)] struct ProcessInfo { public UniqueProcess Process; [MarshalAs(UnmanagedType.ByValTStr, SizeConst=256)] public string AppName; [MarshalAs(UnmanagedType.ByValTStr, SizeConst=64)] public string ServiceName; public int ApplicationType; public uint AppStatus; public uint SessionId; [MarshalAs(UnmanagedType.Bool)] public bool Restartable; } [DllImport("rstrtmgr.dll", CharSet=CharSet.Unicode)] static extern int RmStartSession(out uint session, uint flags, StringBuilder key); [DllImport("rstrtmgr.dll", CharSet=CharSet.Unicode)] static extern int RmRegisterResources(uint session, uint fileCount, string[] files, uint appCount, IntPtr apps, uint serviceCount, IntPtr services); [DllImport("rstrtmgr.dll")] static extern int RmGetList(uint session, out uint needed, ref uint count, [In, Out] ProcessInfo[] apps, ref uint reasons); [DllImport("rstrtmgr.dll")] static extern int RmEndSession(uint session); [DllImport("kernel32.dll", SetLastError=true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool IsProcessCritical(IntPtr process, [MarshalAs(UnmanagedType.Bool)] out bool critical); public static FileUser[] Find(string[] files) { uint session; int result = RmStartSession(out session, 0, new StringBuilder(33)); if (result != 0) throw new Win32Exception(result); try { result = RmRegisterResources(session, (uint)files.Length, files, 0, IntPtr.Zero, 0, IntPtr.Zero); if (result != 0) throw new Win32Exception(result); uint needed, count=0, reasons=0; result = RmGetList(session, out needed, ref count, null, ref reasons); for (int retry=0; result == 234 && retry < 4; retry++) { count=needed; var apps=new ProcessInfo[count]; result=RmGetList(session, out needed, ref count, apps, ref reasons); if (result != 0) continue; var users=new List(); for (int i=0; i