PowerShell: test connection in as few characters as possible

7

I need this down to as few characters as possible. I would use Test-NetConnection but I need PSv2 compatability. Any help would be appreciated.

$socket = new-object Net.Sockets.TcpClient
$socket.Connect("192.168.5.5",445)
$socket.Connected

As a note I have tried this but it doesn't work

(new-object Net.Sockets.TcpClient).Connect("192.168.5.5",445).Connected

Michael Timmerman

Posted 2017-04-03T17:10:15.230

Reputation: 73

1My code does work. I am proposing the challenge of "make it as few characters as possible". I fail to see how this is considered "off topic" when one of the topics is "programming challenges". Please explain.... – Michael Timmerman – 2017-04-03T17:20:19.027

5

@apsillers This is a perfectly on-topic question; we've accepted many "how can I make my code shorter" questions in the past: 1, 2, 3, 4, 5, 6, 7, 8, etc.

– ETHproductions – 2017-04-03T17:27:54.923

This would be better suited for The Nineteenth Byte or something. IMO, it doesn't warrant its own question on the main site.

– Nick Clifford – 2017-04-03T17:28:25.733

5

@NickClifford This type of question, while uncommon, is absolutely on topic.

– Martin Ender – 2017-04-03T17:30:01.987

@NickClifford Viewed as a narrowly-applied [tips] question, rather than as a challenge, this is indeed on topic. (I say this as someone who originally voted to close the question.) – apsillers – 2017-04-03T17:33:31.837

@apsillers Ah, I see. – Nick Clifford – 2017-04-03T17:34:36.133

1

OP, just to explain the contention here, the site accepts two types of posts: "challenges" to achieve some general goal ("Given a number as input, compute the lowest prime number higher than the input number" as one simple example) and "tips" which ask for help golfing in a particular language. "Tips" posts are comparatively rare, and are largely language-specific compendiums of knowledge (e.g., Tips for golfing in Windows PowerShell. However, tips-posts asking to golf a particular problem are allowed, but fairly uncommon, hence the confusion.

– apsillers – 2017-04-03T17:48:08.940

Answers

2

AFAIK, there's no default alias for New-Object, and no type accelerator for Net.Sockets.TcpClient, so they can't be any shorter. You can merge the constructor and connect code into one line:

(New-Object Net.Sockets.TcpClient -A 192.168.5.5,445)

But if it can't connect, it now throws an exception, which you can't silence with -ErrorAction. So handling that ends up being 70 characters, only 8 shorter than your original after shrinking the variable name:

!!$(try{(new-object net.sockets.tcpclient -A 192.168.5.5,445)}catch{})

!! forcing the result to a bool.

TessellatingHeckler

Posted 2017-04-03T17:10:15.230

Reputation: 2 412

My final solution ended up taking parts from both of your answers: While(!!(New-Object net.sockets.tcpclient (192.168.5.5,445))){} Thank you both for your input! I gave the answer to TesselatingHeckler's because I was impressed at the way he returned a bool value in only two characters. Quite impressive! – Michael Timmerman – 2017-04-04T03:44:19.117

6

PowerShell v2+, 63 bytes

You can use a different constructor to create the object and connect in one go.

(new-object Net.Sockets.TcpClient('192.168.5.5',445)).Connected

I've verified this works in v2 on my Windows 8.1 machine. That constructor is supported by .NET 2.0, so this should be v2 compatible.

AdmBorkBork

Posted 2017-04-03T17:10:15.230

Reputation: 41 581

Can replace '192.168.5.5' with 3232236805 – ceilingcat – 2017-04-04T06:36:24.387

@ceilingcat Can you explain that? – Michael Timmerman – 2017-05-13T03:22:02.913

@MichaelTimmerman The integer 3232236805 is equal to 192256^3 + 168256^2 + 5*256 + 5, the native format that the TCP stack uses internally. – ceilingcat – 2017-05-17T01:11:13.560

@ceilingcat Just tested this by pinging 192.168.1.1. I would have never in a million years guessed that this would work lol. I'll be sure to remember this. Thank you for explaining :) – Michael Timmerman – 2017-05-20T14:56:20.360