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()

 

你可能感兴趣的:(sql,server,object,Microsoft,assembly,Class,sqlserver)