如何启用或禁用服务器网络协议

如何启用或禁用服务器网络协议,第1张

QL Server PowerShell (SQLPSexe) 实用工具会启动一个 PowerShell 会话,并加载和注册 SQL Server PowerShell 提供程序和 cmdlets。当运行 PowerShell (PowerShellexe) 而非 SQL Server PowerShell 时,首先请执行以下语句以便手动加载所需的程序集。
# Load the assemblies
[reflectionassembly]::LoadWithPartialName("MicrosoftSqlServerSmo")
[reflectionassembly]::LoadWithPartialName("MicrosoftSqlServerSqlWmiManagement")
下面的脚本会启用协议若要禁用协议,请将 IsEnabled 属性设置为 $false。
使用 SQL Server PowerShell 启用服务器网络协议
使用管理员权限打开一个命令提示符。
若要启动 SQL Server PowerShell,请在命令提示符处键入 sqlpsexe。
执行以下语句以启用 TCP 和 Named Pipes 协议。将 <computer_name> 替换为运行 SQL Server 的计算机的名称。如果您在配置命名实例,请将 MSSQLSERVER 替换为该实例的名称。
$smo = 'MicrosoftSqlServerManagementSmo'
$wmi = new-object ($smo + 'WmiManagedComputer')
# List the object properties, including the instance names
$Wmi
# Enable the TCP protocol on the default instance
$uri = "ManagedComputer[@Name='<computer_name>']/ ServerInstance[@Name='MSSQLSERVER']/ServerProtocol[@Name='Tcp']"
$Tcp = $wmiGetSmoObject($uri)
$TcpIsEnabled = $true
$TcpAlter()
$Tcp
# Enable the named pipes protocol for the default instance
$uri = "ManagedComputer[@Name='<computer_name>']/ ServerInstance[@Name='MSSQLSERVER']/ServerProtocol[@Name='Np']"
$Np = $wmiGetSmoObject($uri)
$NpIsEnabled = $true
$NpAlter()
$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$<instance_name>'。
# Get a reference to the ManagedComputer class
CD SQLSERVER:\SQL\<computer_name>
$Wmi = (get-item )ManagedComputer
# Get a reference to the default instance of the Database Engine
$DfltInstance = $WmiServices['MSSQLSERVER']
# Display the state of the service
$DfltInstance
# Stop the service
$DfltInstanceStop();
# Wait until the service has time to stop
# Refresh the cache
$DfltInstanceRefresh();
# Display the state of the service
$DfltInstance
# Start the service again
$DfltInstanceStart();
# Wait until the service has time to start
# Refresh the cache and display the state of the service
$DfltInstanceRefresh(); $DfltInstance


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/13484287.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-08-16
下一篇 2023-08-16

发表评论

登录后才能评论

评论列表(0条)

保存