Interface Graphique Powershell avec XAML
Visual Studio Community
Installer :
- Installer Visual Studio Community
- Instal Visual Studio Code ou Notepad++
Création du XAML
Lancer visual Studio 2017, puis créer un nouveau projet WPF (peu import le language)

Ajouter Vos Boutons, menu et autres …

Utilisation d’un template powershell
Le plus simple est d’utiliser un template ps1 puis de coller votre code xaml dedans : Voici un lien
Ou le code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework') [xml]$XAML = @' <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Build Windows 10 ISO" Height="573.975" Width="668.135"> <Grid> <Button Name="Run" Content="Run" HorizontalAlignment="Left" Margin="12,489,0,0" VerticalAlignment="Top" Width="632" Background="#FF0DAC2C" FontWeight="Bold" BorderBrush="#FFFDBABA" OpacityMask="Black" Height="39"/> </Grid> </Window> '@ #Read XAML $reader=(New-Object System.Xml.XmlNodeReader $xaml) try{$Form=[Windows.Markup.XamlReader]::Load( $reader )} catch{Write-Host "Unable to load Windows.Markup.XamlReader. Some possible causes for this problem include: .NET Framework is missing PowerShell must be launched with PowerShell -sta, invalid XAML code was encountered."; exit} #=========================================================================== # Store Form Objects In PowerShell #=========================================================================== $xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name ($_.Name) -Value $Form.FindName($_.Name)} $Run.add_Click({ }) # Display UI object $Form.ShowDialog() | out-null |
Copie du code XAML
Une fois l’interface réalisée dans Visual Studio Community, Je copie la section entre Title= et </Windows> depuis Visual Studio Community

Vers le template Powershell

Nommage du Code XAML
Vous devez ensuite ajouter un Name= devant chaque bouton/Texte/… que vous souhaitez contrôler dans votre code powershell.
Attention, visual Studio community peut ajouter un x:Name= au lien d’un Name (comme entourer en rouge dans la capture d’écran). Dans ce cas il faudra supprimer le x: pour avoir uniquement Name

Action Sur bouton
Dans le Template, le bouton s’appel RUN (Name=Run), je peut donc ajouter une « fonction » basé sur le nom du bouton
1 2 3 |
$Run.add_Click({ #Action lorsque je click sur le bouton RUN }) |
Quelques exemples d’utilisation des NAME=
Textbox => $Information.Text = « OK »
ListBox (Ajout dans le menu) => $Axpp.Items.Add(« BingWeather ») | out-null
ListBox (lecture de la valeur) => $Appx = $Appx.SelectedItems
CheckBox => if ($MSU.IsChecked )
Vous pouvez éditer le script powershell : iso-creation-dune-iso-windows-10-multi-langue-v2
Ce script est basé sur ce présent article.
Note : Le XAML est compatible avec ps2exe, vous pouvez donc faire une « mini » application 🙂
Laisser un commentaire