« PowerShell » : différence entre les versions
Aller à la navigation
Aller à la recherche
m (→Exemple) |
m (→Exemple) |
||
| Ligne 22 : | Ligne 22 : | ||
Création de fenêtre de dialogue : | Création de fenêtre de dialogue : | ||
* https://docs.microsoft.com/fr-fr/powershell/scripting/samples/creating-a-custom-input-box?view=powershell-6 | * https://docs.microsoft.com/fr-fr/powershell/scripting/samples/creating-a-custom-input-box?view=powershell-6 | ||
=== Fenêtre de dialogue et sélection multiple === | |||
<pre># Original example posted at http://technet.microsoft.com/en-us/library/ff730950.aspx | |||
Add-Type -AssemblyName System.Windows.Forms | |||
Add-Type -AssemblyName System.Drawing | |||
# Fonction pour la création de boite de message | |||
function Show-Message { | |||
param ( | |||
[string]$Message = "Veuillez entrer votre message", | |||
[string]$Titre = "Titre de la fenêtre", | |||
[switch]$OKCancel, | |||
[switch]$AbortRetryIgnore, | |||
[switch]$YesNoCancel, | |||
[switch]$YesNo, | |||
[switch]$RetryCancel, | |||
[switch]$IconErreur, | |||
[switch]$IconQuestion, | |||
[switch]$IconAvertissement, | |||
[switch]$IconInformation | |||
) | |||
# Affecter la valeur selon le type de boutons choisis | |||
if ($OKCancel) { $Btn = 1 } | |||
elseif ($AbortRetryIgnore) { $Btn = 2 } | |||
elseif ($YesNoCancel) { $Btn = 3 } | |||
elseif ($YesNo) { $Btn = 4 } | |||
elseif ($RetryCancel) { $Btn = 5 } | |||
else { $Btn = 0 } | |||
# Affecter la valeur pour l'icone | |||
if ($IconErreur) {$Icon = 16 } | |||
elseif ($IconQuestion) {$Icon = 32 } | |||
elseif ($IconAvertissement) {$Icon = 48 } | |||
elseif ($IconInformation) {$Icon = 64 } | |||
else {$Icon = 0 } | |||
# Charger la biblithèque d'objets graphiques Windows.Forms | |||
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null | |||
# Afficher la boite de dialogue et renvoyer la valeur de retour (bouton appuyé) | |||
$Reponse = [System.Windows.Forms.MessageBox]::Show($Message, $Titre , $Btn, $Icon) | |||
Return $Reponse | |||
} | |||
# Fin de la fonction pour la création de boite de message | |||
# Création de la fenêtre de dialogue | |||
$hauteurfenetre = 250 | |||
$form = New-Object System.Windows.Forms.Form | |||
$form.Text = "Profils à supprimer" | |||
$form.Size = New-Object System.Drawing.Size(320,$hauteurfenetre) | |||
$form.StartPosition = "CenterScreen" | |||
$OKButton = New-Object System.Windows.Forms.Button | |||
$OKButton.Location = New-Object System.Drawing.Point(75,($hauteurfenetre-80)) | |||
$OKButton.Size = New-Object System.Drawing.Size(75,23) | |||
$OKButton.Text = "OK" | |||
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK | |||
$form.AcceptButton = $OKButton | |||
$form.Controls.Add($OKButton) | |||
$CancelButton = New-Object System.Windows.Forms.Button | |||
$CancelButton.Location = New-Object System.Drawing.Point(150,($hauteurfenetre-80)) | |||
$CancelButton.Size = New-Object System.Drawing.Size(75,23) | |||
$CancelButton.Text = "Cancel" | |||
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel | |||
$form.CancelButton = $CancelButton | |||
$form.Controls.Add($CancelButton) | |||
$label = New-Object System.Windows.Forms.Label | |||
$label.Location = New-Object System.Drawing.Point(10,20) | |||
$label.Size = New-Object System.Drawing.Size(280,20) | |||
$label.Text = "Sélectionner les profils à supprimer ; sélection multiple" | |||
$form.Controls.Add($label) | |||
$listBox = New-Object System.Windows.Forms.Listbox | |||
$listBox.Location = New-Object System.Drawing.Point(10,40) | |||
$listBox.Size = New-Object System.Drawing.Size(280,($hauteurfenetre-120)) | |||
$listBox.SelectionMode = "MultiExtended" | |||
## Exemple pour la suppression de profil local # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # | |||
# $profileliste = Get-WmiObject -ClassName Win32_UserProfile | Where-Object {$_.special -ne "true"} | foreach localpath | |||
# | |||
# foreach ($profile in $profileliste) { | |||
# $listBox.Items.Add($profile) | |||
# } | |||
## à compléter... | |||
## Exemple pour la suppression de dossier # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # | |||
$dossierliste = Get-childitem "c:\" | |||
foreach ($dossier in $dossierliste) { | |||
$listBox.Items.Add($dossier) | |||
} | |||
$form.Controls.Add($listBox) | |||
$form.Topmost = $True | |||
$result = $form.ShowDialog() | |||
if ($result -eq [System.Windows.Forms.DialogResult]::OK) { | |||
$x = $listBox.SelectedItems | |||
if ($x.count -eq 0) { | |||
$reponse = show-Message -Message "Aucun élément de sélectionné !" -Titre "Sélection" -IconAvertissement | |||
} | |||
else { | |||
$maliste = $x -join "`n" | |||
# $x | |||
$reponse = show-Message -Message "Confirmez-vous la suppression des éléments ci-dessous ?`n`n$maliste" -Titre "Confirmation" -YesNo -IconQuestion | |||
if ($reponse -eq "Yes") { | |||
foreach ($elem in $x) { | |||
Get-ChildItem "c:\$elem" | |||
} | |||
} | |||
else { | |||
$reponse = show-Message -Message "Suppression annulée !" -Titre "Annulation" -IconAvertissement | |||
} | |||
} | |||
} | |||
</pre> | |||
==== Image et redimensionnement ==== | ==== Image et redimensionnement ==== | ||
Version du 8 novembre 2019 à 23:30
PowerShell est un langage de programmation pour les systèmes Microsoft Windows.
Référence
- http://www.it-connect.fr/powershell-pour-les-debutants-1ere-partie/
- http://www.it-connect.fr/powershell-et-active-directory-modifier-le-domaine-des-adresses-mails/
- http://www.it-connect.fr/active-directory-supprimer-les-ordinateurs-dune-ou-en-powershell/
- http://www.it-connect.fr/creer-des-comptes-utilisateurs-par-lot-avec-powershell-v2-et-le-module-active-directory-server-2008-r2/
- http://www.it-connect.fr/chapitres/modifier-la-resolution-de-lecran/
Source de script
Gérer les autorisations NTFS avec PowerShell :
Copie de fichiers avec BITS (Background Intelligent Transfer Service) :
Exemple
Fenêtre de dialogue

