Batch or PowerShell to mount drives on a FQDN

2

I currently use a batch script to mount my network shares each morning:

NET USE * /DELETE /Y
NET USE Z: \\<IP>\<SHARE> <AUTHENTICATION> /persistent:no
...

Which is fine when I'm on the network, but doesn't work off-site. We have a domain which points to our public IP, and then port forwarding kicks in, so in theory, I can

NET USE * /DELETE /Y
NET USE Z: \\fqdn.com\<SHARE> <AUTHENTICATION> /persistent:no
...

Except that doesnt work in the batch script or windows UI.

I'm thinking there'd be a way to get the IP of fqdn.com (which is conveniently also stored at fqdn2.com/ip.txt) but there's well outside my experience of batching.

Any input much appreciated.

LuckySpoon

Posted 2013-07-18T23:23:50.207

Reputation: 672

Um, why not just change the end part to /persistent:yes? and not map your drives every day. – Spencer5051 – 2013-07-25T01:31:27.123

You're allowing SMB in from the internet?! That's brave. – afrazier – 2013-07-29T17:01:57.357

Answers

1

The task is a little bit easier by using PowerShell since you have the full power of the .Net framework at your disposal. There is a class called System.Net.WebClient that can be used to download a file and get its contents. You can use a PowerShell script similar to the one below to retrieve the contents of the ip.txt file and then execute your cmd.exe commands.

$fqdn = "fqdn domain name goes here"
$url  = "http://$fqdn/ip.txt"

$fqdnIp = (new-object System.Net.WebClient).DownloadString($url)

Write-Host -ForegroundColor Green "Retrieved contents of ${url}: $fqdnIp"

$cmd = "`"NET USE * /DELETE /Y`""
&cmd.exe /c $cmd

$cmd = "`"NET USE Z: \\$fqdnIp\<SHARE> <AUTHENTICATION> /persistent:no`""
&cmd.exe /c $cmd

user87883

Posted 2013-07-18T23:23:50.207

Reputation:

PERFECT! Thank you for actually answering my question! – LuckySpoon – 2013-07-30T08:50:16.627

0

Just because you can go to the website at fqdn.com, that does not give you access to your internal network or internal domain. Port 80 is the standard port for HTTP (websites) which is being forwarded from the public IP to your internal web server. However, with maybe a few other exceptions, that's all that's allowed through.

To access your internal network and resources, you need a virtual private network (VPN). A VPN is a secure (encrypted) connection from the Internet to your internal network. VPNing allows a computer outside your internal network to "feel" as if it is inside.

Once you have established a VPN connection, your batch script will work. You can access internal network drives and printers, and do anything you could do while in the office.

Microsoft offers a free VPN server with all versions of Windows Server and the client is built into all version of Windows. There are other VPN solutions out there as well, both hardware and software, both free and paid.

Talk to your Windows/network administrator, there may be a VPN already in place.

Keltari

Posted 2013-07-18T23:23:50.207

Reputation: 57 019

I think I may have not made my question clear - ports are all forwarded etc. My point is - if I can get the IP address that fqdn.com resolves to inside my batch script, all is fine. – LuckySpoon – 2013-07-24T09:08:07.747

0

your scripts should work, except that you should check 4 things

  1. Is DNS working? try to verify it on your offsite machine by ping.
  2. try to disable WINS on your off-site client.
  3. Did you forward all the required ports ? try to put the server at DMZ host and see if that works.

    if the public ip does not change, then simple hard code it into your systems32\etc\hosts.

user218473

Posted 2013-07-18T23:23:50.207

Reputation: