none
Mapping a user's home drive via ps1 script/function - shows success but drive is not present in user's environment RRS feed

  • Question

  • Hello, due to more people teleworking this time of year an issue has been noticed and brought up that when connecting to our VPN the network drives are all there except for the home drive. I have created a short function that should and does find and map the home drive for the local user when run on its own. 

    Function Map-HomeDrive
    {
    $adsearch = New-Object DirectoryServices.DirectorySearcher
    $adsearch.Filter = '(&(objectCategory=person)(objectClass=user)('+"anr=$env:UserName"+'))'
    $adsearch.SearchRoot = 'LDAP://DC=domain,DC=com'
    $results = $adsearch.FindOne()
    $object = [adsi]$results.path.ToString()
    $directory = $object.HomeDirectory.ToString()
    if (!(Get-PSDrive -Name U -ErrorAction SilentlyContinue)) {New-PSDrive -Name U -Root $directory -PSProvider FileSystem -Persist -ErrorAction SilentlyContinue}
    }

    running Get-PSDrive -Name U afterwards results in a "Cannot Find drive" message

    running the if (!(Get-PSDrive ... SilentlyContinue) line by itself does map the drive and makes it available to the user.

    Does PowerShell not run the function in the same environment or is there some parameter I may be missing?

    Primarily Windows 7 w/ PowerShell v3

    thanks,

    -Nick

    Thursday, January 23, 2014 10:03 PM

Answers

  • You are not getting a reliable name result

    To get the cuurent user just use the name:

    $user=([adsisearcher]"samaccountname=$env:username").FindOne().GetDirectoryEntry()

    $user.HomeDrive
    $user.HomeDirectory

    If the home drive is set in AD the druve will already be mapped.  You don't have to map it.

    If a user is not on the network but is VPNing  then you can map it.


    ¯\_(ツ)_/¯

    • Marked as answer by -Nick Friday, January 24, 2014 1:26 PM
    Thursday, January 23, 2014 11:16 PM

All replies

  • You are not getting a reliable name result

    To get the cuurent user just use the name:

    $user=([adsisearcher]"samaccountname=$env:username").FindOne().GetDirectoryEntry()

    $user.HomeDrive
    $user.HomeDirectory

    If the home drive is set in AD the druve will already be mapped.  You don't have to map it.

    If a user is not on the network but is VPNing  then you can map it.


    ¯\_(ツ)_/¯

    • Marked as answer by -Nick Friday, January 24, 2014 1:26 PM
    Thursday, January 23, 2014 11:16 PM
  • Sorry - you need this too.

    new-psdrive `
         -Name $user.HomeDrive.Replace(':','') `
         -Root $user.HomeDirectory `
         -PSProvider FileSystem `
         -Scope Global `
         -Persist


    ¯\_(ツ)_/¯

    Thursday, January 23, 2014 11:25 PM
  • Adding to Jrv's answer.

    You will need PowerShell v3  to use -Persist switch with the New-PSDrive cmdlet...and by the look of your ISE I think you have it covered.


    Knowledge is Power{Shell}.

    Friday, January 24, 2014 7:57 AM
  • Awesome, thanks much!  Also needed to send the -Root $user.HomeDirectory.ToString() for it to convert to system.string value. 

    Good to know on -persist with v3, I don't think we're 100% yet, but pretty close.  Is there another way to do this for v2 clients?  just net use? 

    $user=([adsisearcher]"samaccountname=$env:username").FindOne().GetDirectoryEntry()

    if (!(Get-PSDrive -Name U -ErrorAction SilentlyContinue)) {net use U: $user.homeDirectory.ToString() /persistent:yes}


    • Edited by -Nick Friday, January 24, 2014 1:55 PM
    Friday, January 24, 2014 1:13 PM