Welcome › Forums › General PowerShell Q&A › Function
This topic contains 2 replies, has 2 voices, and was last updated by
Participant
11 months, 2 weeks ago.-
AuthorPosts
-
March 13, 2018 at 4:45 pm #95817
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.
-
March 13, 2018 at 8:00 pm #95834
Hi Alex,
The restart-service is missing the name of the service. see below updatefunction 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-
March 13, 2018 at 8:05 pm #95835
Works perfectly. Thank you!
-
-
AuthorPosts
The topic ‘Function’ is closed to new replies.