-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathServiceManagement.psm1
More file actions
77 lines (68 loc) · 1.99 KB
/
ServiceManagement.psm1
File metadata and controls
77 lines (68 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# ServiceManagement Module
# Provides functions for managing Windows services
function Get-ServicePendingStatus {
[CmdletBinding()]
param(
[Parameter()]
[ValidateSet('Start', 'Stop', 'All')]
[string]$Status = 'All'
)
switch ($Status) {
'Start' {
Get-WmiObject -Class win32_service | Where-Object {$_.state -eq 'start pending'}
}
'Stop' {
Get-WmiObject -Class win32_service | Where-Object {$_.state -eq 'stop pending'}
}
'All' {
Get-WmiObject -Class win32_service | Where-Object {$_.state -match 'pending'}
}
}
}
function Stop-PendingServices {
[CmdletBinding()]
param()
$Services = Get-WmiObject -Class win32_service -Filter "state = 'stop pending'"
if ($Services) {
foreach ($service in $Services) {
try {
Stop-Process -Id $service.processid -Force -PassThru -ErrorAction Stop
Write-Output "Successfully stopped service: $($service.Name)"
}
catch {
Write-Warning -Message "Error stopping service $($service.Name). Error details: $_.Exception.Message"
}
}
}
else {
Write-Output "No services with 'Stop Pending' status"
}
}
function Restart-WMIService {
[CmdletBinding()]
param(
[Parameter()]
[switch]$Force
)
try {
Restart-Service -Name Winmgmt -Force:$Force -ErrorAction Stop
Write-Output "Successfully restarted WMI service"
}
catch {
Write-Warning -Message "Error restarting WMI service. Error details: $_.Exception.Message"
}
}
function Get-ServiceList {
[CmdletBinding()]
param(
[Parameter()]
[string]$Filter = "*"
)
Get-CimInstance -Class Win32_Service -Filter "Name like '$Filter'"
}
Export-ModuleMember -Function @(
'Get-ServicePendingStatus',
'Stop-PendingServices',
'Restart-WMIService',
'Get-ServiceList'
)