selecting "A" or "B" in process using Powershell

Hi guys,

I am trying to write my first powershell script.
using powersehll, I want to delete all currently running processesAs and ProcessBs.
I wrote the following code, but it seems that it doesn't end anything.
how can i make this work? (There can be multiple running As and multiple running Bs)

function Kill-RunningProcess(){
        $processesA = @(Get-Process sqlServr -ErrorAction SilentlyContinue)
        $processesB = @(Get-Process winword -ErrorAction SilentlyContinue )
        $array=@($processesA, $processesB)
        if($array){
            foreach($process in $processes){
                $process.Kill()
            }
       }
    }
IzzyTwinklyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zulazenCommented:
Try this:

function kill-runningprocess {

$ProcessesA = get-process -name sqlServr -ErrorAction SilentlyContinue
$ProcessesB = get-process -name winword -ErrorAction SilentlyContinue

$ProcessesA | stop-process
$ProcessesB | stop-process

}

Open in new window


or simply

function kill-runningprocess{
get-process -name sqlServr -ErrorAction SilentlyContinue | stop-process
get-process -name winword -ErrorAction SilentlyContinue | stop-process
}

Open in new window

oBdACommented:
Get-Process accepts an array for process names to retrieve, so you can do that easily in a single line:
Get-Process -Name sqlServr, winword | Stop-Process

Open in new window

Or like this:
Get-Process -Name sqlServr, winword | ForEach-Object {$_.Kill()}

Open in new window

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Above suggestions are correct, and I would prefer to do it that way without any need for vars, but if staying as close as possible to your code, this is the only wrong line:
       foreach($process in $processes){

Open in new window

because you refer to $processes, which does not have a value. You need
       foreach($process in $array){

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
IzzyTwinklyAuthor Commented:
Hi Qlemo,
Your answer is very simple and clear. Thanks you!

Thanks to oBdA as well!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.