locked
Mapping Network Shares Using PowerShell RRS feed

  • Question

  • Good morning,

    I am trying to create a PowerShell script for mapping a network share to Windows 7 workstations via GPO.

    The mapping needs to map the share so that when the student logs into the Windows 7 workstation, they will have their network share mapped on their system. The location which I mapping consists of several network shares (1 share for every student which is accessible using their domain user credentials).

    The following is what I have tried using to test map the share to my test system:

    New-PSDrive -Name "T" -PSProvider FileSystem -Root "\\networkshare" -Persist

    I tried running this command just for my user and I receive the following errors:

    How can I get this to work just for my user and how can I configure the script so that it will use the domain credentials of the logged on user? In other words, how can I configure this script so that the correct share is mapped for each user that logs into the system?

    Thanks in advance,

    Tuesday, February 10, 2015 5:30 PM

Answers

  • Just wondering,  why don't you do this under the user properties in Active Directory?  There is a spot to map a home folder,  and it uses the UNC you are specifying. 
    Tuesday, February 10, 2015 11:09 PM

All replies

  • If you need drive mappings, use group policy rather than a script.

    -- Bill Stewart [Bill_Stewart]

    Tuesday, February 10, 2015 5:49 PM
    Moderator
  • Why wouldn't you do this entirely with a GPO? https://technet.microsoft.com/en-ca/library/cc770902.aspx

    Am I misunderstanding your goal?

    Tuesday, February 10, 2015 5:49 PM
  • I agree with the others: Use Group Policy and Group Policy Preference (GPP) to map your network drives. Consider that with GPP, you can use Item-Level Targeting--it's amazing. That said, here are some other thoughts I had before I questioned why you'd use PS this way:

    - Can you map the drive using net use, such as net use t: \\server\share
    - Are you using a valid UNC: \\server\share? Your example indicates you've only included the server portion of your UNC path.

    Tuesday, February 10, 2015 5:56 PM
  • Thanks for your reply guys.

    I am using a valid UNC: New-PSDrive -Name "T" -PSProvider FileSystem -Root "\\server\share" -Persist, and I am receiving the error.

    I have tried mapping via GPO but it does not work, with or without Item-Level targeting, and it does not map on my test system, which is on the domain.

    I did create a batch script using net use T: \\server\share\ /Persistent:yes but it does not work. Receiving System error 53: the network path was not found (after entering credentials). The network path exists just fine and is accessible when manually mapping to it.

    I tried using the same net use command via PowerShell and nothing got mapped. But when I use the same command by providing the name of the share: net use t: \\server\share\my share /persistent:yes, then the command works and does not prompt me for credentials.

    Is there a command I can use in my PowerShell script or the batch script that will use the credentials of whichever user is logging into the system and by doing so, it will map that users share? The whole point for this will be to apply this via GPO so that whichever user logs into the system, they will have their share mapped which is located in \\server\share\.

    Thanks in advance,

     

    Tuesday, February 10, 2015 7:30 PM
  • No script required if you go with this:

    https://support.microsoft.com/kb/816313?wa=wsignin1.0

    Extremely simple to set up, this is what I use.


    Don't retire TechNet! - (Don't give up yet - 13,085+ strong and growing)

    Tuesday, February 10, 2015 7:59 PM
  • Hi Mike,

    Thanks for the suggestion and link but that is not what I am trying to accomplish.

    The \\server\share\ already consists of network shares for each user. I need to be able to map  the network server share \\server\share\ via GPO or script so that whichever user logs into the system to which the GPO has been applied to, they will be able to see their share mapped as a network drive. 

    Each users share is located in \\server\shares\

    Thanks,

    Tuesday, February 10, 2015 8:27 PM
  • Thanks for your reply guys.

    I am using a valid UNC: New-PSDrive -Name "T" -PSProvider FileSystem -Root "\\server\share" -Persist, and I am receiving the error.

    I have tried mapping via GPO but it does not work, with or without Item-Level targeting, and it does not map on my test system, which is on the domain.

    I did create a batch script using net use T: \\server\share\ /Persistent:yes but it does not work. Receiving System error 53: the network path was not found (after entering credentials). The network path exists just fine and is accessible when manually mapping to it.

    I tried using the same net use command via PowerShell and nothing got mapped. But when I use the same command by providing the name of the share: net use t: \\server\share\my share /persistent:yes, then the command works and does not prompt me for credentials.

    Is there a command I can use in my PowerShell script or the batch script that will use the credentials of whichever user is logging into the system and by doing so, it will map that users share? The whole point for this will be to apply this via GPO so that whichever user logs into the system, they will have their share mapped which is located in \\server\share\.

    Thanks in advance,

     

    You have to resolve the 53 error. That is not a scripting issue. You are using a share path the does not exist.  Fix that and you are set.

    I suspect you are miing an error message somewhere.


    ¯\_(ツ)_/¯

    Tuesday, February 10, 2015 8:30 PM
  • Hi Mike,

    Thanks for the suggestion and link but that is not what I am trying to accomplish.

    The \\server\share\ already consists of network shares for each user. I need to be able to map  the network server share \\server\share\ via GPO or script so that whichever user logs into the system to which the GPO has been applied to, they will be able to see their share mapped as a network drive. 

    Each users share is located in \\server\shares\

    Thanks,

    That's pretty much what the home folder does.

    Connect driveLetter: To: \\servername\users\%username%

    Now each user will have the drive letter you specify connected automatically. If the folder doesn't exist, ADUC will create it for you.

    Perhaps I'm not clear on what you're trying to accomplish.


    Don't retire TechNet! - (Don't give up yet - 13,085+ strong and growing)

    Tuesday, February 10, 2015 8:37 PM
  • Thanks for your reply guys.

    I am using a valid UNC: New-PSDrive -Name "T" -PSProvider FileSystem -Root "\\server\share" -Persist, and I am receiving the error.

    I have tried mapping via GPO but it does not work, with or without Item-Level targeting, and it does not map on my test system, which is on the domain.

    I did create a batch script using net use T: \\server\share\ /Persistent:yes but it does not work. Receiving System error 53: the network path was not found (after entering credentials). The network path exists just fine and is accessible when manually mapping to it.

    I tried using the same net use command via PowerShell and nothing got mapped. But when I use the same command by providing the name of the share: net use t: \\server\share\my share /persistent:yes, then the command works and does not prompt me for credentials.

    Is there a command I can use in my PowerShell script or the batch script that will use the credentials of whichever user is logging into the system and by doing so, it will map that users share? The whole point for this will be to apply this via GPO so that whichever user logs into the system, they will have their share mapped which is located in \\server\share\.

    Thanks in advance,

     

    You have to resolve the 53 error. That is not a scripting issue. You are using a share path the does not exist.  Fix that and you are set.

    I suspect you are miing an error message somewhere.


    ¯\_(ツ)_/¯

    The error doesn't come up when I enter the command with the name of my share:

    net use t: \\server\share\mysharename

    It only comes up when not providing mysharename

    Have you ever seen this happen?

    Thanks,

    Tuesday, February 10, 2015 9:34 PM
  • Thanks for your reply guys.

    I am using a valid UNC: New-PSDrive -Name "T" -PSProvider FileSystem -Root "\\server\share" -Persist, and I am receiving the error.

    I have tried mapping via GPO but it does not work, with or without Item-Level targeting, and it does not map on my test system, which is on the domain.

    I did create a batch script using net use T: \\server\share\ /Persistent:yes but it does not work. Receiving System error 53: the network path was not found (after entering credentials). The network path exists just fine and is accessible when manually mapping to it.

    I tried using the same net use command via PowerShell and nothing got mapped. But when I use the same command by providing the name of the share: net use t: \\server\share\my share /persistent:yes, then the command works and does not prompt me for credentials.

    Is there a command I can use in my PowerShell script or the batch script that will use the credentials of whichever user is logging into the system and by doing so, it will map that users share? The whole point for this will be to apply this via GPO so that whichever user logs into the system, they will have their share mapped which is located in \\server\share\.

    Thanks in advance,

     

    You have to resolve the 53 error. That is not a scripting issue. You are using a share path the does not exist.  Fix that and you are set.

    I suspect you are miing an error message somewhere.


    ¯\_(ツ)_/¯

    The error doesn't come up when I enter the command with the name of my share:

    net use t: \\server\share\mysharename

    It only comes up when not providing mysharename

    Have you ever seen this happen?

    Thanks,

    Tuesday, February 10, 2015 9:35 PM
  • This is another case where you told us the wrong info. None of your posts have been consistent and the image is unreadable. Please try to b more careful in the future and you will get better answers.

    New-PSDrive -Name T -PSProvider FileSystem -Root \\server\share\mysharename -Persist

    That will always work if this works:
    net use t: \\server\share\mysharename

    Note that I don't decorate everything with lots of quotes and I use the exact same root UNC.


    ¯\_(ツ)_/¯

    Tuesday, February 10, 2015 10:22 PM
  • jrv,

    If you can, please read my post from the very beginning. I have provided the same information throughout this post and provided the same examples throughout.

    This being said... the PowerShell command does not work because it returns the following error message:

    New-PSDrive : The specified network resource or device is no longer available                                              At line:1 char:1                                                                                                                                  + New-PSDrive "T" -PSProvider FileSystem -Root "\\server\share\%username%"...                                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo: InvalidOperation: (T:PSDRiveInfo) [New-PSDrive], Win32Exception                              + FullyQualifiedErrorId : CouldNotMapNetworkDrive,Microsoft.PowerShell.Commands.NewPSDriveCommand

    Thanks,

    I am entering the command like this: 

    New-PSDrive -Name "T" -PSProvider FileSystem -Root "\\server\share\%username%" -Persist


    Tuesday, February 10, 2015 10:58 PM
  • You cannot say %username% in PowerShell.

    You must say $Env:USERNAME instead.

    Note that it applies to the current user only.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, February 10, 2015 11:09 PM
    Moderator
  • Just wondering,  why don't you do this under the user properties in Active Directory?  There is a spot to map a home folder,  and it uses the UNC you are specifying. 
    Tuesday, February 10, 2015 11:09 PM
  • jrv,

    If you can, please read my post from the very beginning. I have provided the same information throughout this post and provided the same examples throughout.

    This being said... the PowerShell command does not work because it returns the following error message:

    New-PSDrive : The specified network resource or device is no longer available                                              At line:1 char:1                                                                                                                                  + New-PSDrive "T" -PSProvider FileSystem -Root "\\server\share\%username%"...                                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo: InvalidOperation: (T:PSDRiveInfo) [New-PSDrive], Win32Exception                              + FullyQualifiedErrorId : CouldNotMapNetworkDrive,Microsoft.PowerShell.Commands.NewPSDriveCommand

    Thanks,

    I am entering the command like this: 

    New-PSDrive -Name "T" -PSProvider FileSystem -Root "\\server\share\%username%" -Persist


    You keep changing the command. Anyone knows the %username% does not work in PowerShell. 

    You need to learn how to ask a question in a technical forum.

    Please review this: http://mikepope.com/blog/DisplayBlog.aspx?permalink=554


    ¯\_(ツ)_/¯



    • Edited by jrv Tuesday, February 10, 2015 11:16 PM
    Tuesday, February 10, 2015 11:10 PM
  • Thanks jrv for your comments.

    Thank you to all for your help!

    Monday, February 16, 2015 1:01 AM