forked from caroysMSFT/MSFTProjects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TraceHost.ps1
59 lines (44 loc) · 1.68 KB
/
TraceHost.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
param ([int] $pollinterval, [string]$uri)
$headers = ("TimeStamp","HopNum","Source","Destination","HopHost","TimeTaken")
$logname = "tracehost"
function write-csv($logentry)
{
#Create *.csv if it doesn't exist, with header values.
$logfilepath = "$PSScriptRoot\$logname-$(get-date -format `"MM-dd-yyyy`").csv"
if((test-path -Path $logfilepath) -eq $false)
{
foreach($header in $headers)
{
$headerrow += "$header,"
}
$headerrow = $headerrow.ToString().TrimEnd(",")
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False
[System.IO.File]::WriteAllLines($logfilepath, $headerrow, $Utf8NoBomEncoding)
}
$logentry >> $logfilepath
}
function get-egressip()
{
$response = Invoke-WebRequest -Uri https://whatsmyip.com -UseBasicParsing
$htmlobj = New-Object -Com "HTMLFile"
$htmlobj.IHTMLDocument2_write($response.Content)
$egressIP = $htmlobj.getElementById("shownIpv4").IHTMLElement_innerText
return $egressIP
}
$hostname = $uri.split("/")[2]
while($true)
{
$ttl = 1
$CurrentMinuteTime = (get-date).ToString("o")
foreach($node in (Test-NetConnection $hostname -traceroute).Traceroute)
{
$result = (Test-NetConnection $node -Hops $ttl)
$record= "$CurrentMinuteTime,$ttl,$(get-egressip),$hostname,$node,$($result.PingReplyDetails.RoundtripTime)"
write-csv $record
$ttl++
}
#TODO: Do a GET request
$requesttime = (Measure-Command -Expression {$response = Invoke-WebRequest -UseBasicParsing -Uri $uri}).Milliseconds
write-csv "$CurrentMinuteTime,,$(get-egressip),$uri,$hostname,$requesttime"
start-sleep -Seconds $pollinterval
}