Sunday, January 2, 2011

Starting Service Using Powershell Commands.




Who Should Read This Blog: DBA or System engineer who wants to start the service using powershell commands.

There are two ways to start the service using powershell commands. I shall like to inform the blogger how to achieve that. Be patient and go through following information to understand hurdles as well.

I shall use get-service command first to start all the sql services in the server.

My new blog - How to Handle Sexual Harassment at Workplace
Custom Search
Method 1: Get-service: Take all the sql services in a variable

PS C:\> $services = get-service | Where-Object {$_.name -like "*sql*"}

Start the services one by one

PS C:\> foreach ($service in $services)
>> {Start-Service $service}

Start-Service : Cannot find any service with service name 'System.ServiceProcess.ServiceController'.
At line:2 char:15
+ {Start-Service <<<< $service} + CategoryInfo : ObjectNotFound: (System.ServiceProcess.ServiceCo ntroller:String) [Start-Service], ServiceCommandException + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell. Commands.StartServiceCommand

Let's Understand: Services are not started. We should have used either name or displayname in the $services. Let's try another approach.

PS C:\Windows\system32> $services = get-service | Where-Object {$_.name -like "*sql*"}
PS C:\Windows\system32> foreach ($service in $services)
>> {Start-Service $_.Displayname}
>>


Start-Service : Cannot bind argument to parameter 'Name' because it is null.
At line:2 char:15
+ {Start-Service <<<< $_.Displayname} + CategoryInfo : InvalidData: (:) [Start-Service], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.StartServic eCommand

Let's Understand: Services are not started again. This simply says that $_.name is null. Following is the correct manner to start the service using get-service:

PS C:\Windows\system32> $services = get-service | Where-Object {$_.name -like "*sql*"}
PS C:\Windows\system32> Start-Service -InputObject $services


WARNING: Waiting for service 'SQL Server Analysis Services (MSSQLSERVER) (MSSQLServerOLAPService)' to finish
starting...
WARNING: Waiting for service 'SQL Server Analysis Services (MSSQLSERVER) (MSSQLServerOLAPService)' to finish
starting...
WARNING: Waiting for service 'SQL Server Agent (MSSQLSERVER) (SQLSERVERAGENT)' to finish starting...


Method 2: Following is the method to start the services using gwmi win32_service or get-wmiobject win32_service.

Gathering the list of sql services in the server.

PS C:\Windows\system32> Get-WmiObject win32_service | Where-Object {$_.name -like "*sql*"}

ExitCode : 1077
Name : MSSQLFDLauncher
ProcessId : 0
StartMode : Manual
State : Stopped
Status : OK

ExitCode : 1077
Name : MSSQLSERVER
ProcessId : 0
StartMode : Manual
State : Stopped
Status : OK

ExitCode : 1077
Name : MSSQLServerADHelper100
ProcessId : 0
StartMode : Disabled
State : Stopped
Status : OK

ExitCode : 1077
Name : MSSQLServerOLAPService
ProcessId : 0
StartMode : Manual
State : Stopped
Status : OK

Starting the services. Following is a small command to do this.

PS C:\Windows\system32> Get-WmiObject win32_service | Where-Object {$_.name -like "*sql*"} | Start-Service
WARNING: Waiting for service 'SQL Server (MSSQLSERVER) (MSSQLSERVER)' to finish starting...
Start-Service : Service 'SQL Active Directory Helper Service (MSSQLServerADHelper100)' cannot be started due to the following error: Cannot start service MSSQLServerADHelper100 on computer '.'.
At line:1 char:83
+ Get-WmiObject win32_service | Where-Object {$_.name -like "*sql*"} | Start-Service <<<<
+ CategoryInfo : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service],
ServiceCommandException
+ FullyQualifiedErrorId : CouldNotStartService,Microsoft.PowerShell.Commands.StartServiceCommand

WARNING: Waiting for service 'SQL Server Analysis Services (MSSQLSERVER) (MSSQLServerOLAPService)' to finish
starting...
WARNING: Waiting for service 'SQL Server Analysis Services (MSSQLSERVER) (MSSQLServerOLAPService)' to finish
starting...
WARNING: Waiting for service 'SQL Server Agent (MSSQLSERVER) (SQLSERVERAGENT)' to finish starting...


Along with technical learning I would like to share some great articles for anyone interested in the betterment of his/her family life

2 comments:

  1. How wonderful it is that nobody need wait a single moment before starting to improve the world. See the link below for more info.


    #starting
    www.ufgop.org

    ReplyDelete
  2. you will have an excellent weblog right here! would you prefer to make some invite posts on my blog? online casinos

    ReplyDelete