Skip to content

Instantly share code, notes, and snippets.

@KentNordstrom
Created March 16, 2017 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KentNordstrom/57e7102e85954ace8b178b7ac90d9e80 to your computer and use it in GitHub Desktop.
Save KentNordstrom/57e7102e85954ace8b178b7ac90d9e80 to your computer and use it in GitHub Desktop.
Add/Update your SQL Aliases using PowerShell
<#
.SYNOPSIS
This script adds/updates SQL Aliases on the local machine.
It can be implemented as function in your deployment scripts when installing FIM/MIM
or other application using SQL Aliases.
#>
PARAM([string]$AliasName,[string]$TargetServer,[string]$TargetPort="1433")
#Alias to be added to local machine
$dbAlias = "DBMSSOCN,$TargetServer,$TargetPort"
#Registry location for x86 and x64 alias
$x86="HKLM:\Software\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo"
$x64="HKLM:\Software\Microsoft\MSSQLServer\Client\ConnectTo"
#Create registry item if not present
If(!(Test-Path -Path $x86)){New-Item $x86}
If(!(Test-Path -Path $x64)){New-Item $x64}
#Add/Update 32-bit alias
if((Get-Item -Path $x86).Property -contains $AliasName)
{Set-ItemProperty -Path $x86 -Name $AliasName -Value $dbAlias}
else
{$void = New-ItemProperty -Path $x86 -Name $AliasName -PropertyType String -Value $dbAlias}
#Add/Update 64-bit alias
if((Get-Item -Path $x64).Property -contains $AliasName)
{Set-ItemProperty -Path $x64 -Name $AliasName -Value $dbAlias}
else
{$void = New-ItemProperty -Path $x64 -Name $AliasName -PropertyType String -Value $dbAlias}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment