Answering the additional question in your comment, the Stopwatch Elapsed property performs the duration calculation intrinsically with slightly less code, which is why I used that instead of capturing the start/end time and calculating the difference. The duration is formatted as hours, minutes seconds, and fractional seconds but you can report milliseconds too if you prefer. Note that the milliseconds value is not rounded and you might observe minor timing anomalies with very small durations on multi-processor machines.
Below in another example that uses both methods, along with milliseconds.
try {
while($true) {
$timer = [System.Diagnostics.Stopwatch]::StartNew()
$startTime = [DateTime]::Now
$TimeOfQuery = Invoke-Sqlcmd -Query "SELECT GETDATE() AS TimeOfQuery" -ServerInstance "DBServer"
$timer.Stop()
$endTime = [DateTime]::Now
$timeDifference = $endTime.Subtract($startTime)
"startTime=$($startTime.ToString('yyyy-MM-dd HH:mm:ss.fff'))" + `
", timeOfQuery=$($TimeOfQuery[0].ToString('yyyy-MM-dd HH:mm:ss.fff'))" + `
", endTime=$($endTime.ToString('yyyy-MM-dd HH:mm:ss.fff'))" + `
", timeDifferenceDuration $($timeDifference.ToString())" + `
", timeDifferenceMilliseconds $($timeDifference.Milliseconds)" + `
", stopwatchDuration=$($timer.Elapsed.ToString())" + `
", stopwatchMilliseconds=$($timer.ElapsedMilliseconds)" `
| Out-File "C:\DBA\ping\timing.txt" -Append
Start-Sleep 1
[System.Data.SqlClient.SqlConnection]::ClearAllPools()
}
}
catch {
throw
}
Sample result:
startTime=2021-07-07 07:13:53.826, timeOfQuery=2021-07-07 07:13:53.830, endTime=2021-07-07 07:13:53.830, timeDifferenceDuration 00:00:00.0040058, timeDifferenceMilliseconds 4, stopwatchDuration=00:00:00.0041146, stopwatchMilliseconds=4
startTime=2021-07-07 07:13:54.858, timeOfQuery=2021-07-07 07:13:54.863, endTime=2021-07-07 07:13:54.865, timeDifferenceDuration 00:00:00.0070053, timeDifferenceMilliseconds 7, stopwatchDuration=00:00:00.0070475, stopwatchMilliseconds=7
startTime=2021-07-07 07:13:55.872, timeOfQuery=2021-07-07 07:13:55.877, endTime=2021-07-07 07:13:55.879, timeDifferenceDuration 00:00:00.0070002, timeDifferenceMilliseconds 7, stopwatchDuration=00:00:00.0072906, stopwatchMilliseconds=7
startTime=2021-07-07 07:13:56.890, timeOfQuery=2021-07-07 07:13:56.897, endTime=2021-07-07 07:13:56.897, timeDifferenceDuration 00:00:00.0069949, timeDifferenceMilliseconds 6, stopwatchDuration=00:00:00.0068910, stopwatchMilliseconds=6
startTime=2021-07-07 07:13:57.907, timeOfQuery=2021-07-07 07:13:57.913, endTime=2021-07-07 07:13:57.914, timeDifferenceDuration 00:00:00.0070062, timeDifferenceMilliseconds 7, stopwatchDuration=00:00:00.0071905, stopwatchMilliseconds=7
startTime=2021-07-07 07:13:58.922, timeOfQuery=2021-07-07 07:13:58.927, endTime=2021-07-07 07:13:58.930, timeDifferenceDuration 00:00:00.0080190, timeDifferenceMilliseconds 8, stopwatchDuration=00:00:00.0075408, stopwatchMilliseconds=7
startTime=2021-07-07 07:13:59.935, timeOfQuery=2021-07-07 07:13:59.940, endTime=2021-07-07 07:13:59.943, timeDifferenceDuration 00:00:00.0080083, timeDifferenceMilliseconds 8, stopwatchDuration=00:00:00.0076028, stopwatchMilliseconds=7