通过函数创建的 PowerShell Socket 对象与通过 CLI 创建的 PowerShell Socket 对象不同
我有一个函数:
function QuerySMTPServer ([string]$strSMTPServerName) {
# open up a socket to the SMTP server
$socket = New-Object System.Net.Sockets.TCPClient
$socket.connect($strSMTPServerName, 25)
$socket # output for testing
# read response data (should be a line starting with 220; cf. RFC821)
$stream = $socket.getStream()
$stream # output for testing
...
如果我运行该函数并向其传递我们的(配置良好且正在运行的)SMTP 服务器地址,则输出的 $socket 和 $stream 对象告诉我套接字上有 0 个可用字节,并且没有可用数据在流上:
PS C:\Users\dan.maftei\Documents> QuerySMTPServer "internal-smtp.XYZ.com"
($socket)
Client : System.Net.Sockets.Socket
Available : 0
Connected : True
...
($stream)
DataAvailable : False
但是,确实有可用的数据,因为我可以创建一个字节数组并使用 $stream 对象的 read() 方法读取它。事实上,这正是我在函数的其余部分中所做的,并且一切正常,没有错误。 (!)
更奇怪的是,如果我手动输入 PowerShell 可执行文件中与我的函数调用的完全相同的 cmdlet,我的 $socket 突然(正确地)声称它有 79 字节的数据,而我的 $stream 同样说它有可用的数据:
PS C:\Users\dan.maftei\Documents> $socket = New-Object System.Net.Sockets.TCPClient
PS C:\Users\dan.maftei\Documents> $socket.connect("internal-smtp.XYZ.com", 25)
PS C:\Users\dan.maftei\Documents> $socket
Client : System.Net.Sockets.Socket
Available : 79
Connected : True
PS C:\Users\dan.maftei\Documents> $stream = $socket.getStream()
PS C:\Users\dan.maftei\Documents> $stream
...
DataAvailable : True
...
什么正在发生什么?如果我通过 CLI 而不是通过函数运行这几个 cmdlet,为什么会得到不同的对象?为什么我仍然可以从 $stream 中读取数据,即使根据函数的输出显然它没有数据?
I have a function:
function QuerySMTPServer ([string]$strSMTPServerName) {
# open up a socket to the SMTP server
$socket = New-Object System.Net.Sockets.TCPClient
$socket.connect($strSMTPServerName, 25)
$socket # output for testing
# read response data (should be a line starting with 220; cf. RFC821)
$stream = $socket.getStream()
$stream # output for testing
...
If I run the function and pass it our (well-configured and running) SMTP server address, the $socket and $stream objects that are outputted tell me I have 0 bytes available on the socket, and No data available on the stream:
PS C:\Users\dan.maftei\Documents> QuerySMTPServer "internal-smtp.XYZ.com"
($socket)
Client : System.Net.Sockets.Socket
Available : 0
Connected : True
...
($stream)
DataAvailable : False
However, there IS indeed data available, since I can create a byte array and read into it with the read() method off the $stream object. In fact this is precisely what I do in the remainder of the function, and it all works without error. (!)
Weirder yet, if I manually input into the PowerShell executable the exact same cmdlets my function calls, my $socket suddenly (and rightly) claims it has 79 bytes of data, and my $stream likewise says it has available data:
PS C:\Users\dan.maftei\Documents> $socket = New-Object System.Net.Sockets.TCPClient
PS C:\Users\dan.maftei\Documents> $socket.connect("internal-smtp.XYZ.com", 25)
PS C:\Users\dan.maftei\Documents> $socket
Client : System.Net.Sockets.Socket
Available : 79
Connected : True
PS C:\Users\dan.maftei\Documents> $stream = $socket.getStream()
PS C:\Users\dan.maftei\Documents> $stream
...
DataAvailable : True
...
What is going on?? Why do I get different objects if I run those very few cmdlets via the CLI as opposed to via the function? Why can I still read data off my $stream even though apparently it has none according to the output in the function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
cf:http://social. technet.microsoft.com/Forums/en/winserverpowershell/thread/d6420c96-3cdc-4af8-86d3-224b61a3b52f
第二篇文章,作者:jrich:PS 在脚本中很仓促,它不等待远程数据;让脚本休眠一秒钟(这可能有点过分)可以解决所有问题。
cf: http://social.technet.microsoft.com/Forums/en/winserverpowershell/thread/d6420c96-3cdc-4af8-86d3-224b61a3b52f
2nd post, by jrich: PS is hasty in scripts, it doesn't wait for remote data; making the script sleep for a second (which is probably overkill) fixes everything.