Author Posts

September 18, 2013 at 6:13 am

Hi Guys,

I am trying to create a script here which will find the Stopped "Automatic" services on the servers, as per "C:\ServerList.txt" and will Start them.

With the below command:

Get-wmiobject win32_service -Filter "startmode = 'auto' and state != 'running'" -computername (Get-Content C:\ServerList.txt) | Select-Object -ExpandProperty Name

I get the below output:

CTXCPUBal
ShellHWDetection
ShellHWDetection
ShellHWDetection
stisvc

But when I put "Start-Service" in the pipeline after the whole command,

Get-wmiobject win32_service -Filter "startmode = 'auto' and state != 'running'" -computername (Get-Content C:\ServerList.txt) | Select-Object -ExpandProperty Name | Start-Service

I get the below error:

Start-Service : Cannot find any service with service name 'stisvc'.
At line:1 char:203
+ Get-wmiobject win32_service -Filter "startmode = 'auto' and state != 'running'" -computername (Ge
t-Content C:\UTILS\FarmChecks\Uptime\ServerList.txt) | Select-Object -ExpandProperty Name | Start-S
ervice <<<< + CategoryInfo : ObjectNotFound: (stisvc:String) [Start-Service], ServiceCommandExcep tion + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.StartServic eCommand 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: CTXCPUBal ShellHWDetection ShellHWDetection ShellHWDetection stisvc 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: 1. When we are passing objects as Strings and "Start-Service" does take object Strings, why this is not working? 2. 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. 3. 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? Any help would be much appreciated. Kind Regards, Surya

September 18, 2013 at 6:23 am

start-service only works on the local server

you will need to rewrite your script so that you test the service and use the StartService method of Win32_Service to start the service

September 18, 2013 at 8:35 am

Hi Richard,

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.

Could you please help me with that.

Cheers!
Surya

September 18, 2013 at 8:43 am

Give this a try:

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.
}

September 18, 2013 at 9:21 am

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?

September 18, 2013 at 9:50 am

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)"
    }
}

September 18, 2013 at 9:53 am

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??

September 18, 2013 at 10:04 am

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.

September 18, 2013 at 10:13 am

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!

September 18, 2013 at 10:18 am

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. 🙂

September 18, 2013 at 10:38 am

Sure... will do that... 🙂

Thank You Again!

August 3, 2016 at 7:44 am

Hello Suryakanta,

Do you have a working script model for the same ? As i am looking for something similar to this .

August 5, 2016 at 6:24 pm

While this doesn't answer the start-up type. I have used this successfully in the past to start remote services.

Start-Service -InputObject $(Get-Service -ComputerName Server1 -Name Spooler)
Try
{
    $(Get-Service -ComputerName Server1 -Name Spooler).WaitForStatus('Running','00:02:00')
}
Catch
{
    Write-Warning "Service didn't start after two minutes"
}

Edit:

I just tried this in my test lab and it failed. Maybe it doesn't work as well as I thought it did.

August 5, 2016 at 6:44 pm

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.