Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

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

$
0
0

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


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>