Author Posts

March 13, 2018 at 4:45 pm

function Repair-SophosUpdate {

    [cmdletbinding()]

    Param(

         [Parameter(Mandatory=$True)]
         [string]$computername
    )
    $Services = Get-Service -Name 'Sophos*'

    ForEach ($Service in $Services) {

        If (($Service.Name -like 'Sophos*') -and ($Service.Status -eq 'running')) {
            Restart-Service -Name $Service -PassThru -Verbose

        }
         Else
         {

            Start-Service -Name $Service -PassThru -Verbose
        }
    }
} ; Repair-SophosUpdate

When I run the function I get

Restart-Service : Cannot find any service with service name 'System.ServiceProcess.ServiceController'.
At C:\Users\aolynyk\Desktop\Repair-SophosUpdate.ps1:15 char:13
+             Restart-Service -Name $Service -PassThru -Verbose
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (System.ServiceProcess.ServiceController:String) [Restart-Service], ServiceCommandException
    + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.RestartServiceCommand
 
Restart-Service : Cannot find any service with service name 'System.ServiceProcess.ServiceController'.
At C:\Users\aolynyk\Desktop\Repair-SophosUpdate.ps1:15 char:13
+             Restart-Service -Name $Service -PassThru -Verbose
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (System.ServiceProcess.ServiceController:String) [Restart-Service], ServiceCommandException
    + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.RestartServiceCommand
 
Restart-Service : Cannot find any service with service name 'System.ServiceProcess.ServiceController'.
At C:\Users\aolynyk\Desktop\Repair-SophosUpdate.ps1:15 char:13
+             Restart-Service -Name $Service -PassThru -Verbose
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (System.ServiceProcess.ServiceController:String) [Restart-Service], ServiceCommandException
    + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.RestartServiceCommand
 
Restart-Service : Cannot find any service with service name 'System.ServiceProcess.ServiceController'.
At C:\Users\aolynyk\Desktop\Repair-SophosUpdate.ps1:15 char:13
+             Restart-Service -Name $Service -PassThru -Verbose
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (System.ServiceProcess.ServiceController:String) [Restart-Service], ServiceCommandException
    + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.RestartServiceCommand
 
Restart-Service : Cannot find any service with service name 'System.ServiceProcess.ServiceController'.
At C:\Users\aolynyk\Desktop\Repair-SophosUpdate.ps1:15 char:13
+             Restart-Service -Name $Service -PassThru -Verbose
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (System.ServiceProcess.ServiceController:String) [Restart-Service], ServiceCommandException
    + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.RestartServiceCommand

The services exist and are running.

March 13, 2018 at 8:00 pm

Hi Alex,
The restart-service is missing the name of the service. see below update

function Repair-SophosUpdate {

    [cmdletbinding()]

    Param(

         [Parameter(Mandatory=$True)]
         [string]$computername
    )
    $Services = Get-Service -Name 'Sophos*'

    ForEach ($Service in $Services) {

        If (($Service.Name -like 'Sophos*') -and ($Service.Status -eq 'running')) {
            Restart-Service -Name $Service.Name -PassThru -Verbose

        }
         Else
         {

            Start-Service -Name $Service.Name -PassThru -Verbose
        }
    }
} ; Repair-SophosUpdate

Regards
Shihan

March 13, 2018 at 8:05 pm

Works perfectly. Thank you!