Skip to content

Commit 1f3cd82

Browse files
committed
Update Get-ServiceInfo
rename, improve and bump
1 parent c37f887 commit 1f3cd82

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

ps-services/GetServiceInfo.ps1 renamed to ps-services/Get-ServiceInfo.ps1

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
Function GetServiceInfo {
1+
Function Get-ServiceInfo {
22
<#
33
.SYNOPSIS
44
Retrieves information about services on a specified computer.
55
66
.DESCRIPTION
7-
This function retrieves information about services on a local or remote computer, allows filtering by service name, provides options to include or exclude stopped and manually started services, and supports various output formats.
7+
Retrieves information about services on a local or remote computer, allows filtering by service name, provides options to include or exclude stopped and manually started services, and supports various output formats.
88
99
.PARAMETER ComputerName
1010
NotMandatory - name of the computer on which to retrieve the service information. If not provided, the local computer is used by default.
@@ -13,7 +13,7 @@ Function GetServiceInfo {
1313
.PARAMETER UserName
1414
NotMandatory - username to use for authentication when retrieving service information from a remote computer.
1515
.PARAMETER Password
16-
NotMandatory - password to use for authentication when retrieving service information from a remote computer. The password should be provided as a SecureString object.
16+
NotMandatory - password to use for authentication when retrieving service information from a remote computer, should be provided as a SecureString object.
1717
.PARAMETER PingBefore
1818
NotMandatory - indicates whether to ping the specified computer before retrieving the services. This can be useful to verify the computer's availability before performing the operation.
1919
.PARAMETER IncludeStopped
@@ -25,49 +25,55 @@ Function GetServiceInfo {
2525
.PARAMETER ProcessAsJob
2626
NotMandatory - executes the retrieval of service information as a background job. This allows for parallel processing and asynchronous execution.
2727
.PARAMETER FormatAsTable
28-
NotMandatory - the output as a table. If specified, the output is displayed in a table format.
28+
NotMandatory - the output as a table, if specified, the output is displayed in a table format.
2929
.PARAMETER ExportCsvPath
3030
NotMandatory - specifies the path to export the retrieved service information as a CSV file.
31+
.PARAMETER FilterWildcard
32+
NotMandatory - a wildcard pattern used to filter services based on their display names, only services with display names matching the pattern will be included in the output.
33+
3134
.EXAMPLE
3235
GetServiceInfo -ComputerName "Server01" -ServiceName "MyService"
3336
3437
.NOTES
35-
v0.0.1
38+
v0.0.2
3639
#>
3740
[CmdletBinding()]
3841
param (
39-
[Parameter(Mandatory = $false, Position = 0)]
42+
[Parameter(Mandatory = $false, Position = 0, ValueFromPipeline = $true)]
4043
[string]$ComputerName = $env:COMPUTERNAME,
41-
44+
4245
[Parameter(Mandatory = $false)]
4346
[string]$ServiceName,
44-
47+
4548
[Parameter(Mandatory = $false)]
4649
[string]$UserName,
47-
50+
4851
[Parameter(Mandatory = $false)]
4952
[SecureString]$Password,
50-
53+
5154
[Parameter(Mandatory = $false)]
5255
[switch]$PingBefore,
53-
56+
5457
[Parameter(Mandatory = $false)]
5558
[switch]$IncludeStopped,
56-
59+
5760
[Parameter(Mandatory = $false)]
5861
[switch]$IncludeManualStart,
59-
62+
6063
[Parameter(Mandatory = $false)]
6164
[switch]$SortByStatus,
62-
65+
6366
[Parameter(Mandatory = $false)]
6467
[switch]$ProcessAsJob,
65-
68+
6669
[Parameter(Mandatory = $false)]
6770
[switch]$FormatAsTable,
68-
71+
72+
[Parameter(Mandatory = $false)]
73+
[string]$ExportCsvPath,
74+
6975
[Parameter(Mandatory = $false)]
70-
[string]$ExportCsvPath
76+
[string]$FilterWildcard
7177
)
7278
BEGIN {
7379
$params = @{
@@ -98,6 +104,9 @@ Function GetServiceInfo {
98104
Write-Error "Failed to retrieve services: $($_.Exception.Message)"
99105
return
100106
}
107+
if ($FilterWildcard) {
108+
$Services = $Services | Where-Object { $_.DisplayName -like $FilterWildcard }
109+
}
101110
if (-not $IncludeStopped) {
102111
$Services = $Services | Where-Object { $_.Status -ne 'Stopped' }
103112
}
@@ -112,7 +121,7 @@ Function GetServiceInfo {
112121
}
113122
else {
114123
foreach ($Service in $Services) {
115-
$ServiceInfo = @{
124+
$ServiceInfo = [PSCustomObject]@{
116125
Name = $Service.Name
117126
DisplayName = $Service.DisplayName
118127
Status = $Service.Status
@@ -122,11 +131,13 @@ Function GetServiceInfo {
122131
Account = $Service.ServiceAccountName
123132
StartName = $Service.StartName
124133
}
134+
125135
if ($ProcessAsJob) {
126136
$Job = Start-Job -ScriptBlock {
127137
param($Info)
128138
Write-Output $Info
129139
} -ArgumentList $ServiceInfo
140+
130141
$ServiceInfo.JobId = $Job.Id
131142
}
132143
Write-Output $ServiceInfo

0 commit comments

Comments
 (0)