Skip to content

Commit

Permalink
Support disabling per-user services in Windows #16
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
undergroundwires committed Sep 20, 2021
1 parent c8cb7a5 commit 4b23907
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 11 deletions.
6 changes: 5 additions & 1 deletion src/application/Parser/Script/Syntax/BatchFileSyntax.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { ILanguageSyntax } from '@/domain/ScriptCode';


const BatchFileCommonCodeParts = [ '(', ')', 'else' ];
const PowerShellCommonCodeParts = [ '{', '}' ];

export class BatchFileSyntax implements ILanguageSyntax {
public readonly commentDelimiters = [ 'REM', '::' ];
public readonly commonCodeParts = [ '(', ')', 'else' ];
public readonly commonCodeParts = [ ...BatchFileCommonCodeParts, ...PowerShellCommonCodeParts ];
}
83 changes: 73 additions & 10 deletions src/application/collections/windows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2825,25 +2825,40 @@ actions:
-
name: User Data Storage (UnistoreSvc) Service
recommend: strict
code: sc stop "UnistoreSvc" & sc config "UnistoreSvc" start=disabled
revertCode: sc config "UnistoreSvc" start=demand
call:
function: DisablePerUserService
parameters:
serviceName: UnistoreSvc
defaultStartUpMode: 3 # 0: Boot | 1: System | 2: Automatic | 3: Manual | 4: Disabled
-
name: Sync Host (OneSyncSvc) Service Service
recommend: strict
code: sc stop "OneSyncSvc" & sc config "OneSyncSvc" start=disabled
revertCode: sc config "OneSyncSvc" start=auto & sc start "OneSyncSvc"
call:
function: DisablePerUserService
parameters:
serviceName: OneSyncSvc
defaultStartUpMode: 2 # 0: Boot | 1: System | 2: Automatic | 3: Manual | 4: Disabled
-
name: Contact data indexing
code: sc stop "PimIndexMaintenanceSvc" & sc config "PimIndexMaintenanceSvc" start=disabled
revertCode: sc config "PimIndexMaintenanceSvc" start=demand
call:
function: DisablePerUserService
parameters:
serviceName: PimIndexMaintenanceSvc
defaultStartUpMode: 3 # 0: Boot | 1: System | 2: Automatic | 3: Manual | 4: Disabled
-
name: App user data access
code: sc stop "UserDataSvc" & sc config "UserDataSvc" start=disabled
revertCode: sc config "UserDataSvc" start=demand
call:
function: DisablePerUserService
parameters:
serviceName: UserDataSvc
defaultStartUpMode: 3 # 0: Boot | 1: System | 2: Automatic | 3: Manual | 4: Disabled
-
name: Text messaging
code: sc stop "MessagingService" & sc config "MessagingService" start=disabled
revertCode: sc config "MessagingService" start=demand
call:
function: DisablePerUserService
parameters:
serviceName: MessagingService
defaultStartUpMode: 3 # 0: Boot | 1: System | 2: Automatic | 3: Manual | 4: Disabled
-
name: Windows Push Notification Service
recommend: standard
Expand Down Expand Up @@ -4542,3 +4557,51 @@ functions:
{{ with $revertCode }}
PowerShell -ExecutionPolicy Unrestricted -Command "{{ . | inlinePowerShell | escapeDoubleQuotes }}"
{{ 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"
}
}

0 comments on commit 4b23907

Please sign in to comment.