Skip to content

Instantly share code, notes, and snippets.

@lwalthert
Created January 9, 2017 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lwalthert/f2a2c85c8577f193da7b6ad4bcb34055 to your computer and use it in GitHub Desktop.
Save lwalthert/f2a2c85c8577f193da7b6ad4bcb34055 to your computer and use it in GitHub Desktop.
Should Process Example
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