So if a service has a startup type of "Automatic" or "Manual" but is currently stopped I would like to run a command to see those issues.
In powershell and CMD I am able to see one OR the other, but there is no easy way I can find to filter the data to what I need.
I am basically looking to make script for trouble shooting services. It is going to be able to determine if there are any services that should be started (based on their startup type) but are not running (stopped or suspended).
The issue I am running into is that powershell or CMD do not allow for in depth filter or piping of results. Does any one have a way to help me figure this out?
How can I go about solving this issue?
14 Answers
By looking at this question you could come up with this for old versions of PowerShell:
Get-WmiObject -Class Win32_Service | Select-Object Name,State,StartMode | Where-Object {$_.State -ne "Running" -and $_.StartMode -eq "Auto"}With newer Versions (at least 5 maybe 3/4) you could also use (which was suggested by JC2k8):
Get-Service | Select-Object -Property Name,Status,StartType | Where-Object {$_.Status -eq "Stopped" -and $_.StartType -eq "Automatic"}In older versions of PowerShell the Get-Service cmdlet doesn't offer a member that has the StartType.
PowerShell supports a lot of filtering and piping. :)
4(get-service|?{ $_.Status -eq "Stopped" -and $_.StartType -eq "Automatic"})|
select DisplayName, StartType, Status 3 Get-Service | Where-Object {$_.Status -eq "Stopped"} | where starttype -match automatic You may try this if suits your issue. The below code helps you start all stopped services related to Sophos AV that should be running because they have Automatic start type.
# Start specific automatic start services not running
$service = "*sophos*"
$server = "<server_name>"
$stoppedServices = (Get-WmiObject Win32_Service -ComputerName $server | Where-Object {$_.Name -like $service -and $_.StartMode -eq 'Auto' -and $_.State -ne "Running"}).Name
foreach ($stoppedService in $stoppedServices) { Write-Host -NoNewline "Starting Server/Service: "; Write-Host -ForegroundColor Green $server"/"$stoppedService Get-Service -ComputerName $server -Name $stoppedService | Start-Service
}