Managing SQL Server Client Aliases with WMI Provider

 SQL Server client aliases make user connections easier, faster, and more convenient. Each alias saves all the information you need to connect to a SQL Server, such as the server name and the client protocol used to connect to a server. By using an alias, you do not need to enter the information each time you connect. You can also use an easy-to-remember nickname for your application that is different from the actual server name.

 

To view a list of aliases defined on the local computer:

Get-WmiObject -namespace root\Microsoft\SqlServer\ComputerManagement10 -class SqlServerAlias

 

The SqlServerAlias class does not define any methods. It inherits the methods from its parent class,ManagementObject. The Delete method of the ManagementObject class can be used to delete an existing alias:

$strComputer = "."
# Name of the alias
$strAliasName = "CH0DE1"
$oldalias=Get-WmiObject –computerName $strComputer -namespace root\Microsoft\SqlServer\ComputerManagement10 `
-class SqlServerAlias -filter "AliasName=’$strAliasName’"
$oldalias.Delete()

 

To get a list of loaded assemblies and use the Split-Path cmdlet to get only the filename:

[System.AppDomain]::CurrentDomain.GetAssemblies() | ForEach-Object { split-path $_.Location -leaf } | Sort-Object

 

To load the Microsoft.SqlServer.SqlWmiManagement.dll assembly for SQL Server 2008, use the LoadWithPartialName method of the System.Reflection.Assembly class. The Out-Null cmdlet is used to suppress the informational message:
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement") | Out-Null

For SQL Server 2005, the command should be as follows:
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | Out-Null

 

After the assembly is loaded, you need to first create an SMO object that represents the WMI installation for SQL Server on the local computer. This object will be the parent of the alias you create. The following commands instantiate an object for the SQL Server WMI installation on the local computer:
$strComputer='.'     # '.' or ‘localhost’ for local computer.
$objComputer=New-Object Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer
$strComputer


Now create an alias object from the Microsoft.SqlServer.Management.Smo.Wmi.ServerAlias class and set the parent of this alias to the ManagedComptuer object you created:
$newalias=New-Object ("Microsoft.SqlServer.Management.Smo.Wmi.ServerAlias")
$newalias.Parent=$objComputer


Each alias has a few properties, such as the name of the server to connect to and the client protocol, that need to be populated before it can be created. In this example, you recreate the CH0DE1 alias you deleted earlier. This alias connects to the named instance DEMOPC\CH0DE1 at port 7001 using the TCP/IP protocol:
$newalias.Name=’CH0DE1’ # name of the new alias
# DEMOPC\CH0DE1 is the SQL Server instance the alias points to
$newalias.ServerName=’DEMOPC\CH0DE1’
# 7001 is the port the SQL Server instance DEMOPC\CH0DE1 is listening on
$newalias.ConnectionString=7001
$newalias.ProtocolName=’tcp’
$newalias.Create()

 

The complete script:

# NAME: CreateServerAlias2008.ps1

# COMMENT: This script creates a new SQL Server alias on a computer that has the SQL Server 2008 client tools.
# =====================================================================================================

[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement") | Out-Null

$strComputer='.' # '.' or 'localhost' for local computer.
$objComputer=New-Object Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer $strComputer

$newalias=New-Object ("Microsoft.SqlServer.Management.Smo.Wmi.ServerAlias")
$newalias.Parent=$objComputer
$newalias.Name='CH0DE1' # name of the new alias
# DEMOPC\CH0DE1 is the SQL Server instance the alias points to
$newalias.ServerName='DEMOPC\CH0DE1'
# 7001 is the port the SQL Server instance DEMOPC\CH0DE1 is listening on
$newalias.ConnectionString=7001
$newalias.ProtocolName='tcp'
$newalias.Create()

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值