Solved

Start (or stop) multiple services on local machine

Posted on 2011-03-19
30
720 Views
Last Modified: 2012-05-11
I need to create a script that will read a list of services and start them if stopped or vice versa.
Because of dependencies they will need to start (or stop) in a given order.
These include SQL, Sharepoint and www - I only want these services running when I want to use sharepoint.
So I'd like to run the script before going into sharepoint and then run it again when finished.
Services are named:
SQL Server (Sharepoint)
Sharepoint 2010 Administration
Sharepoint 2010 Timer
Worldwide web publishing service
0
Comment
Question by:QPR
  • 15
  • 5
  • 5
  • +2
30 Comments
 
LVL 11

Expert Comment

by:FastFngrz
Comment Utility
Write a stop.bat and a go.bat with the appropriate 'net stop xxx' in the right order, and a 'net start xxx' in the opposite order. You might consider using net stop xxx /y to stop the dependant services too!
0
 
LVL 29

Author Comment

by:QPR
Comment Utility
I want to use PS
0
 
LVL 2

Expert Comment

by:anantshah
Comment Utility
Can you try the following

$services=Get-Service | where {$_.displayname -match 'SQL Server (Sharepoint)|Sharepoint 2010 Administration|Sharepoint 2010 Timer|Worldwide web publishing service'}
foreach ($service in $services){
    if ($service.Status -match "stopped")
    {
     Start-Service $service.name
    }
    else
    {
     Stop-Service $service.name
    }
    }
0
 
LVL 11

Expert Comment

by:FastFngrz
Comment Utility
Oohh, the above script won't get the order dependancies you desire..  In the same track, write a stop.ps1 with 'stop-service xxx' in the order you desire.
0
 
LVL 29

Author Comment

by:QPR
Comment Utility
SQL Server (Sharepoint) and Sharepoint 2010 Administration DONT start
but
Sharepoint 2010 Timer and Worldwide web publishing service DO start

when I paste the above into Powershell ISE
I have double checked the service display names to make sure they match the script but still no go

When I run the script again to stop them
Worldwide web publishing service DOES stop
but
Sharepoint 2010 Timer DOES NOT stop

seems to be irratic and unreliable
0
 
LVL 6

Expert Comment

by:rwskas
Comment Utility
What is the order you need? Start in this order, stop in reverse order?
SQL Server (Sharepoint)
Sharepoint 2010 Administration
Sharepoint 2010 Timer
Worldwide web publishing service
0
 
LVL 6

Expert Comment

by:rwskas
Comment Utility
If the above assumption is correct.. you could use this
#Get input
If ($Args) {
	If (!($Args -eq "STop" -or $args -eq "Start")) {
		Write-Host "Error - Invalid input";BREAK
	}
	Else { $Command = $Args[0] }
}
Else { Write-Host "ERROR - INPUT REQUIRED" ;break}

#Service/server params
$Services = "SQL Server (Sharepoint)","Sharepoint 2010 Administration","Sharepoint 2010 Timer","Worldwide web publishing service"
$Server = "Localhost"
If ($Command -eq "Stop") {
	$Services = $Services[4..0]
}

#Stop/Start services
Foreach ($Service in $Services) {
	Switch ($Command) {
		"Stop" { gwmi win32_service -comp $Server -Filter='name="$Service"' | %{$_.StopService()} }
		"Start" { gwmi win32_service -comp $Server -Filter='name="$Service"' | %{$_.StartService()} }
	}
}

Open in new window

0
 
LVL 11

Expert Comment

by:FastFngrz
Comment Utility
Are you trying to write/use/debug cool code, or are you needing to get the problem solved?  You might find there's a timing issue as well, so some Start-Sleep might be needed too!  I can solve this problem in 8 lines of code... very easy to maintain too!

SQL is the only 'real' dependancy here, although sharepoint will just complain that SQL isn't there and throw lots of errors in the Event Log.

Stop.ps1
Stop-Service Sharepoint 2010 Administration
Stop-Service Sharepoint 2010 Timer
Stop-Service Worldwide web publishing service
Stop-Service SQL Server (Sharepoint)

