|
1 | | -$parameters = $args[0] |
2 | | - |
3 | | -$hostName = $parameters['hostName'] |
4 | | -$port = $parameters['port'] |
5 | | -$publicCertificateFile = $parameters['publicCertificateFile'] |
6 | | - |
7 | | -$Assem = @() |
8 | | - |
9 | | -$Source = @" |
10 | | -
|
| 1 | +$parameters = $args[0] |
| 2 | + |
| 3 | +$hostName = $parameters['hostName'] |
| 4 | +$port = $parameters['port'] |
| 5 | +$publicCertificateFile = $parameters['publicCertificateFile'] |
| 6 | + |
| 7 | +$Assem = @() |
| 8 | + |
| 9 | +$Source = @" |
| 10 | +
|
11 | 11 | using System; |
12 | 12 | using System.IO; |
13 | 13 |
|
@@ -586,10 +586,10 @@ namespace CL |
586 | 586 | } |
587 | 587 | } |
588 | 588 |
|
589 | | -} |
590 | | -
|
591 | | -"@ |
592 | | - |
| 589 | +} |
| 590 | +
|
| 591 | +"@ |
| 592 | + |
593 | 593 | function Using-Object |
594 | 594 | { |
595 | 595 | [CmdletBinding()] |
@@ -617,28 +617,37 @@ function Using-Object |
617 | 617 | $InputObject.Dispose() |
618 | 618 | } |
619 | 619 | } |
620 | | -} |
621 | | - |
622 | | -Add-Type -ReferencedAssemblies $Assem -TypeDefinition $Source -Language CSharp -ErrorAction SilentlyContinue |
623 | | - |
624 | | -$preLoginMessage = "AAAQAAYBABYAAQUAFwAk/wAAAAEAAACZ2dq6TF91Tqd3QhqNHRpv9/WwhLbfCUO6JBKeUqXXDAAAAAA=" |
625 | | -$tcpClient = New-Object System.Net.Sockets.TcpClient($hostName, $port) |
626 | | - |
627 | | - |
628 | | -Using-Object($stream = New-Object CL.TDSStream($tcpClient.GetStream(), [TimeSpan]::FromSeconds(30), 4096)) { |
629 | | - $stream.CurrentOutboundMessageType = [CL.TDSMessageType]::PreLogin |
| 620 | +} |
| 621 | + |
| 622 | +Add-Type -ReferencedAssemblies $Assem -TypeDefinition $Source -Language CSharp -ErrorAction SilentlyContinue |
| 623 | + |
| 624 | +$preLoginMessage = "AAAQAAYBABYAAQUAFwAk/wAAAAEAAACZ2dq6TF91Tqd3QhqNHRpv9/WwhLbfCUO6JBKeUqXXDAAAAAA=" |
| 625 | +$stream = $null |
| 626 | + |
| 627 | +if($hostName -like "\\.\pipe\*") { |
| 628 | + $pipeName = $hostName.Substring(9) |
| 629 | + $stream = New-Object System.IO.Pipes.NamedPipeClientStream(".", $pipeName, [System.IO.Pipes.PipeDirection]::InOut, [System.IO.Pipes.PipeOptions]::None, [System.Security.Principal.TokenImpersonationLevel]::Impersonation) |
| 630 | + $stream.Connect() |
| 631 | +} |
| 632 | +else { |
| 633 | + $tcpClient = New-Object System.Net.Sockets.TcpClient($hostName, $port) |
| 634 | + $stream = $tcpClient.GetStream() |
| 635 | +} |
| 636 | + |
| 637 | +Using-Object($stream = New-Object CL.TDSStream($stream, [TimeSpan]::FromSeconds(30), 4096)) { |
| 638 | + $stream.CurrentOutboundMessageType = [CL.TDSMessageType]::PreLogin |
630 | 639 | $writeBuffer = [Convert]::FromBase64String($preLoginMessage) |
631 | 640 | $stream.Write($writeBuffer, 0, $writeBuffer.Length) |
632 | 641 | $readBuffer = New-Object System.Byte[] 4096 |
633 | 642 | $bytesRead = $stream.Read($readBuffer, 0, $readBuffer.Length) |
634 | | - $sslStream = New-Object System.Net.Security.SslStream($stream, $true) |
| 643 | + $sslStream = New-Object System.Net.Security.SslStream($stream, $true, {$true}) |
635 | 644 | $sslStream.AuthenticateAsClient($hostName) |
636 | 645 | $certificate = $sslStream.RemoteCertificate |
637 | | - [System.IO.File]::WriteAllBytes($publicCertificateFile,$certificate.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Cert)) |
638 | | -} |
639 | | - |
640 | | - |
641 | | - |
642 | | - |
643 | | - |
644 | | - |
| 646 | + [System.IO.File]::WriteAllBytes($publicCertificateFile,$certificate.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Cert)) |
| 647 | +} |
| 648 | + |
| 649 | + |
| 650 | + |
| 651 | + |
| 652 | + |
| 653 | + |
0 commit comments