Function

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
8 months ago.

  • Author
    Posts
  • #95817

    Participant
    Points: 1
    Rank: Member
    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.

  • #95834

    Participant
    Points: -17
    Rank: Member

    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

    • #95835

      Participant
      Points: 1
      Rank: Member

      Works perfectly. Thank you!

The topic ‘Function’ is closed to new replies.