Last active
November 26, 2020 05:56
-
-
Save mmillar-bolis/6886ef3409b7136fea81636eb17207c4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Microsoft Windows Powershell Script | |
# | |
# Name: Disable Update Service | |
# Version: 0.2.2.0 | |
# Date: 2020-11-25 | |
# Author: MMillar | |
# https://github.com/mmillar-bolis | |
# http://www.bolis.com | |
# | |
# License: | |
# The MIT License | |
# Copyright (c) 2020, The Bolis Group | |
# | |
# Description: | |
# Prevent the update services from running. | |
# | |
# WaaSMedicSvc | |
# BITS | |
# DoSvc | |
# UsoSvc | |
# wuauserv | |
# | |
#Requires -Version 5 | |
#Requires -RunAsAdministrator | |
function Set-ServiceToAuto { | |
param( | |
[Parameter(Mandatory=$true)] | |
[String]$Name | |
) | |
$private:Service = (Get-CimInstance -ClassName Win32_Service | where { $_.Name -match "$Name" }) | |
$ServiceHash = @{ | |
Name = $private:Service.Name | |
StartupType = 'Automatic' | |
} | |
if ( $private:Service.StartMode -match 'Disabled' ) { | |
$private:RegistryNotify = [System.Text.StringBuilder]::new() | |
[void]$private:RegistryNotify.Append('Setting service [') | |
[void]$private:RegistryNotify.Append($private:Service.DisplayName) | |
[void]$private:RegistryNotify.Append('] startup value to [') | |
[void]$private:RegistryNotify.Append($ServiceHash.StartupType) | |
[void]$private:RegistryNotify.Append(']') | |
$private:RegistryNotify.ToString() | |
try{ | |
Set-Service @ServiceHash | |
} | |
catch{ | |
"Unable to modify startup for service: $private:Service" | |
} | |
} else { | |
$private:SuccessNotify = [System.Text.StringBuilder]::new() | |
[void]$private:SuccessNotify.Append('Service [') | |
[void]$private:SuccessNotify.Append($private:Service.DisplayName) | |
[void]$private:SuccessNotify.Append('] is already enabled') | |
$private:SuccessNotify.ToString() | |
} | |
} | |
function Disable-ServiceByRegistry { | |
param( | |
[Parameter(Mandatory=$true)] | |
[String]$Name | |
) | |
$private:Service = (Get-CimInstance -ClassName Win32_Service | where { $_.Name -match "$Name" }) | |
$RegistryHash = @{ | |
Path = "HKLM:\SYSTEM\CurrentControlSet\Services\$Name" | |
Name = 'Start' | |
Value = 4 | |
Force = $true | |
} | |
if ( $private:Service.StartMode -notmatch 'Disabled' ) { | |
$private:RegistryNotify = [System.Text.StringBuilder]::new() | |
[void]$private:RegistryNotify.Append('Forcing service [') | |
[void]$private:RegistryNotify.Append($private:Service.DisplayName) | |
[void]$private:RegistryNotify.Append('] startup registry value to [4](disabled)') | |
$private:RegistryNotify.ToString() | |
try{ | |
Set-ItemProperty @RegistryHash | |
} | |
catch{ | |
"Unable to change registry value for service: $private:Service" | |
} | |
} else { | |
$private:SuccessNotify = [System.Text.StringBuilder]::new() | |
[void]$private:SuccessNotify.Append('Service [') | |
[void]$private:SuccessNotify.Append($private:Service.DisplayName) | |
[void]$private:SuccessNotify.Append('] is already disabled') | |
$private:SuccessNotify.ToString() | |
} | |
} | |
function Stop-ServiceByPID { | |
param( | |
[Parameter(Mandatory=$true)] | |
[String]$Name | |
) | |
$private:Service = (Get-CimInstance -ClassName Win32_Service | where { $_.Name -match "$Name" }) | |
$ProcessHash = @{ | |
Id = $private:Service.ProcessID | |
Force = $true | |
ErrorAction = 'SilentlyContinue' | |
} | |
if ( $private:Service.State -notmatch 'Stopped' ) { | |
try { | |
do { | |
$private:ProcessNotify = [System.Text.StringBuilder]::new() | |
[void]$private:ProcessNotify.Append('Stopping service [') | |
[void]$private:ProcessNotify.Append($private:Service.DisplayName) | |
[void]$private:ProcessNotify.Append('] by PID [') | |
[void]$private:ProcessNotify.Append($private:Service.ProcessID) | |
[void]$private:ProcessNotify.Append(']') | |
$private:ProcessNotify.ToString() | |
Stop-Process @ProcessHash | |
Start-Sleep -Milliseconds 200 | |
} until ( ($private:Service | Get-CimInstance).State -match 'Stopped' ) | |
if ( ($private:Service | Get-CimInstance).State -match 'Stopped' ) { | |
$private:SuccessNotify = [System.Text.StringBuilder]::new() | |
[void]$private:SuccessNotify.Append('Service [') | |
[void]$private:SuccessNotify.Append($private:Service.DisplayName) | |
[void]$private:SuccessNotify.Append('] is now stopped') | |
$private:SuccessNotify.ToString() | |
} | |
} catch { | |
"Unable to stop service $private:Service" | |
} | |
} else { | |
} | |
} | |
function Enable-UpdateService { | |
[CmdletBinding()] | |
param( | |
[Parameter()] | |
[string[]]$ServiceList = @( 'BITS', | |
'DoSvc', | |
'UsoSvc', | |
'wuauserv' | |
) | |
) | |
begin{} | |
process{ | |
foreach ($item in $ServiceList) { | |
Set-ServiceToAuto -Name $item | |
} | |
} | |
end{} | |
} | |
function Disable-UpdateService { | |
[CmdletBinding()] | |
param( | |
[Parameter()] | |
[string[]]$ServiceList = @( 'WaaSMedicSvc', | |
'BITS', | |
'DoSvc', | |
'UsoSvc', | |
'wuauserv' | |
) | |
) | |
begin{} | |
process{ | |
foreach ($item in $ServiceList) { | |
Disable-ServiceByRegistry -Name $item | |
Stop-ServiceByPID -Name $item | |
} | |
} | |
end{} | |
} | |
#Enable-UpdateService | |
Disable-UpdateService |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment