|
| 1 | +Function Get-ServicesStartupType { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Get services based on startup type, scope, and optional criteria. |
| 5 | +
|
| 6 | + .DESCRIPTION |
| 7 | + This function retrieves services based on specified startup type, scope, and optional criteria. It provides detailed information about each service and a summary based on the start types. |
| 8 | +
|
| 9 | + .PARAMETER StartType |
| 10 | + Specifies the startup type of services to filter. Valid values are "Automatic", "Manual", "Disabled", or "AllStartTypes", default value is "Automatic". |
| 11 | + .PARAMETER Scope |
| 12 | + Specifies the scope of services to filter. Valid values are "LocalMachine" or "AllScopes", default value is "LocalMachine". |
| 13 | + .PARAMETER CanPauseAndContinue |
| 14 | + Switch parameter to include only services that support pause and continue operations. |
| 15 | +
|
| 16 | + .EXAMPLE |
| 17 | + Get-ServicesStartupType -StartType Automatic -Scope AllScopes -CanPauseAndContinue |
| 18 | +
|
| 19 | + .NOTES |
| 20 | + v0.0.1 |
| 21 | + #> |
| 22 | + [CmdletBinding()] |
| 23 | + param ( |
| 24 | + [Parameter(Mandatory = $false, Position = 0)] |
| 25 | + [ValidateSet("Automatic", "Manual", "Disabled", "AllStartTypes")] |
| 26 | + [string]$StartType = "Automatic", |
| 27 | + |
| 28 | + [Parameter(Mandatory = $false, Position = 1)] |
| 29 | + [ValidateSet("LocalMachine", "AllScopes")] |
| 30 | + [string]$Scope = "LocalMachine", |
| 31 | + |
| 32 | + [Parameter(Mandatory = $false)] |
| 33 | + [switch]$CanPauseAndContinue |
| 34 | + ) |
| 35 | + BEGIN { |
| 36 | + $ServiceObjects = @() |
| 37 | + } |
| 38 | + PROCESS { |
| 39 | + $Services = Get-Service |
| 40 | + $FilteredServices = $Services | Where-Object { |
| 41 | + ($_.StartType -eq $StartType -or $StartType -eq "AllStartTypes") -and |
| 42 | + ($CanPauseAndContinue.IsPresent -eq $false -or $_.CanPauseAndContinue) |
| 43 | + } |
| 44 | + foreach ($Service in $FilteredServices) { |
| 45 | + $ServiceObjects += [PSCustomObject]@{ |
| 46 | + Path = $Service.DisplayName |
| 47 | + Name = $Service.ServiceName |
| 48 | + StartType = $Service.StartType |
| 49 | + CanPauseAndContinue = $Service.CanPauseAndContinue |
| 50 | + Status = $Service.Status |
| 51 | + Description = $Service.Description |
| 52 | + DisplayName = $Service.DisplayName |
| 53 | + Scope = $Scope |
| 54 | + Type = "Service" |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + END { |
| 59 | + if ($ServiceObjects.Count -eq 0) { |
| 60 | + Write-Warning "No services found with the specified criteria." |
| 61 | + } |
| 62 | + else { |
| 63 | + Write-Output -InputObject $ServiceObjects |
| 64 | + Write-Host "`nService Information Summary" -ForegroundColor Green |
| 65 | + $ServiceObjects | Group-Object StartType | ForEach-Object { |
| 66 | + "$($_.Name) Start Type: $($_.Count)" |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments