mirror of
https://github.com/Microsoft/sql-server-samples.git
synced 2025-12-08 14:58:54 +00:00
Merge pull request #1076 from srdan-bozovic-msft/patch-2
Add support for named pipes + invalid certs
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
$parameters = $args[0]
|
||||
|
||||
$hostName = $parameters['hostName']
|
||||
$port = $parameters['port']
|
||||
$publicCertificateFile = $parameters['publicCertificateFile']
|
||||
|
||||
$Assem = @()
|
||||
|
||||
$Source = @"
|
||||
|
||||
$parameters = $args[0]
|
||||
|
||||
$hostName = $parameters['hostName']
|
||||
$port = $parameters['port']
|
||||
$publicCertificateFile = $parameters['publicCertificateFile']
|
||||
|
||||
$Assem = @()
|
||||
|
||||
$Source = @"
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
@@ -586,10 +586,10 @@ namespace CL
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
"@
|
||||
|
||||
}
|
||||
|
||||
"@
|
||||
|
||||
function Using-Object
|
||||
{
|
||||
[CmdletBinding()]
|
||||
@@ -617,28 +617,37 @@ function Using-Object
|
||||
$InputObject.Dispose()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Add-Type -ReferencedAssemblies $Assem -TypeDefinition $Source -Language CSharp -ErrorAction SilentlyContinue
|
||||
|
||||
$preLoginMessage = "AAAQAAYBABYAAQUAFwAk/wAAAAEAAACZ2dq6TF91Tqd3QhqNHRpv9/WwhLbfCUO6JBKeUqXXDAAAAAA="
|
||||
$tcpClient = New-Object System.Net.Sockets.TcpClient($hostName, $port)
|
||||
|
||||
|
||||
Using-Object($stream = New-Object CL.TDSStream($tcpClient.GetStream(), [TimeSpan]::FromSeconds(30), 4096)) {
|
||||
$stream.CurrentOutboundMessageType = [CL.TDSMessageType]::PreLogin
|
||||
}
|
||||
|
||||
Add-Type -ReferencedAssemblies $Assem -TypeDefinition $Source -Language CSharp -ErrorAction SilentlyContinue
|
||||
|
||||
$preLoginMessage = "AAAQAAYBABYAAQUAFwAk/wAAAAEAAACZ2dq6TF91Tqd3QhqNHRpv9/WwhLbfCUO6JBKeUqXXDAAAAAA="
|
||||
$stream = $null
|
||||
|
||||
if($hostName -like "\\.\pipe\*") {
|
||||
$pipeName = $hostName.Substring(9)
|
||||
$stream = New-Object System.IO.Pipes.NamedPipeClientStream(".", $pipeName, [System.IO.Pipes.PipeDirection]::InOut, [System.IO.Pipes.PipeOptions]::None, [System.Security.Principal.TokenImpersonationLevel]::Impersonation)
|
||||
$stream.Connect()
|
||||
}
|
||||
else {
|
||||
$tcpClient = New-Object System.Net.Sockets.TcpClient($hostName, $port)
|
||||
$stream = $tcpClient.GetStream()
|
||||
}
|
||||
|
||||
Using-Object($stream = New-Object CL.TDSStream($stream, [TimeSpan]::FromSeconds(30), 4096)) {
|
||||
$stream.CurrentOutboundMessageType = [CL.TDSMessageType]::PreLogin
|
||||
$writeBuffer = [Convert]::FromBase64String($preLoginMessage)
|
||||
$stream.Write($writeBuffer, 0, $writeBuffer.Length)
|
||||
$readBuffer = New-Object System.Byte[] 4096
|
||||
$bytesRead = $stream.Read($readBuffer, 0, $readBuffer.Length)
|
||||
$sslStream = New-Object System.Net.Security.SslStream($stream, $true)
|
||||
$sslStream = New-Object System.Net.Security.SslStream($stream, $true, {$true})
|
||||
$sslStream.AuthenticateAsClient($hostName)
|
||||
$certificate = $sslStream.RemoteCertificate
|
||||
[System.IO.File]::WriteAllBytes($publicCertificateFile,$certificate.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Cert))
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
[System.IO.File]::WriteAllBytes($publicCertificateFile,$certificate.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Cert))
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user