ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    PowerShell Error

    IT Discussion
    4
    5
    561
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • alex.olynykA
      alex.olynyk
      last edited by

      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> 
      
      IRJI 1 Reply Last reply Reply Quote 0
      • ObsolesceO
        Obsolesce
        last edited by Obsolesce

        Is this script running locally?

        1 Reply Last reply Reply Quote 0
        • dafyreD
          dafyre
          last edited by dafyre

          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.

          1 Reply Last reply Reply Quote 0
          • ObsolesceO
            Obsolesce
            last edited by Obsolesce

            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
            }
            
            black3dynamiteB 1 Reply Last reply Reply Quote 3
            • black3dynamiteB
              black3dynamite @Obsolesce
              last edited by

              @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.

              1 Reply Last reply Reply Quote 0
              • 1 / 1
              • First post
                Last post