A Script to set up Mapped Drives with error checking

MS SQL On Ubuntu 16.04

So… Having trouble with mapped drives?

I have a Plex server on a Windows 10 machine at home which has sources across multiple network drives (mapped drives).

Like anyone, I mapped the drives to the server and set up my media libraries accordingly.  Unfortunately, if the machine was rebooted, the drive letters could switch around which meant Plex no longer worked – less than ideal.

I wrote this script to create a number of mapped drives – it’s probably more complex than it needs to be, but what can I say?  If I use a script fairly often, things like differently coloured text and error checking make things more obvious so you can quickly check it’s worked out well or otherwise.  Don’t worry – it’ll all make sense pretty soon.

Anyway – here’s the mapped drives script:

Essentially, it maps the network drives to the same letter every time with green messages showing success, but lets you know if there’s trouble mapping any of the disks, and colours any error messages in red.

It also opens a new ‘My Computer’ windows before it completes.

The script window will stay open until you press any key while it is still active.  And, when you run the mapped drives script it looks like this…

some details have been removed from the mapped drives script output.
What the script looks like when run.  No Errors!

Making it work for you

Getting it on your PC

Copy the above script into a blank notepad file and save it as MapNetworkDrives.bat somewhere on your PC.  Double click the file to run whenever you need to make sure your mapped drives are set up properly.

The highlights of the mapped drives script

The important bits of this script are these:

We’ll go through them in turn.

The first one

Any line in the script that starts with @rem is just a comment for the file.  If you change anything, update the comments so if you need to edit it again, it’s all explained.

The second one

Where ever the call function looks like this, you can change anything between the quotes to suit your system or personal preferences – for example, highly sarcastic messages for dumbasses who need to learn about mapped drives.

The third one

The first line maps the network drive //YourDrivepath2 to the mapped drive v: and hides the output while passing the completion code of the net command to the 2nd and 3rd lines.

The second line is checking to see if the net command in line 1 did not complete with errors and when that is true, it prints “//Yourdrivepath2 mapped to v: ” – feel free to change the text after echo to whatever you want to be shown when it works.

The 3rd line checks to see if the net command in line 1 completed with errors, and if it did to put the line in between the quotes – again feel free to change this.

The reason line 3 is so complex compared to line 2 is because it’s changing the colour of the text to red by calling the colorEcho which is defined at the bottom of the script.