SQL Server Alias management with PowerShell and WMI – Update for SQL Server 2012

In 02/2011 I created a PowerShell script to set / get / remove SQL Server aliases:

https://blog.kenaro.com/2011/02/09/enumerate-add-update-and-remove-sql-server-aliases-by-using-powershell/

Today I updated it for SQL Server 2012 and tested it on Windows Server 2012 and Windows Server 2008 R2.

You can download it here:

http://gallery.technet.microsoft.com/SQL-Server-2008-2012-Alias-baf05737

2 thoughts on “SQL Server Alias management with PowerShell and WMI – Update for SQL Server 2012

  1. Love the script, by the way.
    Small tweak needed – using Server 2008r2 and SQL 2008r2 64 bit, was getting “Invalid namespace” errors when connectint calls “$s.connect()”. I think you need to try calling connect in your try/catch waterfall to figure out SQL version. e.g.

    try {
    $s = New-Object System.Management.ManagementScope(“\\.\root\microsoft\sqlserver\computermanagement11”, $cnnOpt)
    $s.connect()
    } catch {
    try {
    $s = New-Object System.Management.ManagementScope(“\\.\root\microsoft\sqlserver\computermanagement10”, $cnnOpt)
    $s.connect()
    } catch {
    $s = New-Object System.Management.ManagementScope(“\\.\root\microsoft\sqlserver\computermanagement”, $cnnOpt)
    $s.connect()
    }
    }

  2. I can confirm on a server with SQL2005 and SQL2008R2, the changes mentioned by Gavin Jones are required to get success, but the addition of the $s.Connect in each of the blocks (two try and one catch) does the trick quite nicely.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.