functions/New-DbaClientAlias.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
Function New-DbaClientAlias {
<#
 .SYNOPSIS
 Creates/updates a sql alias for the specified server - mimics cliconfg.exe
 
 .DESCRIPTION
 Creates/updates a SQL Server alias by altering HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client
 
 .PARAMETER ComputerName
 The target computer where the alias will be created
 
 .PARAMETER Credential
 Allows you to login to remote computers using alternative credentials
 
 .PARAMETER ServerName
 The target SQL Server
   
 .PARAMETER Alias
 The alias to be created
   
 .PARAMETER Protocol
 The protocol for the connection, either TCPIP or NetBIOS. Defaults to TCPIP.
 
 .PARAMETER Silent
 Use this switch to disable any kind of verbose messages
 
 .NOTES
 Tags: Alias
 
 Website: https://dbatools.io
 Copyright: (C) Chrissy LeMaire, clemaire@gmail.com
 License: GNU GPL v3 https://opensource.org/licenses/GPL-3.0
 
 .LINK
 https://dbatools.io/New-DbaClientAlias
 
 .EXAMPLE
 New-DbaClientAlias -ServerName sqlcluster\sharepoint -Alias sp
 Creates a new TCP alias on the local workstation called sp, which points sqlcluster\sharepoint
   
 .EXAMPLE
 New-DbaClientAlias -ServerName sqlcluster\sharepoint -Alias sp -Protocol NamedPipes
 Creates a new NamedPipes alias on the local workstation called sp, which points sqlcluster\sharepoint
   
#>

    [CmdletBinding()]
    Param (
        [DbaInstanceParameter[]]$ComputerName = $env:COMPUTERNAME,
        [PSCredential]$Credential,
        [parameter(Mandatory, ValueFromPipeline)]
        [DbaInstanceParameter[]]$ServerName,
        [parameter(Mandatory)]
        [string]$Alias,
        [ValidateSet("TCPIP", "NamedPipes")]
        [string]$Protocol = "TCPIP",
        [switch]$Silent
    )
    
    begin {
        # This is a script block so cannot use messaging system
        $scriptblock = {
            $basekeys = "HKLM:\SOFTWARE\WOW6432Node\Microsoft\MSSQLServer", "HKLM:\SOFTWARE\Microsoft\MSSQLServer"
            $ServerName = $args[0]
            $Alias = $args[1]
            $serverstring = $args[2]
            
            if ($env:PROCESSOR_ARCHITECTURE -like "*64*") { $64bit = $true }
            
            foreach ($basekey in $basekeys) {
                if ($64bit -ne $true -and $basekey -like "*WOW64*") { continue }
                
                if ((Test-Path $basekey) -eq $false) {
                    throw "Base key ($basekey) does not exist. Quitting."
                }
                
                $client = "$basekey\Client"
                
                if ((Test-Path $client) -eq $false) {
                    # "Creating $client key"
                    $null = New-Item -Path $client -Force
                }
                
                $connect = "$client\ConnectTo"
                
                if ((Test-Path $connect) -eq $false) {
                    # "Creating $connect key"
                    $null = New-Item -Path $connect -Force
                }
                
                if ($basekey -like "*WOW64*") {
                    $architecture = "32-bit"
                }
                else {
                    $architecture = "64-bit"
                }
                
                # Write-Verbose "Creating/updating alias for $ComputerName for $architecture"
                $null = New-ItemProperty -Path $connect -Name $Alias -Value $serverstring -PropertyType String -Force
            }
        }
    }
    
    process {
        if ($protocol -eq "TCPIP") {
            $serverstring = "DBMSSOCN,$ServerName"
        }
        else {
            $serverstring = "DBNMPNTW,\\$ServerName\pipe\sql\query"
        }
        
        foreach ($computer in $ComputerName.ComputerName) {
            
            $null = Test-ElevationRequirement -ComputerName $computer -Continue
            
            if ($PScmdlet.ShouldProcess($computer, "Adding $alias")) {
                try {
                    Invoke-Command2 -ComputerName $computer -Credential $Credential -ScriptBlock $scriptblock -ErrorAction Stop -ArgumentList $ServerName, $Alias, $serverstring
                }
                catch {
                    Stop-Function -Message "Failure" -ErrorRecord $_ -Target $computer -Continue
                }
            }
        }
        
        Get-DbaClientAlias -ComputerName $computer -Credential $Credential | Where-Object AliasName -eq $Alias
    }
}