-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathWindowsServicesManager.ps1
More file actions
118 lines (100 loc) · 3.88 KB
/
WindowsServicesManager.ps1
File metadata and controls
118 lines (100 loc) · 3.88 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<#
.SYNOPSIS
Manages Windows services interactively by listing, starting, stopping, or restarting them.
.DESCRIPTION
This script lists services based on their status (Running, Stopped, Disabled) and allows the user to perform actions on selected services interactively.
.NOTES
Author: Emanuele Bartolesi
Version: 1.0
Created: 2024-11-17
.PARAMETER Status
Specifies the status of services to display: Running, Stopped, or Disabled. Default is Running.
.EXAMPLE
./WindowsServicesManager.ps1
.EXAMPLE
./WindowsServicesManager.ps1 -Status Stopped
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $false)]
[ValidateSet("Running", "Stopped", "Disabled")]
[string]$Status = "Running"
)
# Function to list services based on their status
function Get-ServicesByStatus {
param (
[string]$ServiceStatus
)
Write-Host "Fetching services with status: $ServiceStatus..." -ForegroundColor Yellow
switch ($ServiceStatus) {
"Running" { Get-Service | Where-Object { $_.Status -eq 'Running' } }
"Stopped" { Get-Service | Where-Object { $_.Status -eq 'Stopped' } }
"Disabled" {
Get-WmiObject -Class Win32_Service | Where-Object { $_.StartMode -eq 'Disabled' } | ForEach-Object {
[PSCustomObject]@{
Name = $_.Name
DisplayName = $_.DisplayName
Status = $_.State
StartMode = $_.StartMode
}
}
}
}
}
# Function to manage selected services
function Manage-Service {
param (
[PSCustomObject]$Service
)
Write-Host "Selected Service: $($Service.DisplayName)" -ForegroundColor Cyan
Write-Host "Options: [1] Start [2] Stop [3] Restart [4] Skip" -ForegroundColor Green
$Action = Read-Host "Enter your choice"
switch ($Action) {
"1" {
if ($Service.Status -eq "Stopped") {
Start-Service -Name $Service.Name
Write-Host "Service started: $($Service.DisplayName)" -ForegroundColor Green
} else {
Write-Host "Service is already running." -ForegroundColor Yellow
}
}
"2" {
if ($Service.Status -eq "Running") {
Stop-Service -Name $Service.Name
Write-Host "Service stopped: $($Service.DisplayName)" -ForegroundColor Green
} else {
Write-Host "Service is not running." -ForegroundColor Yellow
}
}
"3" {
if ($Service.Status -eq "Running") {
Restart-Service -Name $Service.Name
Write-Host "Service restarted: $($Service.DisplayName)" -ForegroundColor Green
} else {
Write-Host "Service is not running. Starting the service..." -ForegroundColor Yellow
Start-Service -Name $Service.Name
}
}
"4" {
Write-Host "Skipping service: $($Service.DisplayName)" -ForegroundColor Cyan
}
default {
Write-Host "Invalid choice. Skipping..." -ForegroundColor Red
}
}
}
# Main script execution
Write-Host "Windows Services Manager" -ForegroundColor Cyan
$Services = Get-ServicesByStatus -ServiceStatus $Status
if ($null -eq $Services) {
Write-Host "No services found with status: $Status" -ForegroundColor Yellow
return
}
Write-Host "Select a service to manage:" -ForegroundColor Green
$SelectedService = $Services | Out-GridView -Title "Select a Service to Manage" -PassThru
if ($null -ne $SelectedService) {
Manage-Service -Service $SelectedService
} else {
Write-Host "No service selected. Exiting..." -ForegroundColor Yellow
}
Write-Host "Windows Services Manager execution completed." -ForegroundColor Cyan