怎么重新启动协议服务器,如何启用或禁用服务器网络协议 (SQL Server PowerShell)

SQL Server 安装程序安装了 TCP 和 Named Pipes 网络协议,但这些协议可能并未启用。可以使用以下 PowerShell 脚本或者使用 SQL Server 配置管理器启用或禁用网络协议。必须停止然后再重新启动 SQL Server 数据库引擎,对协议所做的更改才会生效。

有关 PowerShell 的常规信息,请参阅 SQL Server PowerShell 概述。有关如何使用 SQL Server 配置管理器管理协议的详细信息,请参阅如何启用或禁用服务器网络协议(SQL Server 配置管理器)。

SQL Server PowerShell (SQLPS.exe) 实用工具会启动一个 PowerShell 会话,并加载和注册 SQL Server PowerShell 提供程序和 cmdlets。当运行 PowerShell (PowerShell.exe) 而非 SQL Server PowerShell 时,首先请执行以下语句以便手动加载所需的程序集。

# Load the assemblies

[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")

[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement")

下面的脚本会启用协议。若要禁用协议,请将 IsEnabled 属性设置为 $false。

使用 SQL Server PowerShell 启用服务器网络协议

使用管理员权限打开一个命令提示符。

若要启动 SQL Server PowerShell,请在命令提示符处键入 sqlps.exe。

执行以下语句以启用 TCP 和 Named Pipes 协议。将 替换为运行 SQL Server 的计算机的名称。如果您在配置命名实例,请将 MSSQLSERVER 替换为该实例的名称。

$smo = 'Microsoft.SqlServer.Management.Smo.'

$wmi = new-object ($smo + 'Wmi.ManagedComputer').

# List the object properties, including the instance names.

$Wmi

# Enable the TCP protocol on the default instance.

$uri = "ManagedComputer[@Name='']/ ServerInstance[@Name='MSSQLSERVER']/ServerProtocol[@Name='Tcp']"

$Tcp = $wmi.GetSmoObject($uri)

$Tcp.IsEnabled = $true

$Tcp.Alter()

$Tcp

# Enable the named pipes protocol for the default instance.

$uri = "ManagedComputer[@Name='']/ ServerInstance[@Name='MSSQLSERVER']/ServerProtocol[@Name='Np']"

$Np = $wmi.GetSmoObject($uri)

$Np.IsEnabled = $true

$Np.Alter()

$Np

为本地计算机配置协议

当脚本在本地运行并配置本地计算机时,SQL Server PowerShell 可以通过动态确定本地计算机的名称使脚本更为灵活。若要检索本地计算机的名称,请将设置 $uri 变量的行替换为以下行。

$uri = "ManagedComputer[@Name='" + (get-item env:\computername).Value + "']/ServerInstance[@Name='MSSQLSERVER']/ServerProtocol[@Name='Tcp']"

使用 SQL Server PowerShell 重新启动数据库引擎

启用或禁用了协议后,必须停止并重新启动数据库引擎才能使更改生效。执行以下语句,通过使用 SQL Server PowerShell 来停止和启动默认实例。若要停止和启动命名实例,请将 'MSSQLSERVER' 替换为 'MSSQL$'。

# Get a reference to the ManagedComputer class.

CD SQLSERVER:\SQL\

$Wmi = (get-item .).ManagedComputer

# Get a reference to the default instance of the Database Engine.

$DfltInstance = $Wmi.Services['MSSQLSERVER']

# 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

# Start the service again.

$DfltInstance.Start();

# Wait until the service has time to start.

# Refresh the cache and display the state of the service.

$DfltInstance.Refresh(); $DfltInstance

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值