Connecting to a remote machine using Win32API and Ruby

I haven't used the Win32 API in a while as most of my time is spent in *nix as of late, but when I saw this post on the Ruby mailing list, I thought it'd be a good topic to address.  The Win32 API provides a number of different ways of connecting to machines or other network resources WNetAddConnection, WNetAddConnection2, and WNetAddConnection3.  We'll be using WNetAddConnection2, which supersedes WNetAddConnection, and we won't be using a dialog box which is what WNetAddConnect3 is for.  If you want to read about the details check out the MSDN Documentation (Namely, the dwFlags section).  The issue Jon was running in to comes from the NETRESOURCE structure that must be passed that gives info about the resource being connected to, again you can find the full API doc at MSDN, but all sections must be specified.  Here is my quick example, keeping the same output as Jon had requested, as usual, if all goes well with your remote machine you should get a return value of 0. Feel free to clone and adjust as needed. Any suggestions? Feel free to leave them in the comments! If you found this helpful, subscribe to the mailing list to be notified of updates and when my book, Ruby For System Administrators, is released:

Liked what you saw? Get more automation insights delivered straight to your inbox.

* indicates required