Skip to content

Commit fba6301

Browse files
committed
Refactors Disable-Service to use CIM commands
Removed WMI based workflow from disable service Also changes how disabling Windows Update Medic Service works in order to prevent false error messages.
1 parent f7a14a0 commit fba6301

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

packages/scaffold/scripts/functions.psm1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,17 @@ function Disable-Service(
123123
[String]$ComputerName = "$(hostname)"
124124
) {
125125
# See https://social.technet.microsoft.com/Forums/lync/en-US/abde2699-0d5a-49ad-bfda-e87d903dd865/disable-windows-update-via-powershell?forum=winserverpowershell
126-
$service = Get-WmiObject Win32_Service -Filter "Name='$ServiceName'" -ComputerName $ComputerName -Ea 0;
126+
$service = Get-CimInstance win32_service -Filter "Name='$ServiceName'" -Ea 0;
127127
if ($service) {
128128
if ($service.StartMode -ne "Disabled") {
129-
$result = $service.ChangeStartMode("Disabled").ReturnValue;
129+
$result = (Invoke-CimMethod -InputObject $service -methodname ChangeStartmode -Arguments @{startmode='Disabled'}).ReturnValue;
130130
if ($result) {
131131
Write-Host "Failed to disable the '$ServiceName' service on $ComputerName. The return value was $result." -ForegroundColor Red;
132132
} else {
133133
Write-Host "Successfully disabled the '$ServiceName' service on $ComputerName." -ForegroundColor Green;
134134
}
135135
if ($service.State -eq "Running") {
136-
$result = $service.StopService().ReturnValue;
136+
$result = (Invoke-CimMethod -InputObject $service -methodname StopService).ReturnValue.ReturnValue;
137137
if ($result) {
138138
Write-Host "Failed to stop the '$ServiceName' service on $ComputerName. The return value was $result." -ForegroundColor Red;
139139
} else {

packages/scaffold/scripts/windows/disable_update_service.ps1

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,19 @@ try {
66
}
77

88
Write-Output "Disabling Windows Update Medic Service"
9+
# Windows Update Medic Service can only be disabled via the registry
910
Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\WaaSMedicSvc' -Name 'Start' -value 4
10-
Disable-Service("WaaSMedicSVC");
11+
Write-Host "Successfully disabled the 'WaaSMedicSVC' service on $ComputerName." -ForegroundColor Green;
12+
$WaaSMedicSVC = get-ciminstance win32_service -Filter "Name='WaaSMedicSVC'" -Ea 0;
13+
if($WaaSMedicSVC.State -eq "Running"){
14+
$ComputerName = "$(hostname)"
15+
$result = (Invoke-CimMethod -InputObject $WaaSMedicSVC -methodname StopService).ReturnValue
16+
if ($result) {
17+
Write-Host "Failed to stop the 'WaaSMedicSVC' service on $ComputerName. The return value was $result." -ForegroundColor Red;
18+
} else {
19+
Write-Host "Successfully stopped the 'WaaSMedicSVC' service on $ComputerName." -ForegroundColor Green;
20+
}
21+
}
1122

1223
Write-Output "Disabling Update Orchestrator Service"
1324
Disable-Service("UsoSvc");

0 commit comments

Comments
 (0)