# 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
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)