Start.ps1
Start-Service SQL Server (Sharepoint)
Start-Service Sharepoint 2010 Administration
Start-Service Sharepoint 2010 Timer
Start-Service Worldwide web publishing service

Elegant, simple, quick, and we could be on to the next problem in the time it's taken all of us to chat about this!  And to quote the MCP "End of line"  :)
0
 
LVL 29

Author Comment

by:QPR
Comment Utility
that is the order they are (attempting to) starting in as per the script above it.
Will try your suggestion when I get home thanks
0
 
LVL 29

Author Comment

by:QPR
Comment Utility
FastFngrz aren't your 8 lines the same as the previous script suggested - but without using a loop?
I tried this and it doesn't work
0
 
LVL 11

Expert Comment

by:FastFngrz
Comment Utility
Essentially they are the same, agreed, but in the spirit of simplicity and "get 'er done", I believe my code is more efficient.   Oh... I just noted that you'll need quotes around the service names because they have spaces in them.  (I usually refer to the Name, and not the DisplayName when coding)  

Also, w3svc's name is "world wide web publishing service" and not "worldwide web publishing service"

Lastly, if this is for your home dev machine, the only SQL sucks up a lot  of cycles, I stop/start just the w3svc and sql for when I want to develop under Sharepoint.
0
 
LVL 6

Expert Comment

by:rwskas
Comment Utility
For this application, yes yours is 'easier' However, it will not scale, if he decides to add more services to this process.. Write code for the future..
0
 
LVL 29

Author Comment

by:QPR
Comment Utility
www is one of the ones that does stop and start when the script is run :)
Yep I could manually stop/start the services but I was looking for something a little more single stepped... and a good excuse for me to get into PS
0
 
LVL 29

Author Comment

by:QPR
Comment Utility
rwskas - I pasted your script into PS ISE and executed and nothing happened.
No services started, no errors that I can see, just blinks
0
 
LVL 6

Expert Comment

by:rwskas
Comment Utility
Sorry, I should have clarified. Save the script, and run it by passing an argument:
The below will start the services
c:\Scripts\MyScript.ps1 Start
and to stop..
C:\Scripts\MyScript.ps1 Stop
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 29

Author Comment

by:QPR
Comment Utility
First I got this message after typing StopStart.ps1 Stop
-------------
Suggestion [3,General]: The command StopStart.ps1 was not found, but does exist in the current location. Wi
ell doesn't load commands from the current location by default. If you trust this command, instead type ".\
". See "get-help about_Command_Precedence" for more details.
-------------
So I typed this .\StopStart.ps1 stop
and then got the below errors

Get-WmiObject : A parameter cannot be found that matches parameter name 'Filter='name="$Service"''.
At D:\powershell cmdlets\start or stop services\StopStart.ps1:20 char:70
+         "Stop" { gwmi win32_service -comp $Server -Filter='name="$Service"' <<<<  | %{$_.StopService()} }
    + CategoryInfo          : InvalidArgument: (:) [Get-WmiObject], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetWmiObjectCommand

Get-WmiObject : A parameter cannot be found that matches parameter name 'Filter='name="$Service"''.
At D:\powershell cmdlets\start or stop services\StopStart.ps1:20 char:70
+         "Stop" { gwmi win32_service -comp $Server -Filter='name="$Service"' <<<<  | %{$_.StopService()} }
    + CategoryInfo          : InvalidArgument: (:) [Get-WmiObject], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetWmiObjectCommand

Get-WmiObject : A parameter cannot be found that matches parameter name 'Filter='name="$Service"''.
At D:\powershell cmdlets\start or stop services\StopStart.ps1:20 char:70
+         "Stop" { gwmi win32_service -comp $Server -Filter='name="$Service"' <<<<  | %{$_.StopService()} }
    + CategoryInfo          : InvalidArgument: (:) [Get-WmiObject], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetWmiObjectCommand

