lundi 23 novembre 2020

How to select checked boxes names when clicking button in Powershell

Action ADD CLICK on my button $Tab1BTNCreation: i want to copy a folder including some subfolders (mentionnned by checked checkboxes) to a certain destination. The selected checkboxes indicates exaclty wich folder to copy. I've tried a lot of options but nothing is working at that point. The concerned variables are $Checkboxes and $CheckboxesNames in TAB1. I've gave names to my checkboxes variables so the '-unclude' parameter can recognize the folder name... Scroll down till '///// HERE //// Bouton Creation /////////' Thank you for your help.

Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass 
Add-type -Assembly System.Windows.Forms 

[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="Programme de tests QA" Height="515" Width="828" ResizeMode="NoResize">
    <Grid>
        <TabControl HorizontalAlignment="Left" Height="500" VerticalAlignment="Top" Width="825">

            <TabControl.Resources>
                <Style TargetType="TabItem">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="TabItem">
                                <Border Name="Border" BorderThickness="0,0,0,0" CornerRadius="5,5,0,0" Margin="2,0">
                                    <ContentPresenter x:Name="Tab1"
                                        VerticalAlignment="Center"
                                        HorizontalAlignment="Center"
                                        ContentSource="Header"
                                        Margin="10,2"/>
                                </Border>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsSelected" Value="True">
                                        <Setter TargetName="Border" Property="Background" Value="#292929" />
                                    </Trigger>
                                    <Trigger Property="IsSelected" Value="False">
                                        <Setter TargetName="Border" Property="Background" Value="#FF23BBB8" />
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </TabControl.Resources>
           
            <TabItem x:Name="tab1" Header="CR`ÉATION" Height="40" Width="273" Background="#292929" FontFamily="Segoe UI Semibold" Foreground="white" IsSelected="True">
                <Grid Background="#292929" Width="840" Height="455" Margin="-7,1,-14,-4">
                    <Label x:Name="titre1" Content="cr`éation d'une nouvelle serie" FontFamily="Segoe UI" FontSize="20" Foreground="#797979" HorizontalAlignment="Left" Height="47" Margin="49,47,0,0" VerticalAlignment="Top" Width="723"/>
                    <Line X1="55" X2="440" Y1="99" Y2="99" Stroke="#5f5f5f" StrokeThickness="1"></Line>
                    <Line X1="55" X2="440" Y1="200" Y2="200" Stroke="#5f5f5f" StrokeThickness="1"></Line>
                    <Line X1="480" X2="770" Y1="99" Y2="99" Stroke="#5f5f5f" StrokeThickness="1"></Line>
                    <Line X1="480" X2="770" Y1="200" Y2="200" Stroke="#5f5f5f" StrokeThickness="1"></Line>
                    <TextBlock HorizontalAlignment="Left" Height="21" Margin="60,121,0,0" TextWrapping="Wrap" Text="de" VerticalAlignment="Top" Width="36" Foreground="#797979"/>
                    <TextBlock HorizontalAlignment="Left" Height="21" Margin="60,161,0,0" TextWrapping="Wrap" Text="vers" VerticalAlignment="Top" Width="36" Foreground="#797979"/>
                    
                    <ComboBox x:Name="CBBdeRep1" HorizontalAlignment="Left" Height="20" Margin="107,120,0,0" VerticalAlignment="Top" Width="103" BorderBrush="#FF23BBB8" BorderThickness="0" FontSize="10" Foreground="#797979"/>
                    <ComboBox x:Name="CBBdeSerie1" HorizontalAlignment="Left" Height="20" Margin="227,120,0,0" VerticalAlignment="Top" Width="211" BorderBrush="#FF23BBB8" BorderThickness="0" FontSize="10" Foreground="#797979"/>
                    <ComboBox x:Name="CBBversRep1" HorizontalAlignment="Left" Height="20" Margin="107,159,0,0" VerticalAlignment="Top" Width="103" BorderBrush="#FF23BBB8" BorderThickness="0" FontSize="10" Foreground="#797979"/>
                    <TextBox x:Name="TBversSerie1" HorizontalAlignment="Left" Height="20" Margin="227,159,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="211" Padding="2" MaxLength="33" FontSize="10" Foreground="#797979" />
                     
                     <Grid x:Name="CheckBoxGroup1">
                    <CheckBox x:Name="CBbin1" Content=" bin" HorizontalAlignment="Left" Height="19" Margin="480,118,0,0" VerticalAlignment="Top" Width="92" Foreground="#797979"/>
                    <CheckBox x:Name="CBexport1" Content=" export" HorizontalAlignment="Left" Height="19" Margin="480,142,0,0" VerticalAlignment="Top" Width="92" Foreground="#797979"/>
                    <CheckBox x:Name="CBparam1" Content=" param" HorizontalAlignment="Left" Height="19" Margin="480,166,0,0" VerticalAlignment="Top" Width="92" Foreground="#797979"/>
                    <CheckBox x:Name="CBlist1" Content=" list" HorizontalAlignment="Left" Height="19" Margin="600,118,0,0" VerticalAlignment="Top" Width="92" Foreground="#797979"/>
                    <CheckBox x:Name="CBscript1" Content=" script" HorizontalAlignment="Left" Height="19" Margin="600,142,0,0" VerticalAlignment="Top" Width="92" Foreground="#797979"/>
                    <CheckBox x:Name="CBtemplate1" Content=" template" HorizontalAlignment="Left" Height="19" Margin="600,166,0,0" VerticalAlignment="Top" Width="92" Foreground="#797979"/>
                    </Grid>
                   
                    <Button x:Name="BTNcreation" Content="cr`éer la s`érie et synchroniser" FontSize="15" HorizontalAlignment="Left" Height="48" Margin="480,335,0,0"  VerticalAlignment="Top" Foreground="white" Background="#FF23BBB8" Width="290"/>
                    <TextBox x:Name="TBmessages1" HorizontalAlignment="Left" Height="145" Margin="55,238,0,0" TextWrapping="Wrap"  VerticalScrollBarVisibility="Visible" Text="Affichage des messages d'erreurs." Foreground="#797979" Padding="6" FontSize="10" VerticalAlignment="Top" Width="383"/>

                </Grid>
            </TabItem>



            <TabItem x:Name="tab2" Header="SYNCHRONISATION" Background="#FF00565F" Height="40" Width="273" BorderBrush="#FF00565F" FontSize="12" Foreground="White" FontFamily="Segoe UI Semibold">
                <Grid Background="#292929" Width="840" Height="455" Margin="-7,1,-14,-4">
                    <Label Content="synchronisation d'une s`érie" FontFamily="Segoe UI" FontSize="20" Foreground="#797979" HorizontalAlignment="Left" Height="47" Margin="49,47,0,0" VerticalAlignment="Top" Width="723"/>
                    <Line X1="55" X2="440" Y1="200" Y2="200" Stroke="#5f5f5f" StrokeThickness="1"></Line>
                    <Line X1="55" X2="440" Y1="99" Y2="99" Stroke="#5f5f5f" StrokeThickness="1"></Line>
                    <Line X1="480" X2="770" Y1="99" Y2="99" Stroke="#5f5f5f" StrokeThickness="1"></Line>
                    <Line X1="480" X2="770" Y1="200" Y2="200" Stroke="#5f5f5f" StrokeThickness="1"></Line>
                    <TextBlock HorizontalAlignment="Left" Height="21" Margin="60,121,0,0" TextWrapping="Wrap" Text="de" VerticalAlignment="Top" Width="36" Foreground="#797979"/>
                    <TextBlock HorizontalAlignment="Left" Height="21" Margin="60,161,0,0" TextWrapping="Wrap" Text="vers" VerticalAlignment="Top" Width="36" Foreground="#797979"/>


                    <ComboBox x:Name="CBBdeRep2" HorizontalAlignment="Left" Height="20" Margin="107,120,0,0" VerticalAlignment="Top" Width="103" FontSize="10" Foreground="#797979" />
                    <ComboBox x:Name="CBBdeSerie2" HorizontalAlignment="Left" Height="20" Margin="227,120,0,0" VerticalAlignment="Top" Width="211" FontSize="10" Foreground="#797979"/>
                    <ComboBox x:Name="CBBversRep2" HorizontalAlignment="Left" Height="20" Margin="107,159,0,0" VerticalAlignment="Top" Width="103" FontSize="10" Foreground="#797979" />
                    <ComboBox x:Name="CBBversSerie2" HorizontalAlignment="Left" Height="20" Margin="227,159,0,0" VerticalAlignment="Top" Width="211" FontSize="10" Foreground="#797979" />
                    <CheckBox x:Name="CBbin2" Content=" bin" HorizontalAlignment="Left" Height="19" Margin="480,118,0,0" VerticalAlignment="Top" Width="92" Foreground="#797979"/>
                    <CheckBox x:Name="CBexport2" Content=" export" HorizontalAlignment="Left" Height="19" Margin="480,142,0,0" VerticalAlignment="Top" Width="92" Foreground="#797979"/>
                    <CheckBox x:Name="CBparam2" Content=" param" HorizontalAlignment="Left" Height="19" Margin="480,166,0,0" VerticalAlignment="Top" Width="92" Foreground="#797979"/>
                    <CheckBox x:Name="CBlist2" Content=" list" HorizontalAlignment="Left" Height="19" Margin="600,118,0,0" VerticalAlignment="Top" Width="92" Foreground="#797979"/>
                    <CheckBox x:Name="CBscript2" Content=" script" HorizontalAlignment="Left" Height="19" Margin="600,142,0,0" VerticalAlignment="Top" Width="92" Foreground="#797979"/>
                    <CheckBox x:Name="CBtemplate2" Content=" template" HorizontalAlignment="Left" Height="19" Margin="600,166,0,0" VerticalAlignment="Top" Width="92" Foreground="#797979"/>
                    
                    <TextBox x:Name="TBmessages2" HorizontalAlignment="Left" Height="145" Margin="55,238,0,0" TextWrapping="Wrap"  VerticalScrollBarVisibility="Visible" Text="Affichage des messages d'erreurs." Foreground="#797979" Padding="6" FontSize="10" VerticalAlignment="Top" Width="383"/>
                    <Button x:Name="BTNsynchro" Content="synchroniser la s`érie" FontSize="15" HorizontalAlignment="Left" Height="48" Margin="480,335,0,0"  VerticalAlignment="Top" Foreground="white" Background="#FF23BBB8" Width="290"/>

                </Grid>


            </TabItem>



            <TabItem x:Name="tab3" Header="EX`ÉCUTION"  Background="#FF00565F" Height="40" Width="273" BorderBrush="#FF00565F" FontSize="12" Foreground="white" FontFamily="Segoe UI Semibold">
                <Grid Background="#292929" Width="840" Height="455" Margin="-7,1,-14,-4"  >
                    <Label x:Name="titre3" Content="ex`écution des tests QA" FontFamily="Segoe UI" FontSize="20" Foreground="#797979" HorizontalAlignment="Left" Height="47" Margin="49,47,0,0" VerticalAlignment="Top" Width="723"/>
                    <Line X1="55" X2="335" Y1="99" Y2="99" Stroke="#5f5f5f" StrokeThickness="1"></Line>
                    <Line X1="55" X2="335" Y1="160" Y2="160" Stroke="#5f5f5f" StrokeThickness="1"></Line>
                    <Line X1="480" X2="770" Y1="99" Y2="99" Stroke="#5f5f5f" StrokeThickness="1"></Line>
                    <Line X1="480" X2="770" Y1="250" Y2="250" Stroke="#5f5f5f" StrokeThickness="1"></Line>
                    <Line X1="480" X2="770" Y1="200" Y2="200" Stroke="#5f5f5f" StrokeThickness="1"></Line>
                    <TextBlock HorizontalAlignment="Left" Height="21" Margin="54,121,0,0" TextWrapping="Wrap" Text="version" VerticalAlignment="Top" Width="55" Foreground="#797979"/>
                    <TextBlock HorizontalAlignment="Left" Height="21" Margin="482,121,0,0" TextWrapping="Wrap" Text="`équipe" VerticalAlignment="Top" Width="55" Foreground="#797979"/>
                    <TextBlock HorizontalAlignment="Left" Height="21" Margin="482,161,0,0" TextWrapping="Wrap" Text="liste" VerticalAlignment="Top" Width="36" Foreground="#797979"/>

                    <ComboBox x:Name="CBBversion3" HorizontalAlignment="Left" Height="20" Margin="113,120,0,0" VerticalAlignment="Top" Width="70" FontSize="10" Foreground="#797979" />
                    <TextBox x:Name="TBbuild3" HorizontalAlignment="Left" Height="20" Margin="201,120,0,0"  TextWrapping="Wrap" Text="Build" VerticalAlignment="Top" Width="77" Padding="2" MaxLength="4" FontSize="10" Foreground="#797979" />
                    <TextBox x:Name="TBrevision3" HorizontalAlignment="Left" Height="20" Margin="297,120,0,0"  TextWrapping="Wrap" Text="R`év." VerticalAlignment="Top" Width="36" Padding="2" MaxLength="3" FontSize="10" Foreground="#797979" />
                    <ComboBox x:Name="CBBequipe3" HorizontalAlignment="Left" Height="20" Margin="542,120,0,0" VerticalAlignment="Top" Width="150" BorderBrush="#FF23BBB8" BorderThickness="0" FontSize="10" Foreground="#797979"/>        
                    <ComboBox x:Name="CBBliste3" HorizontalAlignment="Left" Height="20" Margin="542,159,0,0" VerticalAlignment="Top" Width="150" BorderBrush="#FF23BBB8" BorderThickness="0" FontSize="10" Foreground="#797979"/>
                    <CheckBox x:Name="CBrtf3" Content=" produire les fichiers en format .rtf" HorizontalAlignment="Left" Height="19" Margin="480,217,0,0" VerticalAlignment="Top" Width="230"  Foreground="#797979"/>

                    <TextBox x:Name="TBmessages3" HorizontalAlignment="Left" Height="190" Margin="55,193,0,0" VerticalScrollBarVisibility="Visible" TextWrapping="Wrap" Text="Affichage des messages d'erreurs." Foreground="#797979" Padding="6" FontSize="10" VerticalAlignment="Top" Width="383"/>
                    <Button x:Name="BTNexecution" Content="lancer les tests" FontSize="15" HorizontalAlignment="Left" Height="48" Margin="480,335,0,0" VerticalAlignment="Top" Foreground="white" Background="#FF23BBB8" Width="290"/>

                </Grid>
            </TabItem>
        </TabControl>


    </Grid>
</Window>

"@

#Read XAML (Parse it)
$reader=(New-Object System.Xml.XmlNodeReader $XAML)
$Window=[Windows.Markup.XamlReader]::Load( $reader )

#region ----- xaml objects to variables -----

#Connect to Controls TAB2
$Tab1CBB1 = $Window.FindName('CBBdeRep1')
$Tab1CBB2 = $Window.FindName('CBBdeSerie1')
$Tab1CBB3 = $Window.FindName('CBBversRep1')
$Tab1TB4 = $Window.FindName('TBversSerie1')
($Tab1CBbin = $Window.FindName('CBbin1')).Name = 'Bin'
($Tab1CBexport = $Window.FindName('CBexport1')).Name = 'Export'
($Tab1CBparam = $Window.FindName('CBparam1')).Name = 'Param'
($Tab1CBlist = $Window.FindName('CBlist1')).Name = 'List'
($Tab1CBscript = $Window.FindName('CBscript1')).Name = 'QA\Script'
($Tab1CBtemplate = $Window.FindName('CBtemplate1')).Name = 'Template'
$Tab1TBmessages = $Window.findName("TBmessages1")
$Tab1BTNcreation = $Window.findName("BTNcreation")


#Connect to Controls TAB2
$Tab2CBB1 = $Window.FindName('CBBdeRep2')
$Tab2CBB2 = $Window.FindName('CBBdeSerie2')
$Tab2CBB3 = $Window.FindName('CBBversRep2')
$Tab2CBB4 = $Window.FindName('CBBversSerie2')
$Tab2CBbin = $Window.FindName('CBbin2')
$Tab2CBexport = $Window.FindName('CBbin2')
$Tab2CBparam = $Window.FindName('CBparam2')
$Tab2CBlist = $Window.FindName('CBlist2')
$Tab2CBscript = $Window.FindName('CBscript2')
$Tab2CBtemplate = $Window.FindName('CBtemplate2')
$Tab2TBmessages = $Window.findName("TBmessages2")
$Tab2BTNsynchro = $Window.findName("BTNsyncro")

#Connect to Controls TAB2
$Tab3CBBversion = $Window.findName("CBBVersion3")
$Tab3TBbuild = $Window.findName("TBbuild3")
$Tab3TBrev = $Window.findName("TBrevision3")
$Tab3CBBequipe = $Window.findName("CBBequipe3")
$Tab3CBBliste = $Window.findName("CBBliste3")
$Tab3CBrtf = $Window.findName("CBrtf3")
$Tab3TBmessages = $Window.findName("TBmessages3")
$Tab3BTNexecution = $Window.findName("BTNexecution")
#endregion

#region ---- Path variables  ----- 
$pathDE='E:\Scripts\BibliothequePowershell\Melanie\GUI-ProgTestQA\Simulation\DE\'
$pathVERS='E:\Scripts\BibliothequePowershell\Melanie\GUI-ProgTestQA\Simulation\VERS\'
$pathDBQcust01='E:\Scripts\BibliothequePowershell\Melanie\GUI-ProgTestQA\Simulation\DE\DBQcust01\Sets\' 
$pathQA14='E:\Scripts\BibliothequePowershell\Melanie\GUI-ProgTestQA\Simulation\DE\QA14\Sets\'
$pathQA15='E:\Scripts\BibliothequePowershell\Melanie\GUI-ProgTestQA\Simulation\DE\QA15\Sets\'
$pathSERIE = 'E:\Scripts\BibliothequePowershell\Melanie\GUI-ProgTestQA\Simulation\DE\QA14\Sets'  #simulation
$pathSIMULATION = 'E:\Scripts\BibliothequePowershell\Melanie\GUI-ProgTestQA\Simulation'
#endregion ----

#lists of series
$listQA14 = Get-ChildItem $pathQA14 -Directory -Name 
$listQA15 = Get-ChildItem $pathQA15 -Directory -Name 
$listDBQcust01 = Get-ChildItem $pathDBQcust01 -Directory -Name 
$FolderLists = Get-ChildItem $pathSIMULATION\Listes -Include *.txt -Name



#------------- TAB1 ------------------

#region /// ComboBox 1 : DE folder
$FoldersCBB1 = @('--- choisir ---','DBQcust01','QA14','QA15')
$Tab1CBB1.SelectedItem = '--- choisir ---'
foreach ($Folder in $FoldersCBB1) {
$Tab1CBB1.Items.Add($Folder)
}
#endregion



#region /// ComboBox 2 : DE SERIE    (éventuellement changer pour SWITCH)
$Tab1CBB1.add_SelectionChanged({
$Selection = ($Tab1CBB1.SelectedItem.ToString())

  if ($Selection -eq 'QA14') 
    { 
        $Tab1CBB2.Items.Clear()
        $Tab1CBB2.Items.Add('--- choisir une série sous QA14 ---')
        $Tab1CBB2.SelectedItem = '--- choisir une série sous QA14 ---' 
        foreach ($Serie in $listQA14) {
            $Tab1CBB2.Items.Add($Serie)
        }   
    }   
    
    elseif ($Selection -eq 'QA15') 
    {
            $Tab1CBB2.Items.Clear()
            $Tab1CBB2.Items.Add('--- choisir une série sous QA15 ---')
            $Tab1CBB2.SelectedItem = '--- choisir une série sous QA15 ---'
            foreach ($Serie in $listQA15)  {
                $Tab1CBB2.Items.Add($Serie)
            }
    }    
    
    elseif ($Selection -eq 'DBQcust01') 
    {
            $Tab1CBB2.Items.Clear()
            $Tab1CBB2.Items.Add('--- choisir une série sous DBQcust01 ---')
            $Tab1CBB2.SelectedItem = '--- choisir une série sous DBQcust01 ---'
            foreach ($Serie in $listDBQcust01) {
                 $Tab1CBB2.Items.Add($Serie)
            }
   }

})

#endregion




#region /// ComboBox 3 : VERS folder
$FoldersCBB3 = @('SPMT','OTHER')
$Tab1CBB3.Items.Add('--- choisir ---')
$Tab1CBB3.SelectedItem = '--- choisir ---' 
foreach ($Folder in $FoldersCBB3) {
    $Tab1CBB3.Items.Add($Folder)
}

#endregion




#region /// TextBox 4 : VERS Série 
#à venir (essayer de mettre le contenu des sélections des cbb automatiquement dans le textbox)

$Tab1TB4.text = "SÉRIE+ÉQUIPE"
#endregion




#region ///// HERE //// Bouton Creation  /////////

$Tab1BTNcreation.Add_Click({


    $SelectionCBB1 = ($Tab1CBB1.SelectedValue)
    $SelectionCBB2 = ($Tab1CBB2.SelectedValue)
    $SelectionCBB3 = ($Tab1CBB3.SelectedValue)
    $SelectionTB4 = ($Tab1TB4.Text)
    

    $CheckedBoxes = get-variable $Tab1CB* | Where-object {$_.IsChecked = $true}
    $CheckBoxesNames= $CheckedBoxes.Name


   New-item -ItemType Directory -Path "$pathVERS\$SelectionCBB3" -Name "$SelectionTB4"
   
   Copy-Item -Path "$pathDE\$SelectionCBB1\$SelectionCBB2" -include $CheckedBoxesNames -Recurse -Destination "$pathVERS\$SelectionCBB3\$SelectionTB4" 

})

#endregion






#------------- TAB2 ------------------

#------------- TAB3 ------------------






$Window.ShowDialog() | out-null



Aucun commentaire:

Enregistrer un commentaire