Forget the CLR stuff... for TCPConnections it is more likely a wild goose chase... I just like playing around with it. However, since Netstat gives the same information then how best to parse Netstat;
The script below [using CLR=>PS] does the basic job very efficiently. Headers would need renaming and ports extracted from Local and remote IP's - but fairly trivial.
;Winbatch 2020B - Parsing Netstat
;============================================================================================
IntControl(73,1,0,0,0)
gosub udfs
cFile = "C:\temp\netstat.txt"
If FileExist(cFile) Then FileDelete(cFile)
cScript= $"
$file = "|file|"
$a = netstat -ano
$a[3..$a.count] | ConvertFrom-String | select p2,p3,p4,p5,p6 | where p5 -eq 'established' | Export-Csv -delimiter "`t" -Path $file -NoTypeInformation
$"
cScript = StrReplace(cScript,"|file|",cFile)
BoxOpen("Please Wait","Parsing Netstat Output")
oNoGo = ObjectType("BOOL",@FALSE)
ObjectClrOption("useany", "System.Management.Automation")
objAutoPs = ObjectClrNew("System.Management.Automation.PowerShell")
oPshell = objAutoPs.Create()
oScope = ObjectType("BOOL",@TRUE)
oPshell.AddScript(cScript,oScope)
objAsync = oPshell.BeginInvoke()
ctimeout=100
tries=0
While objAsync.IsCompleted == oNoGo
TimeDelay(10)
tries=tries+1
If tries>ctimeout Then Break
EndWhile
oPShell.EndInvoke(objAsync)
Boxtext("Script Finished... Starting Excel")
If FileExist(cFile)
cText = FileGet(cFile)
cText = StrReplace(cText,'"','')
cText = StrReplace(cText,',',@TAB)
ClipPut(cText)
oXL = CreateObject("Excel.Application")
oXL.Visible = @TRUE ; change this to @FALSE to run hidden
oXL.ScreenUpdating = @TRUE ; if running hidden, change this to @FALSE
oXL.UserControl = @TRUE
oXL.DisplayAlerts = @FALSE
oXL.WorkBooks.Add()
BoxShut()
oWS = oXL.ActiveWorkBook.Worksheets(1)
oWS.Activate()
oWS.Name = "Netstat"
oWS.Cells(1,1).Select()
oWS.Paste()
oWS.UsedRange.Select()
oXL.Selection.Font.Name = 'Tahoma'
oXL.Selection.Font.Size = 9
oXL.Selection.Font.Bold = @True
oWS.UsedRange.Columns.Autofit()
oWS.ListObjects.Add(:1,oWS.UsedRange, , 1).Name ="Table1"
oWS.Range("Table1[#All]").Select()
oWS.ListObjects("Table1").TableStyle = "TableStyleLight15"
oXL.ActiveWindow.DisplayGridlines = @False
oWS.Cells(1,1).Select()
oWS=0
oXL=0
Pause("Data Loaded Into Excel","Save or Close Workbook")
Else
Pause("Script Has Failed","Cannot Create Excel Output")
Endif
Exit
;======================================================================================================
:WBERRORHANDLER
Terminate(@TRUE,"Error Encountered",err())
;======================================================================================================
:udfs
#DefineSubRoutine err()
If key<>0 Then RegCloseKey(key)
wberroradditionalinfo = wberrorarray[6]
lasterr = wberrorarray[0]
handlerline = wberrorarray[1]
textstring = wberrorarray[5]
linenumber = wberrorarray[8]
errmsg = "Error: ":lasterr:@LF:textstring:@LF:"Line (":linenumber:")":@LF:wberroradditionalinfo
Return(errmsg)
#EndSubRoutine
Return
;===========================================================================================