a.smith
a.smith

Reputation: 315

Why is my Powershell script checking if process is running giving error if it cannot find the process?

I have the following ps1 script:

 $process_check = Get-Service "Process" | findstr -i "Process" 
echo $process_check

When I execute it finds my process called "process" and gives the output

*output*
Running  Process        Process_displayName

I want to make sure my script gives me the expected output if "Process" is not running. To check this i created a new variable this time checking for process "QWERTY" so my full script is:

$process_check = Get-Service "Process" | findstr -i "Process" 
$qwerty_check = Get-Service "QWERTY" | findstr -i "QWERTY" 
echo $process_check
echo $qwerty_check

This outputs the following:

*output*
Running  Process        Process_displayName

Get-Service : Cannot find any service with service name 'qwerty'.
At C:\Users\proc_checks.ps1:5 char:14
+ $qwerty_check = Get-Service "qwerty" | findstr -i "qwerty"
+              ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (qwerty:String) [Get-Service], ServiceCommandException
    + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand 

Is there a way I can have the script not error if it cant find the process by my set process name (in this case "Process" and "qwerty")

Do I perhaps need an if statment checking for null before I assign my variables?

Upvotes: 0

Views: 3682

Answers (3)

Mark
Mark

Reputation: 434

Scepticalist has what I would use- your reply seems to indicate you're not familiar with a try/catch block. You can search on "try catch block" or "exception handling" to learn more.

Simply put- Scepticalist's code tries to execute the process check. If everything goes well, the code inside the catch never executes. However, if there is an error, the code inside the catch "catches" that error and tries to do something about it. There's also an optional "Finally" that will always run- regardless of errors.

So if you want that data in a variable- assign it to that variable in the catch block.

# Name of the process to check for
$ProcessRequest = 'OUTLOOK'

# Check for process by name
Try {
    $Process_Check = Get-Process -Name $ProcessRequest -ErrorAction Stop
    Write-Host "This line of code will not execute if the process check fails- execution immediately jumps to the catch"
}
Catch {
    #This code only executes if the code in try encounters an error
    $Process_Check = "Process not found for $ProcessRequest`nError: $_"
    Write-Host $Process_Check
}
Finally
{
    Write-Host "This code always runs, regardless of errors"
}

# The result of the query is now held in the $Process_Check object
# Output the object - you can pick any fields you need rather than *

$Process_Check | Select *

Upvotes: 1

Scepticalist
Scepticalist

Reputation: 3923

As Ansgar suggests, go and learn how to use Powershell, because mixing it with command line/batch is never going to make your scripting robust or efficient.

# Name of the process to check for
$ProcessRequest = 'OUTLOOK'

# Check for process by name
Try {
    $Process_Check = Get-Process -Name $ProcessRequest -ErrorAction Stop
}
Catch {
    Write-Host "Process not found for $ProcessRequest`nError: $_"
}

# The result of the query is now held in the $Process_Check object
# Output the object - you can pick any fields you need rather than *

$Process_Check | Select *

Upvotes: 0

dee-see
dee-see

Reputation: 24088

Get-Process errors out by design if it doesn't find anything. To change that behavior you can use the -ErrorAction SilentlyContinue parameter.

$process_check = Get-Service "Process" -ErrorAction SilentlyContinue | findstr -i "Process" 
echo $process_check

Note that -ErrorAction is a common parameter and will work on any cmdlet.

Upvotes: 1

Related Questions