Sql server 仅在启动SSMS时启动SQL Server

Sql server 仅在启动SSMS时启动SQL Server,sql-server,powershell,cmd,scripting,ssms,Sql Server,Powershell,Cmd,Scripting,Ssms,我最近安装了SQL Server 2019,并且在启动时消耗了大量的3 GB或RAM 我只是在家里私人使用,所以我把它关掉了 我想知道是否有办法在PowerShell或CMD中使用脚本: 启动SSMS时启动SQL Server(MSSQLSERVER) 关闭SSMS时停止SQL Server(MSSQLSERVER) 您可以创建启动/停止服务器的脚本(例如批处理文件或PowerShell),然后运行SSMS.exe 您还可以在以下位置启动/停止SQL server引擎: 命令提示 net sta

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

我只是在家里私人使用,所以我把它关掉了

我想知道是否有办法在
PowerShell
CMD
中使用脚本:

  • 启动SSMS时启动SQL Server(MSSQLSERVER)
  • 关闭SSMS时停止
    SQL Server(MSSQLSERVER)

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

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

    命令提示

    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
    

    有关的详细信息。

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

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

    命令提示

    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
    

    有关的详细信息。

    此脚本如何?它将等待SSMS退出,然后停止SQL Server。当然,您的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)"
    

    这个剧本怎么样?它将等待SSMS退出,然后停止SQL Server。当然,您的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
    

    我自己找到了一个解决办法:

    # 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和SSM,并将其固定到“开始”菜单或任务Ba。在退出SSM时,硬部分将是一种阻止SQL Server的方法。此外,考虑将SQL Server设置为合理的限制。@ von Pryz,当我工作时,我需要充分的力,所以这不是一个选择,因为您已经标记了——只需创建一个脚本来启动SQLServer和SSMS和PIN,以启动菜单或任务BA。当你退出SSM时,硬部分将是一种停止SQL Server的方法。此外,考虑将SQL Server设置为合理的限制。@ Von Pryz,当我工作时,我需要完全的力,所以这不是一个选项。