Advertisement
Guest User

Untitled

a guest
Jun 27th, 2014
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function Telnet ($RemoteHost, $Port, $message) {
  2.     try {  
  3.         ## Open the socket, and connect to the computer on the specified port
  4.         "Attempting to connect to $RemoteHost..."
  5.         $socket = new-object System.Net.Sockets.TcpClient($remoteHost,$Port)
  6.         if($socket -eq $null) {      
  7.            "Unable to connect to $remotehost" | Out-file $log
  8.            exit
  9.         }      
  10.  
  11.         $stream = $socket.GetStream()
  12.         $writer = new-object System.IO.StreamWriter($stream)
  13.         $buffer = new-object System.Byte[] 1024
  14.         $encoding = new-object System.Text.AsciiEncoding    
  15.  
  16.    
  17.         #Write the command to the remote host
  18.         $writer.WriteLine($message)
  19.         $writer.Flush()
  20.    
  21.         do {
  22.             $currentTime
  23.             #Allow data to buffer for a bit
  24.             start-sleep -m 500
  25.            
  26.             #reads the stream and outputs it to the log
  27.             $read = $stream.Read($buffer, 0, 1024)  
  28.             $result = ($encoding.GetString($buffer, 0, $read))
  29.             $result | Out-File $messageLog
  30.         }
  31.         until($currentTime -ge (Get-Date "10:00pm"))
  32.     }
  33.     finally {
  34.         ## Close the streams
  35.         $writer.Close()
  36.         $stream.Close()
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement