-
Notifications
You must be signed in to change notification settings - Fork 0
/
CheckNetConnections.ps1
42 lines (38 loc) · 1.33 KB
/
CheckNetConnections.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
function Main()
{
$CurrentScriptVersion = "1.0"
Write-Host "================== Checking Network Connections =================="
Write-Host
$server = get-content env:computername
$logPath = "d:\ServiceLogs\ServerNetworkConnections"
if ( Test-Path $logPath -PathType Container )
{
"Log path already exist"
}
else
{
New-Item -Path $logPath -ItemType directory
"Log path folder created"
}
try {
$netobj = get-nettcpconnection | Where {$_.State -eq 'Established' -or $_.State -eq 'TimeWait'}
$netConnCount = $netobj.count
$dateobj = get-date
$date = $dateobj.ToString("yyyy-MM-ddTHH:mm:ss.fff")
$body = @{
applicationName = "ServerNetworkConnections"
date = "$date"
server = "$server"
numConnections = $netConnCount
message = "$server has $netConnCount connections in an Established or Time Wait state"
}
$logFileName = 'netConn-' + $dateobj.ToString("yyyy-MM-dd") + '.txt'
$body | ConvertTo-Json -compress | Out-File -append "$logPath\$logFileName" -Encoding Ascii
}
catch [System.Exception] {
Write-Output $_
Exit 1
}
Write-Host "================== Network Connection : END =================="
}
Main