Author Posts

November 1, 2017 at 5:18 am

Dear Community

I have requirement to find out which database servers are my servers connected to . I do not have access to query SQL PS commands hence i come up with below script to query from server that i connected. The script is bit ok but i wanted to check with you all 🙂 to see if there is any other better way i can do this .

$cmd = netstat -ano | findstr ":1433"
$cmd = $cmd.split(10)
$result = $cmd[10]
$result = $result.replace(":1433", "")
$DBServer = nslookup $result 
$DBServer = $DBServer | select-string "Name"
write-host "Server connected to $DBServer"

November 1, 2017 at 2:50 pm

Not really. If it works, it's great, right? 🙂

November 1, 2017 at 4:13 pm

Actually there is a powershell version of netstat:

Get-NetTCPConnection | where remoteport -eq 1433 | select -expand remoteaddress -first 1 | Resolve-DnsName