仅当SSMS启动时才启动SQL Server



我最近安装了SQL Server 2019,并且在启动时消耗了3 GB或RAM。

我只是在家里用它作个人用途,所以我把它关掉了。

我想知道是否有一种方法可以在PowerShellCMD中使用脚本:

  1. 当我启动SSMS时启动SQL Server (MSSQLSERVER)
  2. 关闭SSMS时停止SQL Server (MSSQLSERVER)

您可以创建脚本来启动/停止服务器(例如批处理文件或PowerShell(,然后运行SSMS.exe。

您还可以在以下位置启动/停止SQL服务器引擎:

命令提示符

net start "SQL Server (MSSQLSERVER)"
net stop "SQL Server (MSSQLSERVER)"

PowerShell

# Get a reference to the ManagedComputer class.
CD SQLSERVER:SQLcomputername
$Wmi = (get-item .).ManagedComputer
$DfltInstance = $Wmi.Services['MSSQLSERVER']
# Display the state of the service.
$DfltInstance
# Start the service.
$DfltInstance.Start();
# Wait until the service has time to start.
# Refresh the cache.  
$DfltInstance.Refresh();
# Display the state of the service.
$DfltInstance
# Stop the service.
$DfltInstance.Stop();
# Wait until the service has time to stop.
# Refresh the cache.
$DfltInstance.Refresh();
# Display the state of the service.
$DfltInstance

有关文档的更多详细信息。

这个脚本怎么样?它将等待SSMS退出,然后停止SQL Server。当然,SSMS可执行文件的路径可能与我的不同。

net start "SQL Server (MSSQLSERVER)"
START /WAIT "" "C:ProgramDataMicrosoftWindowsStart MenuProgramsMicrosoft SQL Server Tools 17Microsoft SQL Server Management Studio 17.lnk"
net stop "SQL Server (MSSQLSERVER)"

我自己找到了一个解决方案:

# Execute this script as Administrator
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit }
# Start all SQL* Services
Get-Service | where {$_.Name -like "SQL*"} |  Start-Service
# Start SSMS and wait till is closed
Start-Process "C:Program Files (x86)Microsoft SQL Server Management Studio 18Common7IDESsms.exe" -NoNewWindow -Wait
# Stop all SQL* Services
Get-Service | where {$_.Name -like "SQL*"} |  Stop-Service