我想運行一個 powershell 腳本,我可以在其中回圈一個命令,直到它回傳 false。
tnc -ComputerName [Address] -port [port]
Start-Sleep -s 5
該腳本每 5 秒運行一次
ComputerName : [Address]
RemoteAddress : IP
RemotePort : Port
InterfaceAlias : WiFi
SourceAddress : [Address]
TcpTestSucceeded : True
我需要它每 5 秒運行一次,但只顯示為 false,然后提取到檔案并繼續運行。
我不知道這是否可能,但有什么幫助
uj5u.com熱心網友回復:
邏輯看起來像這樣,請注意,這有很多變化。由你決定。
DateTime.ToString('u')將具有以下格式TimeStamp:
// u Format Specifier de-DE Culture 2008-10-31 17:04:32Z
// u Format Specifier en-US Culture 2008-10-31 17:04:32Z
// u Format Specifier es-ES Culture 2008-10-31 17:04:32Z
// u Format Specifier fr-FR Culture 2008-10-31 17:04:32Z
來源:https : //docs.microsoft.com/en-us/dotnet/api/system.datetime.tostring?view=net-6.0
$log = do # Do this
{
# Store the result of TNC in this variable to be tested until(...) is False
$tnc = Test-NetConnection -ComputerName [Address] -Port [port] |
Select-Object @{
Name = 'TimeStamp'
Expression = { [datetime]::Now.ToString('u') }
}, RemoteAddress, RemotePort, TcpTestSuceeded
# Return this variable so it's captured in $log Variable to export the log later
$tnc
Start-Sleep -Seconds 5
}
until ($tnc.TcpTestSuceeded) # Until this property is $False
$log | Export-Csv path/to/logfile.csv -NoTypeInformation
uj5u.com熱心網友回復:
像這樣的東西。但是斷開連接的超時時間長得離譜,大約 30 秒。
while ( -not (tnc yahoo.com -port 81) ) { sleep 5 }
WARNING: TCP connect to (2001:4998:24:120d::1:1 : 81) failed
WARNING: TCP connect to (2001:4998:124:1507::f001 : 81) failed
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/366180.html
