我写了一个小的Powershell脚本.
它使用.Net System.IO.StreamReader和System.IO.StreamWriter从/向TCP套接字读取和写入数据.
我想用它和发送/接收命令连接到套接字telnet样式.
我试图从System.IO.StreamReader读取带有ReadLine()方法的行,但是来自服务器的数据可能没有及时到达(?)或IDK.我没有使用异步命令.请帮助我!
这是我的脚本:
$FTPServer = "localhost"
$FTPPort = "21"
$tcpConnection = New-Object System.Net.Sockets.TcpClient($FTPServer, $FTPPort)
$tcpStream = $tcpConnection.GetStream()
$reader = New-Object System.IO.StreamReader($tcpStream)
$writer = New-Object System.IO.StreamWriter($tcpStream)
$writer.AutoFlush = $true
while ($tcpConnection.Connected)
{
while ($tcpStream.DataAvailable)
{
$reader.ReadLine()
}
if ($tcpConnection.Connected)
{
Write-Host -NoNewline "prompt> "
$command = Read-Host
if ($command -eq "escape")
{
break
}
$writer.WriteLine($command) | Out-Null
}
}
$reader.Close()
$writer.Close()
$tcpConnection.Close()
Run Code Online (Sandbox Code Playgroud)
这是输出:
PS C:\Windows\System32\WindowsPowerShell\v1.0> test.ps1
220 Microsoft FTP Service
prompt> user username
331 Password required
prompt> pass …Run Code Online (Sandbox Code Playgroud) 如何在PowerShell中向对象添加列?
例如,Get-Childitem返回一个对象,包括Mode,LastWriteTime,Length Name等.我想用一个额外的列来扩展这个对象,这是从LastWriteTime计算出来的.
这是原始的Get-Childitem输出:
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2012.12.15. 17:02 5390 Log_20121215.txt
-a--- 2013.01.02. 17:10 14014 Log_20130102.txt
-a--- 2013.01.07. 17:08 2200 Log_20130107.txt
Run Code Online (Sandbox Code Playgroud)
我想要这个输出:
Mode LastWriteTime Length Name ComputedColumn
---- ------------- ------ ---- --------------
-a--- 2012.12.15. 17:02 5390 Telenor_Log_20121215.txt 20131215
-a--- 2013.01.02. 17:10 14014 Telenor_Log_20130102.txt 20140102
-a--- 2013.01.07. 17:08 2200 Telenor_Log_20130107.txt 20140207
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助.