Création de fenêtre de dialogue :
Fenêtre de dialogue et sélection multiple
# Original example posted at http://technet.microsoft.com/en-us/library/ff730950.aspx
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# Fonction pour la création de boite de message
function Show-Message {
param (
[string]$Message = "Veuillez entrer votre message",
[string]$Titre = "Titre de la fenêtre",
[switch]$OKCancel,
[switch]$AbortRetryIgnore,
[switch]$YesNoCancel,
[switch]$YesNo,
[switch]$RetryCancel,
[switch]$IconErreur,
[switch]$IconQuestion,
[switch]$IconAvertissement,
[switch]$IconInformation
)
# Affecter la valeur selon le type de boutons choisis
if ($OKCancel) { $Btn = 1 }
elseif ($AbortRetryIgnore) { $Btn = 2 }
elseif ($YesNoCancel) { $Btn = 3 }
elseif ($YesNo) { $Btn = 4 }
elseif ($RetryCancel) { $Btn = 5 }
else { $Btn = 0 }
# Affecter la valeur pour l'icone
if ($IconErreur) {$Icon = 16 }
elseif ($IconQuestion) {$Icon = 32 }
elseif ($IconAvertissement) {$Icon = 48 }
elseif ($IconInformation) {$Icon = 64 }
else {$Icon = 0 }
# Charger la biblithèque d'objets graphiques Windows.Forms
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
# Afficher la boite de dialogue et renvoyer la valeur de retour (bouton appuyé)
$Reponse = [System.Windows.Forms.MessageBox]::Show($Message, $Titre , $Btn, $Icon)
Return $Reponse
}
# Fin de la fonction pour la création de boite de message
# Création de la fenêtre de dialogue
$hauteurfenetre = 250
$form = New-Object System.Windows.Forms.Form
$form.Text = "Profils à supprimer"
$form.Size = New-Object System.Drawing.Size(320,$hauteurfenetre)
$form.StartPosition = "CenterScreen"
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(75,($hauteurfenetre-80))
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(150,($hauteurfenetre-80))
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = "Sélectionner les profils à supprimer ; sélection multiple"
$form.Controls.Add($label)
$listBox = New-Object System.Windows.Forms.Listbox
$listBox.Location = New-Object System.Drawing.Point(10,40)
$listBox.Size = New-Object System.Drawing.Size(280,($hauteurfenetre-120))
$listBox.SelectionMode = "MultiExtended"
## Exemple pour la suppression de profil local # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# $profileliste = Get-WmiObject -ClassName Win32_UserProfile | Where-Object {$_.special -ne "true"} | foreach localpath
#
# foreach ($profile in $profileliste) {
# $listBox.Items.Add($profile)
# }
## à compléter...
## Exemple pour la suppression de dossier # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
$dossierliste = Get-childitem "c:\"
foreach ($dossier in $dossierliste) {
$listBox.Items.Add($dossier)
}
$form.Controls.Add($listBox)
$form.Topmost = $True
$result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
$x = $listBox.SelectedItems
if ($x.count -eq 0) {
$reponse = show-Message -Message "Aucun élément de sélectionné !" -Titre "Sélection" -IconAvertissement
}
else {
$maliste = $x -join "`n"
# $x
$reponse = show-Message -Message "Confirmez-vous la suppression des éléments ci-dessous ?`n`n$maliste" -Titre "Confirmation" -YesNo -IconQuestion
if ($reponse -eq "Yes") {
foreach ($elem in $x) {
Get-ChildItem "c:\$elem"
}
}
else {
$reponse = show-Message -Message "Suppression annulée !" -Titre "Annulation" -IconAvertissement
}
}
}
Image et redimensionnement
PowerShell et fonction :