Skip to content

Instantly share code, notes, and snippets.

@heaths
Created December 11, 2018 01:25
Show Gist options
  • Select an option

  • Save heaths/fb250e361520bd34343ce123f2489790 to your computer and use it in GitHub Desktop.

Select an option

Save heaths/fb250e361520bd34343ce123f2489790 to your computer and use it in GitHub Desktop.
Detects processes using services and optionally stops those processes.
#Requires -Version 3
# The MIT License (MIT)
# Copyright (C) Microsoft Corporation. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
[CmdletBinding()]
param (
[Parameter(Position = 0)]
[ValidateNotNullOrEmpty()]
[string[]] $Name = 'VSStandardCollectorService150',
[Parameter()]
[switch] $Stop,
[Parameter()]
[switch] $Force
)
$ErrorActionPreference = 'Stop'
$loc = data {
ConvertFrom-StringData @'
Error_Elevation_Required = You must run this script in an elevated command prompt
Error_PackageManagement_Required = Please install PackageManagement from http://go.microsoft.com/fwlink/?LinkID=746217
Verbose_Install_RestartManager = Installing the "RestartManager" module
Verbose_RestartManager_Scanning = Scanning for processes using the specified services
Verbose_RestartManager_Stopping = Stopping services
'@
}
$identity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$principal = New-Object System.Security.Principal.WindowsPrincipal $identity
if (!$principal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)) {
throw $loc.Error_Elevation_Required
}
# Install the module if missing.
if (!(Get-Module -ListAvailable RestartManager)) {
Write-Verbose $loc.Verbose_Install_RestartManager
# Make sure PackageManagement is installed (comes with WMF 5.0 / Windows 10).
if (!(Get-Module -ListAvailable PackageManagement)) {
throw $loc.Error_PackageManagement_Required
}
Install-Module RestartManager -Scope CurrentUser -SkipPublisherCheck -Force
}
$session = Start-RestartManagerSession -PassThru
try {
Get-Service $Name | Register-RestartManagerResource -Session $session
Write-Verbose $loc.Verbose_RestartManager_Scanning
Get-RestartManagerProcess -Session $session
if ($Stop) {
Write-Verbose $loc.Verbose_RestartManager_Stopping
Stop-RestartManagerProcess -Session $session -Force:$Force
}
}
finally {
Stop-RestartManagerSession -Session $session
}
<#
.SYNOPSIS
Detects processes using services and optionally stops those processes.
.DESCRIPTION
Gets a list of processes using a service, including the service process itself.
You can pass '-Stop' to optionally attempt to stop those services using the
Windows Restart Manager.
.PARAMETER Name
One or more service names. The default is 'VSStandardCollectorService150'.
.PARAMETER Stop
Stop the processes using the specified service(s).
.PARAMETER Force
Forcibly stop the processes using the specified service(s). Requires '-Stop'.
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment