And after that, when I type the first command to find the Automatic services those are stopped, as per the C:\ServerList.txt, the output remains the same:
Could any please tell me, where I am doing wrong in the above command. I am unable to get answers for below things to be honest:
When we are passing objects as Strings and “Start-Service” does take object Strings, why this is not working?
If it’s working for rest of the services, as it was throwing error for “stisvc” only, the rest of the services shouldn’t come up again, when I run the first command. That means it’s not working for the rest either.
Checked on the set of servers, which are in the C:\ServerList.txt file, but couldn;t find the services started, but it’s not throwing any errors either. Further on found all those services exist on Local machine, from where I am trying to run the above command. Is it trying to start services locally? But why?
Though I understood that you are asking me to use “StartService” Method, which I can get when I pipe “Get-WmiObject Win32_Service” to Get-Member, but am unable to figure out how to do that.
Get-WmiObject win32_service -Filter "startmode = 'auto' and state != 'running'" -computername (Get-Content C:\ServerList.txt) |
ForEach-Object {
$result = $_.StartService()
# You can check the value of $result.ReturnValue here; if it's zero, the service started successfully.
}
Brilliant One Dave! Thank You very much! It really made the one liner script work, but now I have a new problem…
After doing the one-liner script, though it started the services on remote computers, but when I tried to retrieve the data whether it worked or not, through the next line command
“Get-WmiObject win32_service -Filter “startmode = ‘auto’ and state != ‘running’” -computername (Get-Content C:\ServerList.txt) | Select-Object Name, State, Status”
(Believe me, I again couldn’t figure out, how to check the value of $result.Returnvalue as advised by you above… Still learning Powershell )
I got list of services like the below:
Name Status State
clr_optimization_v4.0.30319_32 OK Stopped
clr_optimization_v4.0.30319_64 OK Stopped
gupdate OK Stopped
CTXCPUBal OK Stopped
gupdate OK Stopped
clr_optimization_v4.0.30319_32 OK Stopped
clr_optimization_v4.0.30319_64 OK Stopped
Though they might have started and stopped, but in the list of 20 servers, how to find out on which server it really didn’t start…
Is there anyway to get the respective Server Name along with the above output?
Here’s a fairly straightforward way of outputting the errors if the StartService method failed. It doesn’t take any action, just displays the error on screen. The WMI methods return Win32 error codes, so you can translate them into useful error messages via the Win32Exception class.
Get-WmiObject win32_service -Filter "startmode = 'auto' and state != 'running'" -computername (Get-Content C:\ServerList.txt) |
ForEach-Object {
$result = $_.StartService()
if ($result.ReturnValue -ne 0)
{
$exception = New-Object System.ComponentModel.Win32Exception([int]$result.ReturnValue)
Write-Host -ForegroundColor Red "Server: $($_.__SERVER), Error code: $($result.ReturnValue), Message: $($exception.Message)"
}
}
In addition to that, another query is “Start-Service” doesn’t mention anywhere that it doesn’t work on remote computers and only works on local server… have gone through the “Get-Help Start-Service -Full” command, but nowhere it’s mentioned…
But couldn’t find any parameter as -ComputerName in the syntax, which I assumed that the key to understand that the command works only on local machine, but am I correct in assuming so, or is there any other way available in Powershell??
I’ll test this when I’m at home tonight, but I suspect that you actually can use Start-Service on remote computers. You’d just need to pipe it objects obtained from Get-Service, not from Get-WmiObject.
That being said, I would expect the WMI version you’re already using to give better performance. When you’re using Get-Service, you’d have to retrieve records for every service on the remote machines, then use Where-Object to filter them based on start type and status. Using Get-WmiObject’s -Filter parameter does that filtering on the remote computer, and only sends you the data you really need.
But isn’t it correct that … to get details about “Automatic” services, we have to use “WMI-Object”, as Get-Service can only provide “Name” , “Status” & “DisplayName” properties for a service, but not “Statup Type” or “Executable path” or “LogonAs” properties?
That’s the reason why I had to choose Wmi-Object actually… as first I tried a lot to get the data through Get-Service, as my initial assumption was… Get-Service will get me everything related to Service…
Please correct me if I am wrong…
And really Thank You for helping me out and guiding me till now… Much appreciate it and you guys are the reason why we are able to learn Powershell… Thank You Very Much!
I hadn’t realized that before, but it looks like you’re right. The .NET ServiceController class (which is what Get-Service and Start-Service use) doesn’t provide any information about the startup type for some reason. With that in mind, just stick with WMI.
It appears to work with some services and not others. Something could be stepping on it when it tries to start the Spooler since it’s set to disabled on the server I tried it on.