-
-
Save mfukar/ae6095488f461785d151 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Send-NetworkData { | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory)] | |
[string] | |
$Computer, | |
[Parameter(Mandatory)] | |
[ValidateRange(1, 65535)] | |
[Int16] | |
$Port, | |
[Parameter(ValueFromPipeline)] | |
[string[]] | |
$Data, | |
[System.Text.Encoding] | |
$Encoding = [System.Text.Encoding]::ASCII, | |
[TimeSpan] | |
$Timeout = [System.Threading.Timeout]::InfiniteTimeSpan | |
) | |
begin { | |
# establish the connection and a stream writer | |
$Client = New-Object -TypeName System.Net.Sockets.TcpClient | |
$Client.Connect($Computer, $Port) | |
$Stream = $Client.GetStream() | |
$Writer = New-Object -Type System.IO.StreamWriter -ArgumentList $Stream, $Encoding, $Client.SendBufferSize, $true | |
} | |
process { | |
# send all the input data | |
foreach ($Line in $Data) { | |
$Writer.WriteLine($Line) | |
} | |
} | |
end { | |
# flush and close the connection send | |
$Writer.Flush() | |
$Writer.Dispose() | |
$Client.Client.Shutdown('Send') | |
# read the response | |
$Stream.ReadTimeout = [System.Threading.Timeout]::Infinite | |
if ($Timeout -ne [System.Threading.Timeout]::InfiniteTimeSpan) { | |
$Stream.ReadTimeout = $Timeout.TotalMilliseconds | |
} | |
$Result = '' | |
$Buffer = New-Object -TypeName System.Byte[] -ArgumentList $Client.ReceiveBufferSize | |
do { | |
try { | |
$ByteCount = $Stream.Read($Buffer, 0, $Buffer.Length) | |
} catch [System.IO.IOException] { | |
$ByteCount = 0 | |
} | |
if ($ByteCount -gt 0) { | |
$Result += $Encoding.GetString($Buffer, 0, $ByteCount) | |
} | |
} while ($Stream.DataAvailable -or $Client.Client.Connected) | |
Write-Output $Result | |
# cleanup | |
$Stream.Dispose() | |
$Client.Dispose() | |
} | |
} | |
# pipe in a HTTP request: | |
'GET / HTTP/1.0', '' | Send-NetworkData -Computer www.powershellmagazine.com -Port 80 | |
# Use the Data parameter to do the same: | |
Send-NetworkData -Data 'GET / HTTP/1.0', '' -Computer www.powershellmagazine.com -Port 80 | |
# As before but only wait 2 seconds for a response: | |
Send-NetworkData -Data 'GET / HTTP/1.0', '' -Computer www.powershellmagazine.com -Port 80 -Timeout 0:00:02 | |
# Say hello to an SMTP server: | |
Send-NetworkData -Data "EHLO $Env:ComputerName", "QUIT" -Computer mail.example.com -Port 25 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment