Skip to content

Instantly share code, notes, and snippets.

@kewalaka
Last active May 29, 2021 22:27
Show Gist options
  • Select an option

  • Save kewalaka/5a12be5c8d67a358a2fa0a89fb2fa8a1 to your computer and use it in GitHub Desktop.

Select an option

Save kewalaka/5a12be5c8d67a358a2fa0a89fb2fa8a1 to your computer and use it in GitHub Desktop.
This will write out the SQL aliases on the local machine in a human readable form
$aliasRegistryPath = 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo'
(Get-Item $aliasRegistryPath).Property | ForEach-Object {
$aliasName = $_
$aliasTarget = Get-ItemPropertyValue $aliasRegistryPath -Name $aliasName
$aliasTargetType = switch ($aliasTarget.split(',')[0])
{
'DBNMPNTW' {'a named pipe'}
'DBMSSOCN' {'a tcp port'}
Default { ("an unknown target: '{0}'" -f $aliasTarget.split(',')[0]) }
}
$aliasTargetServer = $aliasTarget.split(',')[1]
$aliasTargetPort = $aliasTarget.split(',')[2]
if ($aliasTargetPort)
{
Write-Host "Alias: '$aliasName' points to '$aliasTargetServer' using $aliasTargetType via port '$aliasTargetPort'"
}
else
{
Write-Host "Alias: '$aliasName' points to '$aliasTargetServer' using $aliasTargetType"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment