仅在启动 SSMS 时启动 SQL 服务器
Start SQL Server only when SSMS is started
我最近安装了 SQL Server 2019,并且在启动时占用了 3 GB 或 RAM。
我只是在家里个人使用它,所以我把它关掉了。
我想知道是否有办法在 PowerShell
或 CMD
中编写脚本:
- 当我启动 SSMS
时启动 SQL Server (MSSQLSERVER)
- 当我关闭 SSMS
时停止 SQL Server (MSSQLSERVER)
您可以创建脚本 start/stop 服务器(例如批处理文件或 PowerShell),然后 运行 SSMS.exe。
您还可以 start/stop SQL 服务器引擎:
命令提示符
net start "SQL Server (MSSQLSERVER)"
net stop "SQL Server (MSSQLSERVER)"
PowerShell
# Get a reference to the ManagedComputer class.
CD SQLSERVER:\SQL\computername
$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
有关 docs 的更多详细信息。
这个脚本怎么样?它将等到 SSMS 退出,然后停止 SQL 服务器。当然,您的 SSMS 可执行文件路径可能与我的不同。
net start "SQL Server (MSSQLSERVER)"
START /WAIT "" "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft SQL Server Tools 17\Microsoft 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 18\Common7\IDE\Ssms.exe" -NoNewWindow -Wait
# Stop all SQL* Services
Get-Service | where {$_.Name -like "SQL*"} | Stop-Service
我最近安装了 SQL Server 2019,并且在启动时占用了 3 GB 或 RAM。
我只是在家里个人使用它,所以我把它关掉了。
我想知道是否有办法在 PowerShell
或 CMD
中编写脚本:
- 当我启动 SSMS 时启动
- 当我关闭 SSMS 时停止
SQL Server (MSSQLSERVER)
SQL Server (MSSQLSERVER)
您可以创建脚本 start/stop 服务器(例如批处理文件或 PowerShell),然后 运行 SSMS.exe。
您还可以 start/stop SQL 服务器引擎:
命令提示符
net start "SQL Server (MSSQLSERVER)"
net stop "SQL Server (MSSQLSERVER)"
PowerShell
# Get a reference to the ManagedComputer class.
CD SQLSERVER:\SQL\computername
$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
有关 docs 的更多详细信息。
这个脚本怎么样?它将等到 SSMS 退出,然后停止 SQL 服务器。当然,您的 SSMS 可执行文件路径可能与我的不同。
net start "SQL Server (MSSQLSERVER)"
START /WAIT "" "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft SQL Server Tools 17\Microsoft 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 18\Common7\IDE\Ssms.exe" -NoNewWindow -Wait
# Stop all SQL* Services
Get-Service | where {$_.Name -like "SQL*"} | Stop-Service