PowerShell循环语句没有执行


PowerShell交流中心分类: Powershell基础PowerShell循环语句没有执行
0
Hwctak asked 7 年 ago

while ($true) {
do {
try {
Write-Host “Trying to reach “$address”:”$port
$client = New-Object System.Net.Sockets.TcpClient($address, $port)
$stream = $client.GetStream()
$writer = New-Object System.IO.StreamWriter($stream)
$reader = New-Object System.IO.StreamReader($stream)
break
}
catch {
# Try to connect server serval 10s
Start-Sleep -s 10
}
} while ($true)

每隔10秒执行一次,但是为什么没有执行。

1 Answers
1
Best Answer
Mooser Lee 管理员 answered 7 年 ago
$address = "www.pstips.net"
$port = 443
while($true)
{
    try {
    Write-Host “Trying to reach “$address”:”$port
    $client = New-Object System.Net.Sockets.TcpClient($address, $port) 
    $stream = $client.GetStream()
    $writer = New-Object System.IO.StreamWriter($stream)
    $write.
    $reader = New-Object System.IO.StreamReader($stream)
    Start-Sleep -s 10
    }
    catch {
    Write-Host "failed on tcp connect."
    
    }

}
Hwctak replied 7 年 ago

老铁,确实有效果!感谢你!请问是否可以留下你的联系方式?