Skip to content

Commit 0fc51ca

Browse files
Merge pull request #95 from igierard/bugfix/pwsh7
Refactors Disable-Service to use CIM commands
2 parents f7a14a0 + cb7ae93 commit 0fc51ca

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
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");

packages/scaffold/scripts/windows/enable_script_execution.ps1

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,3 @@ force-mkdir "HKLM:\SOFTWARE\Policies\Microsoft\Windows"
66

77
Set-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows" -Name "EnableScripts" -Type DWord -Value 1
88
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell" -Name "ExecutionPolicy" -Type String -Value "RemoteSigned"
9-
10-
HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows

0 commit comments

Comments
 (0)