[csharp]
# Récupération du nom de l'utilisateur connecté
function QuiEstConnecte() {
         $lCurrentUser = [system.security.principal.windowsidentity]::getcurrent()
         $name = $lCurrentUser.name.Substring($lCurrentUser.name.lastindexof("\")+1)
         return $name
}

# Détermination si l'utilisateur courant est bien dans le groupe demandé
function EstDansGroupe ([string] $groupechoisi) {
         $IsIn = $false
         $lCurUser =QuiEstConnecte
         $ldapQuery = "(samaccountname="+$lCurUser+")"
         $de = new-object system.directoryservices.directoryentry
         $ads = new-object system.directoryservices.directorysearcher -argumentlist $de,$ldapQuery
         $users = $ads.findall()
         $user = $users[0]
         foreach ($groupe in $user.properties.memberof) {
                 if (($groupe.tolower()).IndexOf("cn="+$groupechoisi.tolower()) -ge 0) {
                    $IsIn=$true
                    break
                 }
         }
         return $IsIn
}

$Compta_Group = "compta"
$Compta_Share = "Compta"

$Direction_Group = "direction"
$Direction_Share = "Direction"

$Commun_group = "commun"
$Commun_Share = "Commun"

$Users_Group = "users"
$users_Share = "Users"

$Serveur = "SERVER"


### Début du programme
$CurrentUser =QuiEstConnecte
$obj = New-Object -com Wscript.Network

if (EstDansGroupe($Compta_group)) {
   Write-Host $CurrentUser " appartient au groupe " $Compta_Group
   $obj.MapNetworkDrive("Z:", "\\"+$Serveur+"\"+$Compta_Share)
}

if (EstDansGroupe($Direction_group)) {
   Write-Host $CurrentUser " appartient au groupe " $Direction_Group
   $obj.MapNetworkDrive("Y:", "\\"+$Serveur+"\"+$Direction_Share)
}

if (EstDansGroupe($Commun_group)) {
   Write-Host $CurrentUser " appartient au groupe " $Commun_Group
   $obj.MapNetworkDrive("X:", "\\"+$Serveur+"\"+$Commun_Share)
}

if (EstDansGroupe($Users_group)) {
   Write-Host $CurrentUser " appartient au groupe " $Users_Group
   $obj.MapNetworkDrive("W:", "\\"+$Serveur+"\"+$Users_Share+"\"+$CurrentUser)
}