Summary: Here is the answer to Problem 4 in Windows PowerShell Mini-Scripting Games 2014.
Microsoft Scripting Guy, Ed Wilson, is here. It is time for the answer to Problem 4 in Mini-Scripting Games 2014. Last week, I posted the complete Problem 4 description and task outline.
Problem 4 synopsis
Your manager tasked you with what he considers to be a really important issue. The people at the Help Desk want a tool that will permit them to easily stop processes. Your manager doesn’t want to give them permission to use Task Manager because it carries too much power.
He decided that he wants you to write a script that will permit people who run the script to kill any process that they have user rights to stop. Your manager does not want people to be able to stop any process, only those for which they have user rights.
Approaching the problem
First of all, I need to admit to a bogus requirement. The part about security was a distractor. Windows PowerShell does not permit a user to do anything that the user does not have rights to do. Therefore, if there is a process the user does not have access to, that user will not be able to kill that process. So that portion of the problem description can be ignored.
Now for the second part…
A graphical interface is all in the eye of the beholder. The hint said don’t do any more work than required, so I decided to use the Out-GridView cmdlet with the –PassThru parameter. This will permit me to select processes from the list, and kill them by using the Stop-Process cmdlet.
Here are the steps to my approach:
- Use Get-Process to grab the list of processes.
- Use Out-GridView to display the processes.
- Use Stop-Process to stop the processes.
Here is my script:
Out-GridView -PassThru |
That is it. I use Get-Process to grab the processes, and I pipe them to the Out-GridView cmdlet. The trick is to use the –PassThru parameter so that the process objects that are selected will pass to Stop-Process.
In the Out-GridView pane, all I need to do is select the processes I want to kill. It is a graphical thingy after all, so I feel it meets the requirements. Sweet. Three very simple lines of script.
That is all there is to solving Problem 4. Mini-Scripting Games Answer Week will continue tomorrow when I will talk about Problem 5.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy