Support disabling per-user services in Windows #16

Some services in Windows have random characters appended to them. This
commit fixes the scripts that has been trying to disable them but
failing in newer Windows versions where they become per-user.
This commit is contained in:
undergroundwires
2021-09-20 23:05:15 +01:00
parent c8cb7a5c28
commit 4b2390736a
2 changed files with 78 additions and 11 deletions

View File

@@ -1,6 +1,10 @@
import { ILanguageSyntax } from '@/domain/ScriptCode'; import { ILanguageSyntax } from '@/domain/ScriptCode';
const BatchFileCommonCodeParts = [ '(', ')', 'else' ];
const PowerShellCommonCodeParts = [ '{', '}' ];
export class BatchFileSyntax implements ILanguageSyntax { export class BatchFileSyntax implements ILanguageSyntax {
public readonly commentDelimiters = [ 'REM', '::' ]; public readonly commentDelimiters = [ 'REM', '::' ];
public readonly commonCodeParts = [ '(', ')', 'else' ]; public readonly commonCodeParts = [ ...BatchFileCommonCodeParts, ...PowerShellCommonCodeParts ];
} }

View File

@@ -2825,25 +2825,40 @@ actions:
- -
name: User Data Storage (UnistoreSvc) Service name: User Data Storage (UnistoreSvc) Service
recommend: strict recommend: strict
code: sc stop "UnistoreSvc" & sc config "UnistoreSvc" start=disabled call:
revertCode: sc config "UnistoreSvc" start=demand function: DisablePerUserService
parameters:
serviceName: UnistoreSvc
defaultStartUpMode: 3 # 0: Boot | 1: System | 2: Automatic | 3: Manual | 4: Disabled
- -
name: Sync Host (OneSyncSvc) Service Service name: Sync Host (OneSyncSvc) Service Service
recommend: strict recommend: strict
code: sc stop "OneSyncSvc" & sc config "OneSyncSvc" start=disabled call:
revertCode: sc config "OneSyncSvc" start=auto & sc start "OneSyncSvc" function: DisablePerUserService
parameters:
serviceName: OneSyncSvc
defaultStartUpMode: 2 # 0: Boot | 1: System | 2: Automatic | 3: Manual | 4: Disabled
- -
name: Contact data indexing name: Contact data indexing
code: sc stop "PimIndexMaintenanceSvc" & sc config "PimIndexMaintenanceSvc" start=disabled call:
revertCode: sc config "PimIndexMaintenanceSvc" start=demand function: DisablePerUserService
parameters:
serviceName: PimIndexMaintenanceSvc
defaultStartUpMode: 3 # 0: Boot | 1: System | 2: Automatic | 3: Manual | 4: Disabled
- -
name: App user data access name: App user data access
code: sc stop "UserDataSvc" & sc config "UserDataSvc" start=disabled call:
revertCode: sc config "UserDataSvc" start=demand function: DisablePerUserService
parameters:
serviceName: UserDataSvc
defaultStartUpMode: 3 # 0: Boot | 1: System | 2: Automatic | 3: Manual | 4: Disabled
- -
name: Text messaging name: Text messaging
code: sc stop "MessagingService" & sc config "MessagingService" start=disabled call:
revertCode: sc config "MessagingService" start=demand function: DisablePerUserService
parameters:
serviceName: MessagingService
defaultStartUpMode: 3 # 0: Boot | 1: System | 2: Automatic | 3: Manual | 4: Disabled
- -
name: Windows Push Notification Service name: Windows Push Notification Service
recommend: standard recommend: standard
@@ -4542,3 +4557,51 @@ functions:
{{ with $revertCode }} {{ with $revertCode }}
PowerShell -ExecutionPolicy Unrestricted -Command "{{ . | inlinePowerShell | escapeDoubleQuotes }}" PowerShell -ExecutionPolicy Unrestricted -Command "{{ . | inlinePowerShell | escapeDoubleQuotes }}"
{{ end }} {{ end }}
-
name: DisablePerUserService # https://docs.microsoft.com/en-us/windows/application-management/per-user-services-in-windows
parameters:
- name: serviceName
- name: defaultStartUpMode
call:
function: RunPowerShell
parameters:
code: |-
$serviceQueries = @('{{ $serviceName }}', '{{ $serviceName }}_*')
foreach ($serviceQuery in $serviceQueries) {
$service = Get-Service -Name $serviceQuery -ErrorAction Ignore
if(!$service) {
Write-Host "Service `"$serviceQuery`" is not found, no action is needed"
continue
}
$name = $service.Name
Stop-Service $name -ErrorAction SilentlyContinue
if($?) {
Write-Host "Stopped `"$name`""
} else {
Write-Warning "Could not stop `"$name`""
}
$regKey = "HKLM:\SYSTEM\CurrentControlSet\Services\$name"
if(Test-Path $regKey) {
Set-ItemProperty $regKey -Name Start -Value 4 -Force
Write-Host "Disabled `"$name`""
} else {
Write-Host "Service is not registered at Windows startup, no action is needed."
}
}
revertCode: |-
$serviceQueries = @('{{ $serviceName }}', '{{ $serviceName }}_*')
foreach ($serviceQuery in $serviceQueries) {
$service = Get-Service -Name $serviceQuery -ErrorAction SilentlyContinue
if(!$service) {
Write-Warning "Service `"$serviceQuery`" not found"
continue
}
$name = $service.Name
$regKey = "HKLM:\SYSTEM\CurrentControlSet\Services\$name"
if(Test-Path $regKey) {
Set-ItemProperty $regKey -Name Start -Value 0 -Force
Write-Host "Enabled `"$name`", may require restarting your computer."
} else {
Write-Error "Registry key at `"$regKey`" does not exist"
}
}