This is the script I have (I forget the user who helps/configured this)
$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
My issue is that when the it fails I am not getting an output log and I have no time stamps.
Is this something I can add and/or how do I go about it?
What log output are you expecting when it fails ?
when Test-NetConnection fails, probably the next lines in the while loop won’t be executing.
So its better to do error handling using try catch block. You can log the error message in catch block.
Select-Object doesn’t execute when Test-NetConnection fails. And if the machine is not reachable, it gives just a warning message as you get.
Above script will keep on looping until the system comes up, and returns the value $tnc. it will have the time when the ping succeeded. So you can add a check if it failed and save it in a variable, the return it along with $tnc using calculated property.
Couldn’t understand how you executed above code. You should be executing a .ps1 file. When PowerShell is something matters to you task in hand, you should definitely take some time and understand some basics.
Note: Please format code when you post next time. You can do the same for your previous reply.
$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
This is the code I run currently- it will only output -
WARNING: Name resolution of [Address] failed
WARNING: Name resolution of [Address] failed
I require the output to have a date/time stamp attached to it.
ie. WARNING: Name resolution of [Address] failed - 20/01/2020 - 07:00 (or something like this)
My issue is we send files from us to a 3rd party and they have reported that there are data dropouts that they say are coming from us. When we check there are no network outages.
I am trying to prove that if there is a drop in connection it’s not from us but from them.
There are drops on our side (or I wouldn’t get any drops) but they (as far as we can see) don’t happen at the same time that the files are being transferred.
Warning message which you see cannot be modified, its part of Test-NetConnection cmdlet. If required you put custom warning messages by using Write-Warning
PS C:\WINDOWS\system32> $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 @{
# Test-NetConnection here
if($tnc.TcpTestSuceeded -eq $flase){
$FailedDuring = $tnc.TimeStamp
}
$tnc | Select-Object -Property @{
Name = 'TimeStamp'
Expression = $FailedDuring
}, RemoteAddress, RemotePort, TcpTestSuceeded
Start-Sleep -Seconds 5
At line:8 char:3
+ if($tnc.TcpTestSuceeded -eq $flase){
+ ~
Missing '=' operator after key in hash literal.
At line:8 char:3
+ if($tnc.TcpTestSuceeded -eq $flase){
+ ~
The hash literal was incomplete.
At line:2 char:1
+ {
+ ~
Missing closing '}' in statement block or type definition.
At line:17 char:23
+ Start-Sleep -Seconds 5
+ ~
Missing while or until keyword in do loop.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingEqualsInHashLiteral
its an incomplete code and failed due to syntax error, please read the error message carefully and try to understand what it says. The fragment I shared is to be put inside the do-until loop of your code.
What I meant with # Test-NetConnection here is to put the Test-NetConnection line with Select-Object there. In this forum we try to avoid writing full code and make the user write, hence learn.