Powershell Script zum Mappen von Laufwerken

Mal ein Kleines Script das so noch nicht Funktioniert, da das Mapping über GET-PSDrive nur für die eine Powershell Session funktioniert.

Nicht allerdings wenn ich dann ausserhalb des Scripts auf das gemappte Laufwerk zugreifen will.

 

 

##2013-07-11 - Daniel Schenkl #
##Find the matching HomeDirectory from Active Directory and map it to the homedrive U: for use in Citrix environments
##

#importing modules to use
Import-Module ActiveDirectory

#variables
$user=$Env:USERNAME
$homedir

#main program
#check if there is a network drive called 

Get-PSDrive -Name U | format-wide root > $drive

Write-Host $drive

if ($drive -eq "U:\") {
    #remove mapped network drive
    Remove-PSDrive -name U -psprovider FileSystem

    }

else 
    { getADUser
    }

#find the matching user and save the HomeDirectory to $homedir
#seperate homeDirectory
function getADUser {
    Get-ADUser -Filter 'name -like "*"' -SearchBase "CN=Username,OU=IT,DC=company,DC=de" -Properties homeDirectory |  Format-Wide Homedirectory
    }

$homedir = getADUser

Write-Host $homedir

#map a new HomeDirectory to U
#New-PSDrive -name U -psprovider FileSystem -root $homedir
if not exist U: net use U: $homedir /persistant:NO
#set drive u to U:
#Set-Location U:
#check U: and show it
Get-PSDrive -Name U | format-wide root

#that's it keep smiling ;)