@@ -16,6 +16,7 @@ function Get-DecryptedObject {
1616 - Export-DbaLinkedServer
1717
1818 This function is heavily based on Antti Rantasaari's script at http://goo.gl/wpqSib
19+ (currently pointing to: https://www.netspi.com/decrypting-mssql-database-link-server-passwords/)
1920 Antti Rantasaari 2014, NetSPI
2021 License: BSD 3-Clause http://opensource.org/licenses/BSD-3-Clause
2122
@@ -132,73 +133,7 @@ function Get-DecryptedObject {
132133 }
133134
134135 Write-Message - Level Verbose - Message " Query password information from the Db."
135-
136- if ($server.Name -like ' ADMIN:*' ) {
137- Write-Message - Level Verbose - Message " We already have a dac, so we use it."
138- $results = $server.Query ($sql )
139- } else {
140- $instance = $server.InstanceName
141- if (-not $server.IsClustered ) {
142- $connString = " Server=ADMIN:127.0.0.1\$instance ;Trusted_Connection=True;Pooling=false"
143- } else {
144- $dacEnabled = $server.Configuration.RemoteDacConnectionsEnabled.ConfigValue
145-
146- if ($dacEnabled -eq $false ) {
147- If ($Pscmdlet.ShouldProcess ($server.Name , " Enabling remote DAC on clustered instance." )) {
148- try {
149- Write-Message - Level Verbose - Message " DAC must be enabled for clusters, even when accessed from active node. Enabling."
150- $server.Configuration.RemoteDacConnectionsEnabled.ConfigValue = $true
151- $server.Configuration.Alter ()
152- } catch {
153- Stop-Function - Message " Failure enabling remote DAC on clustered instance $sourceName " - Target $sourceName - ErrorRecord $_
154- return
155- }
156- }
157- }
158-
159- $connString = " Server=ADMIN:$sourceName ;Trusted_Connection=True;Pooling=false;"
160- }
161-
162- try {
163- $results = Invoke-Command2 - Raw - Credential $Credential - ComputerName $fullComputerName - ArgumentList $connString , $sql {
164- try {
165- $connString = $args [0 ]
166- $sql = $args [1 ]
167- $conn = New-Object System.Data.SqlClient.SQLConnection($connString )
168- $cmd = New-Object System.Data.SqlClient.SqlCommand($sql , $conn )
169- $dt = New-Object System.Data.DataTable
170- $conn.open ()
171- $dt.Load ($cmd.ExecuteReader ())
172- $conn.Close ()
173- $conn.Dispose ()
174- return $dt
175- } catch {
176- $exception = $_
177- try {
178- $conn.Close ()
179- $conn.Dispose ()
180- } catch {
181- $null = 1
182- }
183- throw $exception
184- }
185- }
186- } catch {
187- Stop-Function - Message " Can't establish local DAC connection on $sourceName ." - Target $server - ErrorRecord $_
188- }
189-
190- if ($server.IsClustered -and $dacEnabled -eq $false ) {
191- If ($Pscmdlet.ShouldProcess ($server.Name , " Disabling remote DAC on clustered instance." )) {
192- try {
193- Write-Message - Level Verbose - Message " Setting remote DAC config back to 0."
194- $server.Configuration.RemoteDacConnectionsEnabled.ConfigValue = $false
195- $server.Configuration.Alter ()
196- } catch {
197- Stop-Function - Message " Failure disabling remote DAC on clustered instance $sourceName " - Target $server - ErrorRecord $_
198- }
199- }
200- }
201- }
136+ $results = $server.Query ($sql )
202137
203138 Write-Message - Level Verbose - Message " Go through each row in results"
204139 foreach ($result in $results ) {
@@ -215,8 +150,6 @@ function Get-DecryptedObject {
215150 $encode = New-Object System.Text.UnicodeEncoding
216151
217152 # Print results - removing the weird padding (8 bytes in the front, some bytes at the end)...
218- # Might cause problems but so far seems to work.. may be dependant on SQL server version...
219- # If problems arise remove the next three lines..
220153 $i = 8 ; foreach ($b in $decrypted ) { if ($decrypted [$i ] -ne 0 -and $decrypted [$i + 1 ] -ne 0 -or $i -eq $decrypted.Length ) { $i -= 1 ; break ; }; $i += 1 ; }
221154 $decrypted = $decrypted [8 .. $i ]
222155
@@ -229,4 +162,4 @@ function Get-DecryptedObject {
229162 ProviderName = $result.ProviderName
230163 }
231164 }
232- }
165+ }
0 commit comments