Function

When I run the function

function Repair-SophosUpdate {

     [cmdletbinding()]

     Param(

     [Parameter(Mandatory=$True)]
     [string]$computername

     )

$services = Get-Service -Name 'Sophos Agent', 'Sophos Message Router'

if ($services -eq 'running') {

Restart-Service -Name 'Sophos Agent', 'Sophos Message Router' -PassThru -Verbose

}

}  
;  Repair-SophosUpdate

I get no output

PS C:\Users\aolynyk\Desktop> .\Repair-SophosUpdate.ps1
cmdlet Repair-SophosUpdate at command pipeline position 1
Supply values for the following parameters:
computername: localhost

PS C:\Users\aolynyk\Desktop> 

I’m sure its something simple. I’m very new to scripting.

That’s because it’s not restarting anything.

Services’ names are different from the display names, and you’re providing the display names. So Restart-Service isn’t finding anything to restart, ergo, no output.

PS C:\Users\aolynyk> gsv | where {$_.Name -like "Sop*"}

Status   Name               DisplayName                           
------   ----               -----------                           
Running  Sophos Agent       Sophos Agent                          
Running  Sophos AutoUpda... Sophos AutoUpdate Service             
Running  Sophos Message ... Sophos Message Router                 
Running  Sophos Web Cont... Sophos Web Control Service            
Running  sophossps          Sophos System Protection Service    

The Name and Display Name look the same. Where do I go from here?

If you are targeting only specific services in the collection.
Just be more direct. I do not have Sophos, So, I’m app* services

    function Repair-SophosUpdate {

         [cmdletbinding()]

         Param(

         # [Parameter(Mandatory=$True)]
         [string]$computername = $env:COMPUTERNAME

         )

        $services = Get-Service -Name 'app*'
        ForEach ($Service in $services) 
        {
            if (($Service.Name -match 'Mgmt|Svc') -and ($Service.Status -match 'Stopped'))
            {
                "$($Service.Name) is not running. Attempting restart"
                Restart-Service -Name $($Service.Name) -PassThru -Verbose -WhatIf
            }
        }
    } 
    Repair-SophosUpdate


    AppIDSvc is not running. Attempting restart
    What if: Performing the operation "Restart-Service" on target "Application Identity (AppIDSvc)".
    AppMgmt is not running. Attempting restart
    What if: Performing the operation "Restart-Service" on target "Application Management (AppMgmt)".
    AppXSvc is not running. Attempting restart
    What if: Performing the operation "Restart-Service" on target "AppX Deployment Service (AppXSVC) (AppXSvc)".

You’d just change this above to this

    $services = Get-Service -Name 'Sophos*'
    ForEach ($Service in $services) 
    {
        if ($Service.Name -match 'Agent|Router')
        {
            "$($Service.Name) is not running. Attemting restart"
            Restart-Service -Name $($Service.Name) -PassThru -Verbose -WhatIf
        }
    }
function Repair-SophosUpdate {

     [cmdletbinding()]

     Param(
[Parameter(Mandatory=$True)]
     [string]$computername = $env:COMPUTERNAME
)

$services = Get-Service -Name 'Sophos*'
ForEach ($Service in $services)
{
if ($services.Name -like 'Sophos*') and ($services.Status -eq 'running')
{
Restart-Service -Name $services -PassThru -Verbose
}}}

else Start-Service -Name $services -PassThru -Verbose

I have this but on line 13 it reads it’s missing a statement block after if condition