Last active
February 3, 2022 07:23
-
-
Save notionparallax/b503c4a256363acc35e49527956cfc11 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$logpath = "c:\Servicing\weblog.txt" | |
$dtfmt = 'yyyy-mm-dd HH:mm:ss.ff' | |
$trigerTime = [System.DateTime]::UtcNow.tostring($dtfmt) # Could be $(Get-Date -AsUTC -Format $dtfmt ) in never versions of PS | |
function gather { | |
$adapters = Get-WmiObject -class win32_networkadapterconfiguration | where-object { $null -ne $_.DefaultIPGateway } | Select DNSDomain, IPAddress, MACAddress, Description, DefaultIPGateway | |
$result = New-Object -TypeName psobject -Property @{ | |
user = $env:username | |
triggeredat = $trigerTime | |
adapters = $adapters | |
} | |
return $result | |
#TODO: add BSSID with `netsh wlan show interfaces` to tell which WAP we're on. Need to test what happens on a wired connection | |
} | |
# $url = "https://postman-echo.com/post" | |
# $url = "http://localhost:7071/api/incomingPOST" | |
$url = "https://desk-demo.azurewebsites.net/api/incomingpost" | |
$mydetails = gather | |
Write-Output "Sending: $mydetails" | |
try { | |
$do = Invoke-RestMethod -Method 'Post' -Uri $url -Body ($mydetails | ConvertTo-Json -Compress) -ContentType "application/json" | |
$datastring = $($do | ConvertTo-Json -Compress) | |
Write-Output "datastring: $datastring" | |
$tolog = "$trigerTime - $datastring" | |
Write-Output $tolog | |
} | |
catch { | |
$details = $($mydetails | ConvertTo-Json -Compress) | |
# From https://stackoverflow.com/a/23815539/1835727 | |
$result = $_.Exception.Response.GetResponseStream() | |
$reader = New-Object System.IO.StreamReader($result) | |
$reader.BaseStream.Position = 0 | |
$reader.DiscardBufferedData() | |
$responseBody = $reader.ReadToEnd(); | |
$tolog = "$trigerTime - ERROR: $_ - $details - $responseBody |" | |
Write-Output "An error occurred: $tolog" | |
} | |
Add-content -Path $logpath -Value $tolog |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment