rev_ollie 0 Newbie Poster

Hi,

I'm trying to write a "quick" powershell script to end processes passed as arguments:

$args
$check = Get-process $args

foreach ($arg in $args) {
    if ($check -eq $null)
    {
    Write-host $arg " - Its not running"
    }
    else
    {
    Write-host $arg " - It's running"
    }
}

I'm trying to find the best way of either reportingthe process isn't running.
If I open Notepad.exe and then run this with the argument "notepad" it will work as expected.

If I open Notepad.exe and run this with "notepad" "excel" (excel is not active at this point) - I will get the exception and it will report that both excel and notepad are running.
Exception = "Cannot find a process with the name "excel" (Expected bahaviour of course as its not open).

Is there a way to make this work?
I thought perhaps catching the exception and reporting from that - or finding a better way of actually searching for the process.
I can use -ea 0 to stop the exception showing but it will still report both processes running...

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.