Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/powershell
- function Get-Netstat{
- $netstatCheck = Test-Path "C:\WINDOWS\System32\Netstat.exe"
- If($netstatCheck -eq $true){
- $netstat = netstat -n
- $netstat = $netstat[4..$netstat.Count]
- $outputArray = @()
- Foreach ($line in $netstat){
- $line = $line -split ' ' | Where-Object {$_ -ne ""}
- $outputArray += New-Object psobject -Property @{
- Protocol = $line[0]
- LocalAddress = $line[1]
- ForeignAddress = $line[2]
- State = $line[3]
- }
- }
- }
- Else{ # We assume Linux Netstat if Else, probably won't work on macOS.
- $netstat = netstat -nt
- $netstat = $netstat[3..$netstat.Count]
- $outputArray = @()
- Foreach ($line in $netstat){
- $line = $line -split ' ' | Where-Object {$_ -ne ""}
- $outputArray += New-Object psobject -Property @{
- Protocol = $line[0]
- ReceiveQ = $line[1]
- SendQ = $line[2]
- LocalAddress = $line[3]
- ForeignAddress = $line[4]
- State = $line[5]
- }
- }
- }
- $outputArray #| ft
- }
- Get-Netstat | ft
Advertisement
Add Comment
Please, Sign In to add comment