Overblog Tous les blogs Top blogs Technologie & Science Tous les blogs Technologie & Science
Editer l'article Suivre ce blog Administration + Créer mon blog
MENU

PSUI Radio

Publié le par damcuvelier

PSUI.ps1:

$erroractionpreference = 'silentlycontinue'
$Global:here = (Get-Location).Path
$Global:ScriptsPath = (Get-Location).Path

function ButtonsName{
$global:Button1Name = "Script1"
$global:Button2Name = "Script2"
$global:Button3Name = "Script3"
$global:Button4Name = "Script4"
$global:Button5Name = "Script5"
}

function RunUI($xamlpath){
Add-Type -AssemblyName PresentationFramework
$global:SelectedButton = $null

[xml]$xaml = Get-Content -Path "$xamlpath\interface.xaml"
$reader = (New-Object System.Xml.XmlNodeReader $xaml)
$window = [Windows.Markup.XamlReader]::Load($reader)

# Accéder aux éléments XAML
$MainGrid = $window.FindName("MainGrid")
$xaml.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]") | ForEach {
    Set-Variable -Name ($_.Name) -Value $Window.FindName($_.Name) -Scope Global
}

# Définir l'image de fond dynamiquement
$imagePath = "$xamlpath\interface.png"
$image = New-Object System.Windows.Controls.Image
$image.Source = [System.Windows.Media.Imaging.BitmapImage]::new([uri]$imagePath)
$image.Stretch = [System.Windows.Media.Stretch]::Fill
$MainGrid.Children.Insert(0, $image)

# Ajouter les événements Checked aux boutons radio pour mettre à jour la variable
ButtonsName
$Bouton1.Add_Checked({ $global:SelectedButton = $global:Button1Name })
$Bouton2.Add_Checked({ $global:SelectedButton = $global:Button2Name })
$Bouton3.Add_Checked({ $global:SelectedButton = $global:Button3Name })
$Bouton4.Add_Checked({ $global:SelectedButton = $global:Button4Name })
$Bouton5.Add_Checked({ $global:SelectedButton = $global:Button5Name })

# Ajouter l'événement Click au bouton OK
$OkButton.Add_Click({
    if ($global:SelectedButton) {
    $global:cmd = "$here\$SelectedButton.ps1"
    $window.Close()
    }
})

# Ajouter l'événement Click au bouton Cancel
$CancelButton.Add_Click({
    Write-Host "Operation Canceled"
    $window.Close()
})

# Afficher la fenêtre
$window.ShowDialog() | Out-Null
# return $retour
}

RunUI($here)
Start-Process -FilePath powershell.exe -ArgumentList "-file $cmd" -WindowStyle Hidden -WorkingDirectory $here

script1.ps1:

$Global:here = (Get-Location).Path
"Script1" > "$here\output.txt"

...

 

interface.xaml:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Exemple UI" Height="300" Width="400" Background="White">
    <Grid x:Name="MainGrid">
        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
            <RadioButton x:Name="Bouton1" Content="Bouton 1" GroupName="Options" Margin="10"/>
            <RadioButton x:Name="Bouton2" Content="Bouton 2" GroupName="Options" Margin="10"/>
            <RadioButton x:Name="Bouton3" Content="Bouton 3" GroupName="Options" Margin="10"/>
            <RadioButton x:Name="Bouton4" Content="Bouton 4" GroupName="Options" Margin="10"/>
            <RadioButton x:Name="Bouton5" Content="Bouton 5" GroupName="Options" Margin="10"/>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="10">
                <Button x:Name="OkButton" Content="OK" Width="75" Margin="10"/>
                <Button x:Name="CancelButton" Content="Cancel" Width="75" Margin="10"/>
            </StackPanel>
        </StackPanel>
    </Grid>
</Window>

 

no more

to run script instead of script1.ps1, replace:

$global:Button1Name = "Script1"

by 

$global:Button1Name = "<your script name>"

 

 

 

Commenter cet article