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.