Created
February 16, 2021 10:24
-
-
Save kirill-d-lappo/369901673d7c43b5150c51c0fa68302b to your computer and use it in GitHub Desktop.
Powershell 7 : Parallel Functions For Windows Service Manipulations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #requires -version 7 | |
| # Requires pwsh 7 or higher | |
| # Add functions to your $PROFILE so they are always accessible | |
| function Restart-ServiceParallel{ | |
| [CmdletBinding()] | |
| param ( | |
| [Parameter()] | |
| [string] | |
| $Name | |
| ) | |
| Get-Service $Name | ForEach-Object -Parallel { Restart-Service $_ } | |
| } | |
| function Start-ServiceParallel{ | |
| [CmdletBinding()] | |
| param ( | |
| [Parameter()] | |
| [string] | |
| $Name | |
| ) | |
| Get-Service $Name | ForEach-Object -Parallel { Start-Service $_ } | |
| } | |
| function Stop-ServiceParallel{ | |
| [CmdletBinding()] | |
| param ( | |
| [Parameter()] | |
| [string] | |
| $Name | |
| ) | |
| Get-Service $Name | ForEach-Object -Parallel { Stop-Service $_ } | |
| } | |
| function Remove-ServiceParallel{ | |
| [CmdletBinding()] | |
| param ( | |
| [Parameter()] | |
| [string] | |
| $Name, | |
| [Parameter()] | |
| [switch] | |
| $Force | |
| ) | |
| if (!$Force){ | |
| $response = Read-Host -Prompt "Removed services can't be restored! Are you sure? (yes/no)" | |
| if ($response -eq 'y'){ | |
| $response = Read-Host -Prompt "Type 'yes'" | |
| } | |
| if ($response -ne 'yes'){ | |
| Write-Host "Aborted service removal." | |
| return | |
| } | |
| } | |
| Get-Service $Name | ForEach-Object -Parallel { Remove-Service $_ } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment