jobejoe Posted January 28, 2016 Posted January 28, 2016 (edited) Hi people, I have a script that we use to map network drives on our Surface RTs, it runs on login asks them for their username and password and then maps the drives dependent on what they input. I'll copy the script below for you to see. I've been asked if I can create a powershell script to do the same thing, but I am struggling to get it to work how I intend as I am completely new to powershell. Any help would be greatly appreciated. I think the original script was taken from here and edited a bit to fit our needs. I just looked and it was Sted from this forum that originally created this script, so thank you! echo off rem mapdrive prompt v3 20-09-13 net use N: /delete net use T: /delete net use W: /delete rem echo %mapu% set /p str=Enter Username: rem echo %str% set str2=%str:~0,2% rem echo %str2% rem echo %stra% rem decide what year group by first character if %str2%==08 goto equals1 if %str2%==09 goto equals2 if %str2%==10 goto equals3 if %str2%==11 goto equals4 if %str2%==12 goto equals5 if %str2%==13 goto equals6 if %str2%==14 goto equals7 if %str2%==15 goto equals8 rem echo not 0-15 so presumably staff net use N: \\fpg-fs01\%str%$ /user:%str% * net use W: "\\fpg-fs02\RMShared Documents" /user:%str% * net use T: \\fpg-fs01\StaffShared rem echo goto end :equals1 net use N: \\fpg-fs02\%str%$ /user:%str% * net use W: "\\fpg-fs02\RMShared Documents" goto end :equals2 net use N: \\fpg-fs02\%str%$ /user:%str% * net use W: "\\fpg-fs02\RMShared Documents" goto end :equals3 net use N: \\fpg-fs02\%str%$ /user:%str% * net use W: \\fpg-fs02"RMShared Documents" goto end :equals4 net use N: \\fpg-fs02\%str%$ /user:%str% * net use W: "\\fpg-fs02\RMShared Documents" goto end :equals5 net use N: \\fpg-fs02\%str%$ /user:%str% * net use W: "\\fpg-fs02\RMShared Documents" goto end :equals6 net use N: \\fpg-fs02\%str%$ /user:%str% * net use W: "\\fpg-fs02\RMShared Documents" goto end :equals7 net use N: \\fpg-fs02\intake14\%str% /user:%str% * net use W: "\\fpg-fs02\RMShared Documents" goto end :equals8 net use N: \\fpg-fs02\intake15\%str% /user:%str% * net use W: "\\fpg-fs02\RMShared Documents" goto end :end Edited January 28, 2016 by jobejoe
themightymrp Posted January 28, 2016 Posted January 28, 2016 I can't guarantee this is exactly right (or as neat as it could no doubt be) but it may be fairly close with a few tweaks: $User = Read-Host -Prompt 'Input your username' $year = $user.substring(0,2) get-psdrive N, T, W | remove-psdrive If ($year -Match "08" -or "09" -or "10" -or "11" -or "12" -or "13" -or "14" -or "15) {New-PSDrive –Name "N" –PSProvider FileSystem –Root "\\fpg-fs02\$user$"} Else {New-PSDrive –Name "N" –PSProvider FileSystem –Root "\\fpg-fs01\$user$"} If ($year -Match "08" -or "09" -or "10" -or "11" -or "12" -or "13" -or "14" -or "15) {New-PSDrive –Name "W" –PSProvider FileSystem –Root "\\fpg-fs02\RMShared Documents"} Else {New-PSDrive –Name "W" –PSProvider FileSystem –Root "\\fpg-fs02\RMShared Documents"} If ($year -NotMatch "08" -or "09" -or "10" -or "11" -or "12" -or "13" -or "14" -or "15) {New-PSDrive –Name "T" –PSProvider FileSystem –Root "\\fpg-fs01\StaffShared"}
themightymrp Posted January 28, 2016 Posted January 28, 2016 Sorry, edit that above. I have too many quotes in there that don't need to be: $User = Read-Host -Prompt 'Input your username' $year = $user.substring(0,2) get-psdrive N, T, W | remove-psdrive If ($year -Match "08" -or "09" -or "10" -or "11" -or "12" -or "13" -or "14" -or "15") {New-PSDrive –Name N –PSProvider FileSystem –Root \\fpg-fs02\$user$} Else {New-PSDrive –Name N –PSProvider FileSystem –Root \\fpg-fs01\$user$} If ($year -Match "08" -or "09" -or "10" -or "11" -or "12" -or "13" -or "14" -or "15") {New-PSDrive –Name W –PSProvider FileSystem –Root \\fpg-fs02\RMShared Documents} Else {New-PSDrive –Name W –PSProvider FileSystem –Root \\fpg-fs02\RMShared Documents} If ($year -NotMatch "08" -or "09" -or "10" -or "11" -or "12" -or "13" -or "14" -or "15") {New-PSDrive –Name T –PSProvider FileSystem –Root \\fpg-fs01\StaffShared}
themightymrp Posted January 28, 2016 Posted January 28, 2016 SORRY!!!! Final code, much tidied $User = Read-Host -Prompt 'Input your username' $year = $user.substring(0,2) get-psdrive N, T, W | remove-psdrive If ($year -Match '08|09|10|11|12|13|14|15') {New-PSDrive –Name N –PSProvider FileSystem –Root \\fpg-fs02\$user$} Else {New-PSDrive –Name N –PSProvider FileSystem –Root \\fpg-fs01\$user$} {New-PSDrive –Name W –PSProvider FileSystem –Root "\\fpg-fs02\RMShared Documents"} If ($year -NotMatch '08|09|10|11|12|13|14|15') {New-PSDrive –Name T –PSProvider FileSystem –Root \\fpg-fs01\StaffShared}
jobejoe Posted January 29, 2016 Author Posted January 29, 2016 (edited) Thank you, I will give this a try and see how I get on One thing I did see when trying to figure this out is the get-credential command for the username and password, it also seems to bring up a more user friendly login interface, can that be used here or is the way you have done it the best way. It's school kids using this so just want it as simplified for them as possible, haha. THank you so much for your help so far, life saver. Edited January 29, 2016 by jobejoe
themightymrp Posted January 29, 2016 Posted January 29, 2016 No worries. Looking at it again with fresh morning eyes, you probably don't need the {} brackets around the line which maps the W drive I'm still feeling my way through powershell myself so when I saw this thread I thought it would be a good challenge
themightymrp Posted January 29, 2016 Posted January 29, 2016 Hi there Yes you could use the get-credential by changing the start of the script to something like this: $cred = Get-Credential $User = $cred.username $year = $user.substring(13,2) The numbers in the brackets denote how far along the username to start picking out letters/numbers and how many characters to read. For me I enter my credentials in the form domain\username - if I want to work with the username section, I want to start 13 characters in so that it doesn't read the domain name and the '\' It depends how you want users to enter their username really
jobejoe Posted February 2, 2016 Author Posted February 2, 2016 This does work, the only issue I am having is when a student logs off and another comes along and logs in, the script runs and they input their details. It won't map the shared drive as it's already been logged into by the previous user. Which is strange as it works perfectly fine when doing the same thing with the batch script.
jobejoe Posted February 2, 2016 Author Posted February 2, 2016 (edited) I got this working how I wanted, below is the final script I used. I needed FQDN for the shares it seems, but it's all working now thankfully. Really appreciate your help themightymrp, thanks once again. Code: Net use N: /delete Net use W: /delete Net use T: /delete $cred = Get-Credential $User = $cred.username $year = $user.substring(0,2) If ($year -Match '09|10|11|12|13|14|15') {New-PSDrive –Name N –PSProvider FileSystem –Root "\\fpg-fs02.fpgs.internal\$user$" -credential $cred -Persist -Scope Global} Else {New-PSDrive –Name N –PSProvider FileSystem –Root "\\fpg-fs01.fpgs.internal\$user$" -credential $cred -Persist -Scope Global} New-PSDrive –Name W –PSProvider FileSystem –Root "\\fpg-fs02.fpgs.internal\RMShared Documents" -credential $cred -Persist -Scope Global If ($year -NotMatch '09|10|11|12|13|14|15') {New-PSDrive –Name T –PSProvider FileSystem –Root "\\fpg-fs01.fpgs.internal\StaffShared" -credential $cred -Persist -Scope Global} Edited February 2, 2016 by jobejoe
themightymrp Posted February 2, 2016 Posted February 2, 2016 Nicely done! Sorry I didn't get back to you this morning, I didn't get any free time. I see you had to modify the lines to map the drives to use the -credential parameter, didn't think about that bit. I'm glad I pointed you in the right direction anyway
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now