Get-WmiObject : A parameter cannot be found that matches parameter name 'Filter='name="$Service"''.
At D:\powershell cmdlets\start or stop services\StopStart.ps1:20 char:70
+         "Stop" { gwmi win32_service -comp $Server -Filter='name="$Service"' <<<<  | %{$_.StopService()} }
    + CategoryInfo          : InvalidArgument: (:) [Get-WmiObject], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetWmiObjectCommand
0
 
LVL 11

Expert Comment

by:FastFngrz
Comment Utility
I'll defer to you, but in my experience the simpler, the better.  Good luck!
0
 
LVL 6

Expert Comment

by:rwskas
Comment Utility
Sorry, thats a bad typo on my part... The -Filter="name='$service'" should be: -Filter "name='$service'"
new code attached.
#Get input
If ($Args) {
        If (!($Args -eq "STop" -or $args -eq "Start")) {
                Write-Host "Error - Invalid input";BREAK
        }
        Else { $Command = $Args[0] }
}
Else { Write-Host "ERROR - INPUT REQUIRED" ;break}

#Service/server params
$Services = "SQL Server (Sharepoint)","Sharepoint 2010 Administration","Sharepoint 2010 Timer","Worldwide web publishing service"
$Server = "Localhost"
If ($Command -eq "Stop") {
        $Services = $Services[4..0]
}

#Stop/Start services
Foreach ($Service in $Services) {
        Switch ($Command) {
                "Stop" { gwmi win32_service -comp $Server -Filter 'name="$Service"' | %{$_.StopService()} }
                "Start" { gwmi win32_service -comp $Server -Filter 'name="$Service"' | %{$_.StartService()} }
        }
}

Open in new window

0
 
LVL 29

Author Comment

by:QPR
Comment Utility
sorry this got buried among a ton of other stuff - will try it out this weekend
0
 
LVL 29

Author Comment

by:QPR
Comment Utility
Having a problem getting it to run.
When I try and run it from the PS command line it says it was not found but does exist in the current location.
So I put the full path to the file when calling the script - doesn't like spaces
So I enclose the full command in double quotes - one second later I'm back at the prompt - assume ran ok but no services stopped.
So I rename the folder where it lives so there are no spaces and run the script using the full path to it and then get an error about scripts being disabled and get pointed to a help topic that describes signatures etc!

Can I get past this? I am local admin on this workstation
0
 
LVL 29

Author Comment

by:QPR
Comment Utility
ok I had to run
set-executionpolicy unrestricted
I'm *guessing* it runs ok as there are no errors and after half a second the prompt returns.
No services stopped though.
I stop them manually and re-run passing "Start" they don't start either
I've kept the code to 2 services to keep it simpler (and this isn't the machine that the script is destined for)

I run the command (below) by opening a PS command prompt and typing
c:\ps_scripts\ToggleServices.ps1 Stop
#Get input
If ($Args) {
        If (!($Args -eq "Stop" -or $args -eq "Start")) {
                Write-Host "Error - Invalid input";BREAK
        }
        Else { $Command = $Args[0] }
}
Else { Write-Host "ERROR - INPUT REQUIRED" ;break}

#Service/server params
$Services = "Indexing Service","Java Quick Starter"
$Server = "Localhost"
If ($Command -eq "Stop") {
        $Services = $Services[4..0]
}

#Stop/Start services
Foreach ($Service in $Services) {
        Switch ($Command) {
                "Stop" { gwmi win32_service -comp $Server -Filter 'name="$Service"' | %{$_.StopService()} }
                "Start" { gwmi win32_service -comp $Server -Filter 'name="$Service"' | %{$_.StartService()} }
        }
}

Open in new window

0
 
LVL 8

Expert Comment

by:bchallis
Comment Utility
I have encountered a similar problem in that I do both .NET development and SharePoint work so need to with SQL Server, SharePoint and IIS.  My approach was to create some PowerShell cmdlets to use switches to determine what sets of services to start or stop.  (I found that needed to stop my Skype service as well).  I have attached the file that I use as part of a module that I use on my system. ManageServices.txt
0
 
LVL 29

Author Comment

by:QPR
Comment Utility
thanks I'll give it a try when a get a spare moment
0
 
LVL 29

Author Comment

