Question Création d'un dossier user + personne privé

Plus d'informations
il y a 4 ans 11 mois #28613 par guigui76
Bonsoir
ma petite participation à la communauté:
la création d'un dossier User et d'un dossier personnel privé ne pouvant être supprimé par l'user.
Bonne soirée

[code:1]#################################################
#inspiration
# Downloaded on blog.akril.net
# www.tutos.eu/?n=8963
#################################################

# Chargement des Windows Form


#lieu de stockage du dossier
$stockage = \"C:\1\\"


#region

[void][System.Reflection.Assembly]::LoadWithPartialName(\"System.Windows.Forms\"«»)

[void][System.Reflection.Assembly]::LoadWithPartialName(\"System.Drawing\"«»)

#endregion

# Creation de la form principale
$form = New-Object Windows.Forms.Form

# Pour bloquer le resize du form et supprimer les icones Minimize and Maximize
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog

$form.MaximizeBox = $False

$form.MinimizeBox = $False

# Choix du titre
$form.Text = \"Création du lecteur P:\"

# Choix de la taille
$form.Size = New-Object System.Drawing.Size(360,280)

# Récupére la description actuelle du poste
$old_name= Get-CimInstance -ClassName Win32_OperatingSystem | select Description


#################################################
# ajout des boutons graphiques
#################################################

$button_ok = New-Object System.Windows.Forms.Button

$button_ok.Text = \"OK\"

$button_ok.Size = New-Object System.Drawing.Size(100,30)

$button_ok.Location = New-Object System.Drawing.Size(200,180)

$form.Controls.Add($button_ok)


$button_exit = New-Object System.Windows.Forms.Button

$button_exit.Text = \"Exit\"

$button_exit.Size = New-Object System.Drawing.Size(100,30)

$button_exit.Location = New-Object System.Drawing.Size(50,180)

$form.Controls.Add($button_exit)


#################################################
# ajout du label prénom
#################################################


$label1 = New-Object System.Windows.Forms.Label

$label1.AutoSize = $true

$label1.Location = New-Object System.Drawing.Point(50,20)

$label1.Text = 'Prénom:'

$form.Controls.Add($label1)


$textBox1 = New-Object System.Windows.Forms.TextBox

$textBox1.Size = New-Object System.Drawing.Size(250,20)

$textBox1.Location = New-Object System.Drawing.Point(50,40)

$form.Controls.Add($textBox1)


#################################################
# ajout du label nom
#################################################

$label2 = New-Object System.Windows.Forms.Label

$label2.AutoSize = $true

$label2.Location = New-Object System.Drawing.Point(50,70)

$label2.Text = 'Nom:'

$form.Controls.Add($label2)


$textBox2 = New-Object System.Windows.Forms.TextBox

$textBox2.Size = New-Object System.Drawing.Size(250,20)

$textBox2.Location = New-Object System.Drawing.Point(50,90)

$form.Controls.Add($textBox2)

#################################################
# ajout du label de stockage
#################################################

$label3 = New-Object System.Windows.Forms.Label

$label3.AutoSize = $true

$label3.Location = New-Object System.Drawing.Point(50,120)

$label3.Text = 'Lieu de création:'
$form.Controls.Add($label3)


#################################################
# ajout du label de stockage
#################################################

$textBox3 = New-Object System.Windows.Forms.TextBox

$textBox3.Size = New-Object System.Drawing.Size(250,80)

$textBox3.Location = New-Object System.Drawing.Point(50,140)

$textBox3.Text = $stockage

$form.Controls.Add($textBox3)


#################################################
# Quand on clique sur le bouton Fermer
#################################################

$button_exit.Add_Click(

{
$form.Close();
})

#################################################
# Gestion event quand on clique sur le bouton OK
#################################################



$button_ok.Add_Click(

{
if (($textbox1.TextLength -eq 0) -or ($textbox2.TextLength -eq 0) -or ($textbox3.TextLength -eq 0))
{
# Creation de la form principale
$form = New-Object Windows.Forms.Form

# Pour bloquer le resize du form et supprimer les icones Minimize and Maximize
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog

$form.MaximizeBox = $False

$form.MinimizeBox = $False

# Choix du titre
$form.Text = \"Erreur:\"

# Choix de la taille
$form.Size = New-Object System.Drawing.Size(160,120)

# Récupére la description actuelle du poste
$old_name= Get-CimInstance -ClassName Win32_OperatingSystem | select Description


#################################################
# ajout des boutons graphiques
#################################################

$button_ok = New-Object System.Windows.Forms.Button

$button_ok.Text = \"OK\"

$button_ok.Size = New-Object System.Drawing.Size(100,30)

$button_ok.Location = New-Object System.Drawing.Size(20,40)

$form.Controls.Add($button_ok)


#################################################
# ajout du label erreur
#################################################


$label1 = New-Object System.Windows.Forms.Label

$label1.AutoSize = $true

$label1.Location = New-Object System.Drawing.Point(10,20)

$label1.Text = 'Il manque des informations!'

$form.Controls.Add($label1)



$button_ok.Add_Click(

{
$form.Close();
})

$form.ShowDialog()

}
else
{

#lieu de stockage du dossier
$PersonnelPathRoot = $textBox3.Text

#################################################
# Gestion nom et prénom
#################################################

#Calcul du nombre de caractère du prénom
$nb_textbox1=$textBox1.Text.Length

#mise en variable de la premiere lettre du prenom
$prenom_light= $textBox1.Text.substring(0,$textBox1.Text.length -($nb_textbox1-1))

#Calcul du nombre de caractère du nom
$nb_textbox2=$textBox2.Text.Length

if ($nb_textbox2-lt '6')

{$nb_maxi =0}

else
{$nb_maxi = 6}

#mise en variable du nom avec 7 lettres aux maxi
$nom_light= $textBox2.Text.substring(0,$textBox2.Text.length -$nb_maxi )

#prépparation du nom du profil
$Nom_profil= $nom_light+$prenom_light

##################################################################################################
#Creation du lecteur p:
##################################################################################################



# Creation du dossier personnel
New-Item -ItemType Directory -Path \"$PersonnelPathRoot\$Nom_profil\"

$bloquage = \"$\"
$destination = \"$PersonnelPathRoot$Nom_profil\"

Write-host $destination
#ajout du partage
New-SmbShare –Name $Nom_profil$ -Path $destination -FullAccess Utilisateurs

#New-SmbShare –Name dudul$ –Path C:\dudul -FullAccess Utilisateurs

# Ajout des autorisations NTFS
Add-NTFSAccess –Path \"$PersonnelPathRoot\$Nom_profil\" –Account \"$Nom_profil\" –AccessRights Modify

# Modifier le proprietaire sur le dossier
Set-NTFSOwner -Path \"$PersonnelPathRoot\$Nom_profil\" -Account \"$Nom_profil\"

# Creation du dossier Personnel_privé
New-Item -ItemType Directory -Path \"$PersonnelPathRoot\$Nom_profil\Personnel_prive\"

# Ajout des autorisations NTFS
Add-NTFSAccess –Path \"$PersonnelPathRoot\$Nom_profil\Personnel_prive\" –Account \"$Nom_profil\" -AccessType Deny -AppliesTo ThisFolderOnly -AccessRights Delete

# Supprimer des autorisations NTFS
Remove-NTFSAccess –Path \"$PersonnelPathRoot\$Nom_profil\" –Account \"Utilisateurs\" -AccessRights FullControl
}
}
)
# Affichage de la Windows

$form.ShowDialog()

#################################################
# END OF PROGRAM
#################################################[/code:1]

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 4 ans 11 mois #28629 par Arnaud Petitjean
Merci !!!

MVP PowerShell et créateur de ce magnifique forum :-)
Auteur de 6 livres PowerShell aux éditions ENI
Fondateur de la société Start-Scripting
Besoin d'une formation PowerShell ?

Connexion ou Créer un compte pour participer à la conversation.

Temps de génération de la page : 0.116 secondes
Propulsé par Kunena