PowerShell Check

Server:

$port = 9876
$separator = ";"


$endpoint = new-object System.Net.IPEndPoint([system.net.ipaddress]::any, $port)
$listener = new-object System.Net.Sockets.TcpListener $endpoint
$listener.start()

$recvbuf = new-object System.Byte[] 1024

$encoding = new-object System.Text.AsciiEncoding

write-host "Listening..." -ForegroundColor Green
$socket = $listener.AcceptTcpClient()
    
while($true)
{
     $stream = $socket.GetStream()
     $command = $null
     if($stream.DataAvailable)
     {
          $read = $stream.Read($recvbuf, 0, $recvbuf.Length)
          if($null -ne $read)
          {
              $commands = $encoding.GetString($recvbuf, 0, $read)
          #     write-host ($encoding.GetString($recvbuf, 0, $read))
             
              $cmds = $commands.Split($separator)
              foreach($cmd in $cmds)
              {
               write-host $cmd -ForegroundColor Yellow
              }
            }  
     }
}
$socket
$listener.stop()

Start-Sleep -s 10

Exit

Client:

# $ip = "127.0.0.1"
$ip = "10.111.18.165"

$port = 9876
$separator = ";"

 

$socket = new-object System.Net.Sockets.TcpClient($ip, $port)
if($null -eq $socket)
{
   return;
}

$stream = $socket.GetStream()
$sendBuf = new-object System.Byte[] 1024

$encoding = new-object System.Text.AsciiEncoding

$command = "TestData"

$sendBuf = $encoding.GetBytes($command)
$stream.Write($sendBuf, 0, $sendBuf.Length)
 

$stream.Close()
$socket.Close()

Start-Sleep -s 10

Exit

你可能感兴趣的:(PowerShell Check)