PowerShell bulk start/Stop SQL Server services in Windows

Source: Internet
Author: User
Tags echo 7 goto mssql net command sleep

The SQL Server service is not set to automatic on its own development machine because it takes up too much memory and affects boot time too much

And every time you use SQL Server, run to the Windows Control Panel to open the service, feel the operation is very cumbersome

So I wrote a little script to open the SQL Server service with PowerShell.

SERVICE_SQL_SERVER.PS1:

The code is as follows Copy Code

$Service _name = "MSSQL ' $SQLSERVER"

$Service = Get-service $Service _name | Findstr $Service _name | Findstr "Stopped"

if ([String]::isnullorempty ($Service))
{
Write-host $Service _name "service has started or cannot be found"
}
Else
{
Write-host $Service _name "Start ..."
Start-service $Service _name
Write-host $Service _name "started ..."
}

[System.threading.thread]::sleep (3000)

Service_sql_server.bat:

The code is as follows Copy Code
PowerShell D:projectpowershellservice_sql_server.ps1
Sleep 5

Use PowerShell command net start MSSQL$MSSQL05 service name is invalid

Solutions


Add-name to PowerShell under the sun? Q??? To?? SQL Server.

Start-service-name ' mssql$mssql05 '
Before the correction of the same r, the second one in the reply. Description of the command:

In PowerShell, the original order?? 臃??

Cmd/c ' net start ' mssql$mssql05 '

Or

Cmd/c ' sc start ' mssql$mssql05 '

Stop the costume?? /p>

CMD/C ' net top ' mssql$mssql05 '

Or

Cmd/c ' sc stop ' mssql$mssql05 '

--------------------------------------------------------------------------------
Folding@home

Batch code found on the Web

According to the net command, you can start the services for each instance by writing a batch script implementation. Copy the following code, stored as a batch file "Start&stopsqlserver.bat" with the suffix named bat:

The code is as follows Copy Code
@echo off
: A
The list of instances of the Echo native:
Echo---------------------------
Echo 1 Pc143sql2005de1
Echo 2 Pc143sql2005de2
Echo 3 Pc143sql2005de3
Echo 4 PC143SQL2005DE4
Echo 5 PC143SQL2005DE5
Echo 6 Pc143sql2005de6
Echo 7 Pc143sql2005de7
Echo 8 Pc143sql2005de8
Echo 9 Pc143sql2005de9
Echo Pc143sql2005de10
Echo---------------------------
Echo Action action:
Echo 1 Start Service
echo 0 Stop Service
Echo---------------------------
Echo.
set/p var2= Please enter action action: [1/0]
set/p var1= Please enter the instance number: [1/2/3/4/5/6/7/8/9/10]
If%var1% ==1 if%var2% ==1 goto S1
If%var1% ==2 if%var2% ==1 goto S2
If%var1% ==3 if%var2% ==1 goto S3
If%var1% ==4 if%var2% ==1 goto S4
If%var1% ==5 if%var2% ==1 goto S5
If%var1% ==6 if%var2% ==1 goto S6
If%var1% ==7 if%var2% ==1 goto S7
If%var1% ==8 if%var2% ==1 goto S8
If%var1% ==9 if%var2% ==1 goto S9
If%var1% ==10 if%var2% ==1 goto S10
If%var1% ==1 if%var2% ==0 goto T1
If%var1% ==2 if%var2% ==0 goto T2
If%var1% ==3 if%var2% ==0 goto T3
If%var1% ==4 if%var2% ==0 goto T4
If%var1% ==5 if%var2% ==0 goto T5
If%var1% ==6 if%var2% ==0 goto T6
If%var1% ==7 if%var2% ==0 goto T7
If%var1% ==8 if%var2% ==0 goto T8
If%var1% ==9 if%var2% ==0 goto T9
If%var1% ==10 if%var2% ==0 goto T10
Echo.
Cls
Goto A:
Echo.
: S1
NET Start sqlagent$sql2005de1/y
NET Start msftesql$sql2005de1/y
Goto Endapp
Echo.
: S2
NET Start sqlagent$sql2005de2/y
NET Start msftesql$sql2005de2/y
Goto Endapp
Echo.
: S3
NET Start sqlagent$sql2005de3/y
NET Start msftesql$sql2005de3/y
Goto Endapp
Echo.
: S4
NET Start sqlagent$sql2005de4/y
NET Start msftesql$sql2005de4/y
Goto Endapp
Echo.
: S5
NET Start sqlagent$sql2005de5/y
NET Start msftesql$sql2005de5/y
Goto Endapp
Echo.
: S6
NET Start sqlagent$sql2005de6/y
NET Start msftesql$sql2005de6/y
Goto Endapp
Echo.
: S7
NET Start sqlagent$sql2005de7/y
NET Start msftesql$sql2005de7/y
Goto Endapp
Echo.
: S8
NET Start sqlagent$sql2005de8/y
NET Start msftesql$sql2005de8/y
Goto Endapp
Echo.
: S9
NET Start sqlagent$sql2005de9/y
NET Start msftesql$sql2005de9/y
Goto Endapp
Echo.
: S10
NET Start sqlagent$sql2005de10/y
NET Start msftesql$sql2005de10/y
Goto Endapp
Echo.
: T1
NET Stop mssql$sql2005de1/y
NET Stop msftesql$sql2005de1/y
Goto Endapp
Echo.
: T2
NET Stop mssql$sql2005de2/y
NET Stop msftesql$sql2005de2/y
Goto Endapp
Echo.
: T3
NET Stop mssql$sql2005de3/y
NET Stop msftesql$sql2005de3/y
Goto Endapp
Echo.
: T4
NET Stop mssql$sql2005de4/y
NET Stop msftesql$sql2005de4/y
Goto Endapp
Echo.
: T5
NET Stop mssql$sql2005de5/y
NET Stop msftesql$sql2005de5/y
Goto Endapp
: T6
NET Stop mssql$sql2005de6/y
NET Stop msftesql$sql2005de6/y
Goto Endapp
: T7
NET Stop mssql$sql2005de7/y
NET Stop msftesql$sql2005de7/y
Goto Endapp
: T8
NET Stop mssql$sql2005de8/y
NET Stop msftesql$sql2005de8/y
Goto Endapp
: T9
NET Stop mssql$sql2005de9/y
NET Stop msftesql$sql2005de9/y
Goto Endapp
: T10
NET Stop mssql$sql2005de10/y
NET Stop msftesql$sql2005de10/y
Goto Endapp
: Endapp
Set/p var3= whether to continue: [y/n]
If%var3% = = y goto A:

This shows a batch script that is started and stopped by 10 instances of SQL Server on PC143. Let's run this script to start one of the instance service pc143sql2005de4 on PC143:

Turn off the instance service, similar to starting the instance service, such as:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.