2017-05-10

Mount-Fileserver.ps1 - Mount file server folders based on numbers in username

The problem to solve

I had to mount secondary home folders that have users divided in separate folders based on a random number in their usernames.

The conditions were these:

1. Students have 4-5 letters and 3 numbers in usernames (abcde123) and their foler is in a sub-folder called users-N where N is the first number in their usernames.

2. Employees have 4-5 letters and 2 numbers in usernames and use another share on the same server.

This is how I did it:

The script

#========== Script Begins Mount-Fileserver.ps1 ==========
# Script to mount file storage at, Djerf 2017-04-26
#\\fileserver.example.net\\users-N\username # used for students with 3-numbers in username, N is the first number
#\\fileserver.example.net\\users-fo\username # used for employees with 2-numbers in username

# Change this to your file server, make sure to change the share names below
$FileServer="fileserver.example.net"

# Employees share
$EmployeeShare="users-fo"

# Students share, first number in username will be added
$StudentShare="users-"

$MountUsername = $Env:Username


if ($MountUsername -match "^[a-z]{4,5}[0-9]{2}$") {
Net use z: /delete
    Net use z: \\$FileServer\$EmployeeShare\$MountUsername
}

if ($MountUsername -match "^[a-z]{4,5}[0-9]{3}$") {
    $MountUserNumbersonly = $MountUsername -replace '\D+(\d+)','$1'
    $MountUserFirstNumber=$MountUserNumbersonly.Substring(0,1)
    Net use z: /delete
Net use z: \\$FileServer\$StudentShare-$MountUserFirstNumber\$MountUsername
}
#========== Script Ends ==========

What could be improved

I tried to use New-PSDrive to mount, but for some reason that only added the drive inside powershell and not in the File Explorer, so I had to go back to use the old "NET USE" command.
If anyone know why that happened and how to fix it, please let me know!
My work load is too high at the moment to spend more time to fix this when its working but ugly.

How it runs

I chose to use a GPO to deploy the script, so that it is stored locally on all computers that needs to mount this server. Experience say that that will work the best in the long term. I have been running scripts directly from the AD-servers, but from time to time that adds a lag. If I update the script the GPO will automatically upgrade the file on the clients.

The GPO will make sure to create a folder and copies the ps1-file and a launcher vbs script that I use to make the script invisible 

#========== Script Begins Mount-Fileserver-Launcher.vbs ==========
' Launcher to make scheduled task run invisible Djerf 2016-11-22
Dim shell,command
Set shell = CreateObject("WScript.Shell")
windowsdir = shell.ExpandEnvironmentStrings("%windir%")
programdir = shell.ExpandEnvironmentStrings("%programfiles%")
command =("""" & windowsdir & "\system32\WindowsPowerShell\v1.0\powershell.exe""" & " -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -WindowStyle Hidden -File " & """" & programdir & "\Script\Mount-Fileserver.ps1""")
shell.Run command,0
#========== Script Ends ==========
(This script should be modified to run the ps1-file from the same folder as the vbs-script, but I'm a total noob at vbs, so this is what I came up with after a lot of Googling.)

I create a scheduled task that runs the vbs-script on user logon, and the vbs script runs the ps1-script in the background and the correct folder is mounted.