Managing Server Network Protocols with WMI Provider

本文介绍如何使用 PowerShell 脚本查看和修改 SQL Server 的网络协议属性,包括更改默认实例的端口号及为不同 IP 地址分配不同的端口号。

To view the list of properties associated with each network protocol for the default instance on the local computer:

Get-WmiObject -namespace root\Microsoft\SqlServer\ComputerManagement10 -class ServerNetworkProtocolProperty -filter "InstanceName = 'MSSQLSERVER'" | Select-Object ProtocolName, PropertyName, IPAddressName

 

To change the port number for IPAll from 1433 to another port — for example, 3660:

# NAME: ChangeDefaultPortNumber.ps1
# COMMENT: This script change the port number of the default instance from 1433 to 3660
$wmi=Get-WmiObject -namespace root\Microsoft\SqlServer\ComputerManagement10 -class ServerNetworkProtocolProperty -filter "PropertyName='TcpPort' and IPAddressName='IPAll' and InstanceName='MSSQLSERVER'"
$wmi.SetStringValue(3660) | Out-Null
$sqlservice = Get-WmiObject –namespace root\Microsoft\SqlServer\ComputerManagement10 -class SqlService –filter "ServiceName='MSSQLSERVER'"  #ServiceName='MSSQL`$INSTANCE1'
$sqlservice.StopService() | Out-Null
$sqlservice.StartService() | Out-Null
# Confirm the default port number has been changed
Get-WmiObject –namespace root\Microsoft\SqlServer\ComputerManagement10 -class ServerNetworkProtocolProperty -filter "PropertyName=’TcpPort’ and IPAddressName='IPAll' and InstanceName='MSSQLSERVER'" | Select-Object PropertyStrVal

To assign different port numbers to different IP addresses,the complete script,DisableListenOnAllIPs.ps1, is shown here:

# . for the local computer
# If you want to connect to a remote machine, specify the machine name here.
$strComputer = "."
# Name of the targeted SQL Server instance. Here the default instance is targeted.
# For a named instance INSTANCE1, use "INSTANCE1".
$strInstanceName = "MSSQLSERVER"
$wmi=Get-WmiObject -computerName $strComputer -namespace
root\Microsoft\SqlServer\ComputerManagement10 -class ServerNetworkProtocolProperty -filter "PropertyName=’ListenOnAllIPs’
and InstanceName=’$strInstanceName’"
$wmi.SetFlag(0) | Out-Null
$sqlservice = Get-WmiObject -computerName $strComputer –namespace
root\Microsoft\SqlServer\ComputerManagement10 `
-class SqlService –filter "ServiceName=’MSSQL`$$strInstanceName’"
$sqlservice.StopService() | Out-Null
$sqlservice.StartService() | Out-Null
$wmi=Get-WmiObject -computerName $strComputer -namespace
root\Microsoft\SqlServer\ComputerManagement10 `
-class ServerNetworkProtocolProperty -filter "PropertyName=’ListenOnAllIPs’
and InstanceName=’$strInstanceName’"
# Confirm the ListenOnAllIPs property has been disabled.
Write-Host "The value of the ListenOnAllIPs property is set to " $wmi.PropertyNumVal

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值