by:QPR
Comment Utility
wow I forgot all about this one :)
ok I copied the entire text and saved it as StopStart.ps1
Opened a PS shell and CD'd to the correct folder and then typed
.\StopStart.ps1
and in under a second it just went back to a new prompt. No output to the screen, no services toggled.
Did I miss something?
0
 
LVL 8

Expert Comment

by:bchallis
Comment Utility
As it defines three functions after you dot source the file you then need to call the function with the appropriate switch values such as:

Start-BCMServices -SharePoint

Cheers
0
 
LVL 29

Author Comment

by:QPR
Comment Utility
dot source the file?
0
 
LVL 8

Expert Comment

by:bchallis
Comment Utility
As the script is just a collection of functions, running the script will simply declare/create the functions and store them in memory as without any code to call them, they will not execute.  If you run the script normally the functions will only exist while the script is running so as soon as the file is processed they will be removed.  If you dot source the file, that means calling it with a period followed by a space and then the file name:

. .\script.ps1

everything that is declared in the script will remain after the file is processed and and be used.  In this case it would mean that after the script has run you could type:

Start-BCMServices -sql

at the command line and the Start-BCMServices function would execute as the functions would still be in memory.

A way to check that the function is present is to type:
dir function:

at the command line and see if the functions are listed.

The way that I use files like these is that I have created a module which has a module file that dot sources the running of the
support/utility files that I use.

. $psScriptRoot\GeneralUtilities.ps1
. $psScriptRoot\CompareObjectOutput.ps1
. $psScriptRoot\GetDownloadQuotaLevel.ps1
. $psScriptRoot\GetFileSizesByType.ps1
. $psScriptRoot\ManageBCServices.ps1
. $psScriptRoot\NewPassword.ps1
. $psScriptRoot\SelectItemByAclEntry.ps1
. $psScriptRoot\WriteWhatif.ps1
. $psScriptRoot\BCPSLog.ps1
. $psScriptRoot\SelectItemFromList.ps1

Hope this helps clarify what dot sourcing means.
0
 
LVL 29

Author Comment

by:QPR
Comment Utility
Perfect sense... I now know what dot sourcing is and that individual functions reside in memory and can be called independant of the .ps1 file.
This is possibly a noob question but how can you create a permanent function like you would a .bat file with DOS?

I'd like to be able to call the stop or start function at any given time without having to run the ps1 file first or passing any parameters 9services)
All on or all off
0
 
LVL 8

Accepted Solution

by:
bchallis earned 500 total points
Comment Utility
There are two approaches to this.  
The most simple is to create a script file called profile.ps1 with the function in it in either your user hive windows powershell directory, in my case this is:
C:\Users\bchallis\Documents\WindowsPowerShell

or in the System directory
C:\Windows\System32\WindowsPowerShell\v1.0

The difference in using these two directories is that if it is in your user hive directory it will only be active when you use the computer where as if it is under the Windows directory it will be active for anyone logging on to the computer.  By calling the script profile.ps1 it will be used by every powershell console (PowerShell command line, PowerShell_ISE editor as examples).

The second option is to create a module which requires creating a subdirectory under the Modules directory in either your hive directory or the windows directory structure:
C:\Windows\System32\WindowsPowerShell\v1.0\Modules

and in that folder creating a file with the same name as the folder and with a psm1 extenstion.  In this file you put in the dot sourced names of the script files you want to include.  The list of dot sources files I included in the earlier post were from my psm1 file in the
C:\Users\bchallis\Documents\WindowsPowerShell\Modules\PBCPSModule
folder, called  PBCPSModule.psm1.

The in my profile script I simply included:
Import-Module PBCPSModule | Out-Null
to load the functionality in.

While this approach is a bit more complex it can be much more flexible as you can create individual script files per function to make them easier to maintain rather the the one huge 'Swiss army knife' profile and manage them in a block with the Import-Module cmdlet.



0
 
LVL 29

Author Closing Comment

by:QPR
Comment Utility
great thanks
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
This article shows how a content item can be identified directly or through translation of a navigation type. It then shows how this information can be used to create a menu for further navigation.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now