Function

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

 
Participant
8 months ago.

  • Author
    Posts
  • #95738

    Participant
    Points: 1
    Rank: Member

    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.

  • #95739

    Keymaster
    Points: 1,624
    Helping HandTeam Member
    Rank: Community Hero

    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.

  • #95750

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

  • #95778

    Participant
    Points: 207
    Helping Hand
    Rank: Participant

    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
            }
        }
    
  • #95810

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

The topic ‘Function’ is closed to new replies.