PowerShell Error



  • 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 -Force
    
            }
             Else
             {
    
                Start-Service -Name $Service -PassThru -Verbose
            }
        }
    
    } ; Repair-SophosUpdate -Verbose
    

    When I run this I get

    PS C:\Users\aolynyk\Desktop> C:\Users\aolynyk\Desktop\Repair-SophosUpdate.ps1
    cmdlet Repair-SophosUpdate at command pipeline position 1
    Supply values for the following parameters:
    computername: localhost
    Restart-Service : Cannot find any service with service name 'System.ServiceProcess.ServiceController'.
    At C:\Users\aolynyk\Desktop\Repair-SophosUpdate.ps1:16 char:13
    +             Restart-Service -Name $Service -PassThru -Verbose -Force
    +             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (System.ServiceProcess.ServiceController:String) [Restart-Service], ServiceCommandException
        + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.RestartServiceCommand
    

    I can restart the services as an admin but when I run the script as an admin I get the same error

    PS C:\WINDOWS\system32> Restart-Service 'Sophos Agent' -PassThru
    
    Status   Name               DisplayName                           
    ------   ----               -----------                           
    Running  Sophos Agent       Sophos Agent                          
    
    
    
    PS C:\WINDOWS\system32> Restart-Service 'Sophos Message Router' -PassThru
    
    Status   Name               DisplayName                           
    ------   ----               -----------                           
    Running  Sophos Message ... Sophos Message Router                 
    
    
    
    PS C:\WINDOWS\system32> 
    


  • Is this script running locally?



  • Try:

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

    That makes it so that the command will run on the local machine if you don't specify another machine for it to run on.

    I think that @Tim_G has a good question as to whether or not the command is running locally or not.



  • What's the goal here?

    • Restart two known services named:
      • "Sophos Agent"
      • "Sophos Message Router"

    If that's the case, there's a much easier way to do it:

    function Restart-SophosServices {
    Restart-Service -Name "Sophos Agent" -Force -Verbose
    Restart-Service -Name "Sophos Message Router" -Force -Verbose
    }
    


  • @tim_g said in PowerShell Error:

    What's the goal here?

    • Restart two known services named:
      • "Sophos Agent"
      • "Sophos Message Router"

    If that's the case, there's a much easier way to do it:

    function Restart-SophosServices {
    Restart-Service -Name "Sophos Agent" -Force -Verbose
    Restart-Service -Name "Sophos Message Router" -Force -Verbose
    }
    

    Its either that or just wanted to restart all Sophos services.


Log in to reply