Search code examples
powershellpowershell-3.0

Powershell get process script not working correctly


I want this script to show me WinWord processes which are running currently and to send an email alert out. Currently the 1st script works if there are WinWord processes running and lists them in an email BUT if they are no WinWord processes running the script fails advising parameter is empty or null, cannot find process.

The second script works if there are no processes and doesn’t throw the null or empty parameter if there are no WinWord processes running but only shows true or false by email. Not the list of processes running. Also would like to add some write host in the script for the email output to say. Below are the number of WinWord’s Processes running.

I am a newbie to PowerShell. I am learning as I go. I also used -erroraction silentlycontinue for the first script but when outputting to email it still fails with empty or null parameter.

Below are the scripts.

Doesn't work if process is not running, cannot accept argument which is null or empty. If process is running works perfect.

$winwords = (get-process -ComputerName Dummyserver -Name Winword |where-object { $_.Count -gt 0})| Out-String

Send-MailMessage -To "Dummyemail.co.uk" -From " Dummyemail.co.uk" -Subject "WinWord Check" -SmtpServer " Dummy.priv" -Body $winwords

Works if argument is null or empty but an email alert is out which says false or true BUT doesn't display the amount of processes running like the first script.

$processname = "Winword"
$instanceisrunning = (get-process -ComputerName Dummyserver| where-object { $_.Name -eq $processname }).count -gt 0 | Out-String

Send-MailMessage -To "Dummyemail co.uk" -From "Dummyemail.co.uk" -Subject "WinWord Check" -SmtpServer " Dummyemail.priv" -Body $isrunning

Solution

  • You have the right idea using where-object to limit the processes to just the name you want. The logic you are using for count greater than zero will return a boolean ($true or $false), which is why you are seeing that output.

    If you want to always send a message when this is run, you can do this:

    $instances = (get-process -ComputerName Dummyserver | where-object { $_.Name -eq $processname })
    
    if($instances.count -gt 0)
    {
       $message = $instances | Out-String
    }
    else
    {
       $message = "No instances running"
    }
    
    Send-MailMessage -To "Dummyemail co.uk" -From "Dummyemail.co.uk" -Subject "WinWord Check" -SmtpServer " Dummyemail.priv" -Body $message
    

    If you only want a message sent when instances are running:

    $instances = (get-process -ComputerName Dummyserver | where-object { $_.Name -eq $processname })
    
    if($instances.count -gt 0)
    {
       Send-MailMessage -To "Dummyemail co.uk" -From "Dummyemail.co.uk" -Subject "WinWord Check" -SmtpServer " Dummyemail.priv" -Body ($instances | Out-String)
    }
    

    Edit: Adding code to work of multiple computers with one email

    $computers = @("dummyserver","otherserver","nextserver")
    
    $processName = "WinWord"
    
    $message = "$processName is running on the following:"
    
    foreach ($computer in $computers)
    {
        $message += "Hostname: $computer"
    
        $instances = (Get-Process -ComputerName $computers | Where-Object { $_.Name -eq $processName })
    
        if($instances.count -gt 0)
        {
           $message += ($instances | Out-String)
        }
        else
        {
           $message += "No instances running"
        }
    }
    
    Send-MailMessage -To "Dummyemail co.uk" -From "Dummyemail.co.uk" -Subject "WinWord Check" -SmtpServer " Dummyemail.priv" -Body $message