Created
January 9, 2017 13:06
-
-
Save lwalthert/f2a2c85c8577f193da7b6ad4bcb34055 to your computer and use it in GitHub Desktop.
Should Process Example
This file contains 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
function Stop-ProcessUsingWMI | |
{ | |
[CmdletBinding(SupportsShouldProcess=$True)] param( | |
[parameter(mandatory=$true)] [regex] $pattern | |
) | |
foreach ($process in Get-WmiObject Win32_Process | | |
where { $_.Name -match $pattern }) | |
{ | |
if ($PSCmdlet.ShouldProcess( | |
"process $($process.Name) " + | |
" (id: $($process.ProcessId))" , | |
"Stop Process")) | |
{ | |
$process.Terminate() | |
} | |
} | |
} | |
#Next, start a Notepad process: | |
#notepad | |
#Now call Stop-ProcessUsingCIM, specifying the -WhatIf parameter: | |
#Stop-ProcessUsingCIM notepad -Whatif | |
#What if: Performing operation "Stop Process" on Target "process notepad.exe (id: 6748)". |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment